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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled#buffer() . 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: ByteBufUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccumulateWithoutCopy() {
	int sourceLength = 128;
	int sourceReaderIndex = 32;
	int expectedAccumulationSize = 16;

	ByteBuf src = createSourceBuffer(sourceLength, sourceReaderIndex, expectedAccumulationSize);
	ByteBuf target = Unpooled.buffer(expectedAccumulationSize);

	// If src has enough data and no data has been copied yet, src will be returned without modification.
	ByteBuf accumulated = ByteBufUtils.accumulate(target, src, expectedAccumulationSize, target.readableBytes());

	assertSame(src, accumulated);
	assertEquals(sourceReaderIndex, src.readerIndex());
	verifyBufferContent(src, sourceReaderIndex, expectedAccumulationSize);
}
 
Example 2
Source File: ByteBufUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create a source buffer whose length is <tt>size</tt>. The content between <tt>readerIndex</tt> and
 * <tt>readerIndex + accumulationSize</tt> is <tt>ACCUMULATION_BYTE</tt> and the remaining is
 * <tt>NON_ACCUMULATION_BYTE</tt>.
 *
 * @param size The size of the source buffer.
 * @param readerIndex The reader index of the source buffer.
 * @param accumulationSize The size of bytes that will be read for accumulating.
 *
 * @return The required source buffer.
 */
private ByteBuf createSourceBuffer(int size, int readerIndex, int accumulationSize) {
	ByteBuf buf = Unpooled.buffer(size);

	for (int i = 0; i < readerIndex; i++) {
		buf.writeByte(NON_ACCUMULATION_BYTE);
	}

	for (int i = readerIndex; i < readerIndex + accumulationSize; i++) {
		buf.writeByte(ACCUMULATION_BYTE);
	}

	for (int i = readerIndex + accumulationSize; i < size; i++) {
		buf.writeByte(NON_ACCUMULATION_BYTE);
	}

	buf.readerIndex(readerIndex);
	return buf;
}
 
Example 3
Source File: HandlerRedirectUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example 4
Source File: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example 5
Source File: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example 6
Source File: ByteBufUtilsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccumulateWithCopy() {
	int sourceLength = 128;
	int firstSourceReaderIndex = 32;
	int secondSourceReaderIndex = 0;
	int expectedAccumulationSize = 128;

	int firstAccumulationSize = sourceLength - firstSourceReaderIndex;
	int secondAccumulationSize = expectedAccumulationSize - firstAccumulationSize;

	ByteBuf firstSource = createSourceBuffer(sourceLength, firstSourceReaderIndex, firstAccumulationSize);
	ByteBuf secondSource = createSourceBuffer(sourceLength, secondSourceReaderIndex, secondAccumulationSize);

	ByteBuf target = Unpooled.buffer(expectedAccumulationSize);

	// If src does not have enough data, src will be copied into target and null will be returned.
	ByteBuf accumulated = ByteBufUtils.accumulate(
		target,
		firstSource,
		expectedAccumulationSize,
		target.readableBytes());
	assertNull(accumulated);
	assertEquals(sourceLength, firstSource.readerIndex());
	assertEquals(firstAccumulationSize, target.readableBytes());

	// The remaining data will be copied from the second buffer, and the target buffer will be returned
	// after all data is accumulated.
	accumulated = ByteBufUtils.accumulate(
		target,
		secondSource,
		expectedAccumulationSize,
		target.readableBytes());
	assertSame(target, accumulated);
	assertEquals(secondSourceReaderIndex + secondAccumulationSize, secondSource.readerIndex());
	assertEquals(expectedAccumulationSize, target.readableBytes());

	verifyBufferContent(accumulated, 0, expectedAccumulationSize);
}