本文整理了Java中org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf
类的一些代码示例,展示了ByteBuf
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf
类的具体详情如下:
包路径:org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf
类名称:ByteBuf
暂无
代码示例来源:origin: apache/flink
@Override
public KvStateInternalRequest deserializeMessage(ByteBuf buf) {
KvStateID kvStateId = new KvStateID(buf.readLong(), buf.readLong());
int length = buf.readInt();
Preconditions.checkArgument(length >= 0,
"Negative length for key and namespace. " +
"This indicates a serialization error.");
byte[] serializedKeyAndNamespace = new byte[length];
if (length > 0) {
buf.readBytes(serializedKeyAndNamespace);
}
return new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
}
}
代码示例来源:origin: apache/flink
/**
* Serializes the exception containing the failure message sent to the
* {@link org.apache.flink.queryablestate.network.Client} in case of
* protocol related errors.
*
* @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the message into.
* @param requestId The id of the request to which the message refers to.
* @param cause The exception thrown at the server.
* @return A {@link ByteBuf} containing the serialized message.
*/
public static ByteBuf serializeRequestFailure(
final ByteBufAllocator alloc,
final long requestId,
final Throwable cause) throws IOException {
final ByteBuf buf = alloc.ioBuffer();
// Frame length is set at the end
buf.writeInt(0);
writeHeader(buf, MessageType.REQUEST_FAILURE);
buf.writeLong(requestId);
try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
ObjectOutput out = new ObjectOutputStream(bbos)) {
out.writeObject(cause);
}
// Set frame length
int frameLength = buf.readableBytes() - Integer.BYTES;
buf.setInt(0, frameLength);
return buf;
}
代码示例来源:origin: apache/flink
@Override
public void close() throws Exception {
if (!isClosed) {
// If we did not consume the whole buffer yet, we have to release
// it here. Otherwise, it's the responsibility of the consumer.
if (!isEndOfInput) {
buf.release();
}
isClosed = true;
}
}
代码示例来源:origin: apache/flink
private ByteBuf readChunk() {
if (isClosed) {
return null;
} else if (buf.readableBytes() <= chunkSize) {
isEndOfInput = true;
// Don't retain as the consumer is responsible to release it
return buf.slice();
} else {
// Return a chunk sized slice of the buffer. The ref count is
// shared with the original buffer. That's why we need to retain
// a reference here.
return buf.readSlice(chunkSize).retain();
}
}
代码示例来源:origin: apache/flink
@Override
public KvStateResponse deserializeMessage(ByteBuf buf) {
int length = buf.readInt();
Preconditions.checkArgument(length >= 0,
"Negative length for state content. " +
"This indicates a serialization error.");
byte[] content = new byte[length];
buf.readBytes(content);
return new KvStateResponse(content);
}
}
代码示例来源:origin: com.alibaba.blink/flink-table
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
byte[] request = new byte[buf.readableBytes()];
buf.readBytes(request);
byte call = request[0];
switch (call) {
case TableServiceMessage.GET_PARTITIONS:
getPartitions(ctx, request);
break;
case TableServiceMessage.READ:
read(ctx, request);
break;
case TableServiceMessage.WRITE:
write(ctx, request);
break;
case TableServiceMessage.INITIALIZE_PARTITION:
initializePartition(ctx, request);
break;
default:
LOG.error("Unsupported call: " + call);
}
}
代码示例来源:origin: com.alibaba.blink/flink-runtime
@Override
ByteBuf write(ByteBufAllocator allocator) throws IOException {
final ByteBuf result = allocateBuffer(allocator, ID);
try (ObjectOutputStream oos = new ObjectOutputStream(new ByteBufOutputStream(result))) {
oos.writeObject(cause);
if (receiverId != null) {
result.writeBoolean(true);
receiverId.writeTo(result);
} else {
result.writeBoolean(false);
}
// Update frame length...
result.setInt(0, result.readableBytes());
return result;
}
catch (Throwable t) {
result.release();
if (t instanceof IOException) {
throw (IOException) t;
} else {
throw new IOException(t);
}
}
}
代码示例来源:origin: org.apache.flink/flink-runtime
static BufferResponse readFrom(ByteBuf buffer) {
InputChannelID receiverId = InputChannelID.fromByteBuf(buffer);
int sequenceNumber = buffer.readInt();
int backlog = buffer.readInt();
boolean isBuffer = buffer.readBoolean();
int size = buffer.readInt();
ByteBuf retainedSlice = buffer.readSlice(size).retain();
return new BufferResponse(retainedSlice, isBuffer, sequenceNumber, receiverId, backlog);
}
}
代码示例来源:origin: apache/flink
/**
* Serializes the failure message sent to the
* {@link org.apache.flink.queryablestate.network.Client} in case of
* server related errors.
*
* @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the message into.
* @param cause The exception thrown at the server.
* @return The failure message.
*/
public static ByteBuf serializeServerFailure(
final ByteBufAllocator alloc,
final Throwable cause) throws IOException {
final ByteBuf buf = alloc.ioBuffer();
// Frame length is set at end
buf.writeInt(0);
writeHeader(buf, MessageType.SERVER_FAILURE);
try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
ObjectOutput out = new ObjectOutputStream(bbos)) {
out.writeObject(cause);
}
// Set frame length
int frameLength = buf.readableBytes() - Integer.BYTES;
buf.setInt(0, frameLength);
return buf;
}
代码示例来源:origin: apache/flink
/**
* Tests server failure serialization.
*/
@Test
public void testServerFailureSerialization() throws Exception {
IllegalStateException cause = new IllegalStateException("Expected test");
ByteBuf buf = MessageSerializer.serializeServerFailure(alloc, cause);
int frameLength = buf.readInt();
assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
Throwable request = MessageSerializer.deserializeServerFailure(buf);
assertEquals(buf.readerIndex(), frameLength + 4);
assertEquals(cause.getClass(), request.getClass());
assertEquals(cause.getMessage(), request.getMessage());
}
代码示例来源:origin: org.apache.flink/flink-runtime_2.11
@Override
ByteBuf write(ByteBufAllocator allocator) throws IOException {
ByteBuf result = null;
try {
result = allocateBuffer(allocator, ID, 16 + 16 + 4 + 16 + 4);
partitionId.getPartitionId().writeTo(result);
partitionId.getProducerId().writeTo(result);
result.writeInt(queueIndex);
receiverId.writeTo(result);
result.writeInt(credit);
return result;
}
catch (Throwable t) {
if (result != null) {
result.release();
}
throw new IOException(t);
}
}
代码示例来源:origin: apache/flink
private void sendError(ChannelHandlerContext ctx, String error) {
if (ctx.channel().isActive()) {
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.INTERNAL_SERVER_ERROR,
Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET)));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
ctx.writeAndFlush(response);
}
}
}
代码示例来源:origin: com.alibaba.blink/flink-runtime
static TaskEventRequest readFrom(ByteBuf buffer, ClassLoader classLoader) throws IOException {
// directly deserialize fromNetty's buffer
int length = buffer.readInt();
ByteBuffer serializedEvent = buffer.nioBuffer(buffer.readerIndex(), length);
// assume this event's content is read from the ByteBuf (positions are not shared!)
buffer.readerIndex(buffer.readerIndex() + length);
TaskEvent event =
(TaskEvent) EventSerializer.fromSerializedEvent(serializedEvent, classLoader);
ResultPartitionID partitionId =
new ResultPartitionID(
IntermediateResultPartitionID.fromByteBuf(buffer),
ExecutionAttemptID.fromByteBuf(buffer));
InputChannelID receiverId = InputChannelID.fromByteBuf(buffer);
return new TaskEventRequest(event, partitionId, receiverId);
}
}
代码示例来源:origin: apache/flink
/**
* Helper for serializing the messages.
*
* @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the message into.
* @param requestId The id of the request to which the message refers to.
* @param messageType The {@link MessageType type of the message}.
* @param payload The serialized version of the message.
* @return A {@link ByteBuf} containing the serialized message.
*/
private static ByteBuf writePayload(
final ByteBufAllocator alloc,
final long requestId,
final MessageType messageType,
final byte[] payload) {
final int frameLength = HEADER_LENGTH + REQUEST_ID_SIZE + payload.length;
final ByteBuf buf = alloc.ioBuffer(frameLength + Integer.BYTES);
buf.writeInt(frameLength);
writeHeader(buf, messageType);
buf.writeLong(requestId);
buf.writeBytes(payload);
return buf;
}
代码示例来源:origin: apache/flink
/**
* De-serializes the header and returns the {@link MessageType}.
* <pre>
* <b>The buffer is expected to be at the request id position.</b>
* </pre>
* @param buf The {@link ByteBuf} containing the serialized request id.
* @return The request id.
*/
public static long getRequestId(final ByteBuf buf) {
return buf.readLong();
}
代码示例来源:origin: com.alibaba.blink/flink-runtime
private boolean readOrDiscardBufferResponse(ByteBuf data) {
checkState(currentNettyMessage != null && currentNettyMessage instanceof NettyMessage.BufferResponse);
NettyMessage.BufferResponse bufferResponse = (NettyMessage.BufferResponse) currentNettyMessage;
// If current buffer is empty, then no more data to receive
if (bufferResponse.dataBufferSize == 0) {
return true;
}
ByteBuf dataBuffer = bufferResponse.getBuffer();
if (remainingBufferSize < 0) {
remainingBufferSize = bufferResponse.dataBufferSize;
}
if (dataBuffer != null) {
remainingBufferSize -= copyToTargetBuffer(data, dataBuffer, remainingBufferSize);
} else {
int actualBytesToDiscard = Math.min(data.readableBytes(), remainingBufferSize);
data.readerIndex(data.readerIndex() + actualBytesToDiscard);
remainingBufferSize -= actualBytesToDiscard;
}
return remainingBufferSize == 0;
}
代码示例来源:origin: apache/flink
/**
* De-serializes the header and returns the {@link MessageType}.
* <pre>
* <b>The buffer is expected to be at the header position.</b>
* </pre>
* @param buf The {@link ByteBuf} containing the serialized header.
* @return The message type.
* @throws IllegalStateException If unexpected message version or message type.
*/
public static MessageType deserializeHeader(final ByteBuf buf) {
// checking the version
int version = buf.readInt();
Preconditions.checkState(version == VERSION,
"Version Mismatch: Found " + version + ", Expected: " + VERSION + '.');
// fetching the message type
int msgType = buf.readInt();
MessageType[] values = MessageType.values();
Preconditions.checkState(msgType >= 0 && msgType < values.length,
"Illegal message type with index " + msgType + '.');
return values[msgType];
}
代码示例来源:origin: apache/flink
@Override
public long progress() {
return buf.readerIndex();
}
代码示例来源:origin: apache/flink
/**
* Helper for serializing the header.
*
* @param buf The {@link ByteBuf} to serialize the header into.
* @param messageType The {@link MessageType} of the message this header refers to.
*/
private static void writeHeader(final ByteBuf buf, final MessageType messageType) {
buf.writeInt(VERSION);
buf.writeInt(messageType.ordinal());
}
代码示例来源:origin: apache/flink
buf.skipBytes(4); // skip frame length
assertEquals("Buffer not recycled", 0, buf.refCnt());
1222112278,
new RuntimeException("Expected test Exception"));
buf.skipBytes(4); // skip frame length
assertEquals("Buffer not recycled", 0, buf.refCnt());
channel.alloc(),
new RuntimeException("Expected test Exception"));
buf.skipBytes(4); // skip frame length
buf = channel.alloc().buffer(4).writeInt(1223823);
assertEquals("Buffer not recycled", 0, buf.refCnt());
内容来源于网络,如有侵权,请联系作者删除!