Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readableBytes()

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readableBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadOnlySlicedBufferTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) {
	ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable();
	int[] actual = new int[actualBytesBuffer.limit()];
	for (int i = 0; i < actual.length; ++i) {
		actual[i] = actualBytesBuffer.get();
	}
	assertArrayEquals(expectedBytes, actual);

	// verify absolutely positioned read method:
	ByteBuf buffer = (ByteBuf) actualBuffer;
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.getByte(buffer.readerIndex() + i);
	}
	assertArrayEquals(expectedBytes, actual);

	// verify relatively positioned read method:
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.readByte();
	}
	assertArrayEquals(expectedBytes, actual);
}
 
Example 2
Source File: MessageSerializer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: MessageSerializer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: ReadOnlySlicedBufferTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) {
	ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable();
	int[] actual = new int[actualBytesBuffer.limit()];
	for (int i = 0; i < actual.length; ++i) {
		actual[i] = actualBytesBuffer.get();
	}
	assertArrayEquals(expectedBytes, actual);

	// verify absolutely positioned read method:
	ByteBuf buffer = (ByteBuf) actualBuffer;
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.getByte(buffer.readerIndex() + i);
	}
	assertArrayEquals(expectedBytes, actual);

	// verify relatively positioned read method:
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.readByte();
	}
	assertArrayEquals(expectedBytes, actual);
}
 
Example 5
Source File: MessageSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: MessageSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: ByteBufUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Accumulates data from <tt>source</tt> to <tt>target</tt>. If no data has been
 * accumulated yet and <tt>source</tt> has enough data, <tt>source</tt> will be
 * returned directly. Otherwise, data will be copied into <tt>target</tt>. If the
 * size of data copied after this operation has reached <tt>targetAccumulationSize</tt>,
 * <tt>target</tt> will be returned, otherwise <tt>null</tt> will be returned to indicate
 * more data is required.
 *
 * @param target The target buffer.
 * @param source The source buffer.
 * @param targetAccumulationSize The target size of data to accumulate.
 * @param accumulatedSize The size of data accumulated so far.
 *
 * @return The ByteBuf containing accumulated data. If not enough data has been accumulated,
 * 		<tt>null</tt> will be returned.
 */
@Nullable
public static ByteBuf accumulate(ByteBuf target, ByteBuf source, int targetAccumulationSize, int accumulatedSize) {
	if (accumulatedSize == 0 && source.readableBytes() >= targetAccumulationSize) {
		return source;
	}

	int copyLength = Math.min(source.readableBytes(), targetAccumulationSize - accumulatedSize);
	if (copyLength > 0) {
		target.writeBytes(source, copyLength);
	}

	if (accumulatedSize + copyLength == targetAccumulationSize) {
		return target;
	}

	return null;
}
 
Example 8
Source File: NettyMessageClientDecoderDelegateTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<ByteBuf> partitionBuffer(ByteBuf buffer, int partitionSize) {
	List<ByteBuf> result = new ArrayList<>();

	try {
		int bufferSize = buffer.readableBytes();
		for (int position = 0; position < bufferSize; position += partitionSize) {
			int endPosition = Math.min(position + partitionSize, bufferSize);
			ByteBuf partitionedBuffer = ALLOCATOR.buffer(endPosition - position);
			partitionedBuffer.writeBytes(buffer, position, endPosition - position);
			result.add(partitionedBuffer);
		}
	} catch (Throwable t) {
		releaseBuffers(result.toArray(new ByteBuf[0]));
		ExceptionUtils.rethrow(t);
	}

	return result;
}
 
Example 9
Source File: ReadOnlySlicedBufferTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) {
	ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable();
	int[] actual = new int[actualBytesBuffer.limit()];
	for (int i = 0; i < actual.length; ++i) {
		actual[i] = actualBytesBuffer.get();
	}
	assertArrayEquals(expectedBytes, actual);

	// verify absolutely positioned read method:
	ByteBuf buffer = (ByteBuf) actualBuffer;
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.getByte(buffer.readerIndex() + i);
	}
	assertArrayEquals(expectedBytes, actual);

	// verify relatively positioned read method:
	for (int i = 0; i < buffer.readableBytes(); ++i) {
		actual[i] = buffer.readByte();
	}
	assertArrayEquals(expectedBytes, actual);
}
 
Example 10
Source File: MessageSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: MessageSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: RedirectingSslHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
	if (in.readableBytes() >= SSL_RECORD_HEADER_LENGTH && SslHandler.isEncrypted(in)) {
		handleSsl(context);
	} else {
		context.pipeline().replace(this, HTTP_CODEC_HANDLER_NAME, new HttpServerCodec());
		context.pipeline().addAfter(HTTP_CODEC_HANDLER_NAME, NON_SSL_HANDLER_NAME, new NonSslHandler());
	}
}
 
Example 13
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
	if (in.readableBytes() >= SSL_RECORD_HEADER_LENGTH && SslHandler.isEncrypted(in)) {
		handleSsl(context);
	} else {
		context.pipeline().replace(this, HTTP_CODEC_HANDLER_NAME, new HttpServerCodec());
		context.pipeline().addAfter(HTTP_CODEC_HANDLER_NAME, NON_SSL_HANDLER_NAME, new NonSslHandler());
	}
}
 
Example 14
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
	if (in.readableBytes() >= SSL_RECORD_HEADER_LENGTH && SslHandler.isEncrypted(in)) {
		handleSsl(context);
	} else {
		context.pipeline().replace(this, HTTP_CODEC_HANDLER_NAME, new HttpServerCodec());
		context.pipeline().addAfter(HTTP_CODEC_HANDLER_NAME, NON_SSL_HANDLER_NAME, new NonSslHandler());
	}
}