Java Code Examples for org.apache.flink.runtime.io.network.buffer.NetworkBuffer#release()

The following examples show how to use org.apache.flink.runtime.io.network.buffer.NetworkBuffer#release() . 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: ChannelStateReaderImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void readAndVerify(int bufferSize, InputChannelInfo channelInfo, byte[] data, ChannelStateReader reader) throws IOException {
	int dataSize = data.length;
	int iterations = dataSize / bufferSize + (-(dataSize % bufferSize) >>> 31);
	NetworkBuffer buffer = getBuffer(bufferSize);
	try {
		for (int i = 0; i < iterations; i++) {
			String hint = String.format("dataSize=%d, bufferSize=%d, iteration=%d/%d", dataSize, bufferSize, i + 1, iterations);
			boolean isLast = i == iterations - 1;
			assertEquals(hint, isLast ? NO_MORE_DATA : HAS_MORE_DATA, reader.readInputData(channelInfo, buffer));
			assertEquals(hint, isLast ? dataSize - bufferSize * i : bufferSize, buffer.readableBytes());
			assertArrayEquals(hint, Arrays.copyOfRange(data, i * bufferSize, Math.min(dataSize, (i + 1) * bufferSize)), toBytes(buffer));
			buffer.resetReaderIndex();
			buffer.resetWriterIndex();
		}
	} finally {
		buffer.release();
	}
}
 
Example 2
Source File: ChannelStateSerializerImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void write(byte[] data, ChannelStateSerializerImpl serializer, OutputStream baos) throws IOException {
	DataOutputStream out = new DataOutputStream(baos);
	serializer.writeHeader(out);
	NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(data.length), FreeingBufferRecycler.INSTANCE);
	try {
		buffer.writeBytes(data);
		serializer.writeData(out, buffer);
		out.flush();
	} finally {
		buffer.release();
	}
}
 
Example 3
Source File: ChannelStateSerializerImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readAndCheck(byte[] data, ChannelStateSerializerImpl serializer, ByteArrayInputStream is) throws IOException {
	serializer.readHeader(is);
	int size = serializer.readLength(is);
	assertEquals(data.length, size);
	NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(data.length), FreeingBufferRecycler.INSTANCE);
	try {
		int read = serializer.readData(is, wrap(buffer), size);
		assertEquals(size, read);
		assertArrayEquals(data, readBytes(buffer));
	} finally {
		buffer.release();
	}
}
 
Example 4
Source File: ChannelStateReaderImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private byte[] writeSomeBytes(int bytesCount, DataOutputStream out, ChannelStateSerializer serializer) throws IOException {
	byte[] bytes = generateData(bytesCount);
	NetworkBuffer buf = getBuffer(bytesCount);
	try {
		buf.writeBytes(bytes);
		serializer.writeData(out, buf);
		return bytes;
	} finally {
		buf.release();
	}
}