Java Code Examples for org.apache.flink.runtime.io.network.buffer.Buffer#getNioBufferReadable()

The following examples show how to use org.apache.flink.runtime.io.network.buffer.Buffer#getNioBufferReadable() . 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: BufferReaderWriterUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
static long writeToByteChannel(
		FileChannel channel,
		Buffer buffer,
		ByteBuffer[] arrayWithHeaderBuffer) throws IOException {

	final ByteBuffer headerBuffer = arrayWithHeaderBuffer[0];
	headerBuffer.clear();
	headerBuffer.putInt(buffer.isBuffer() ? HEADER_VALUE_IS_BUFFER : HEADER_VALUE_IS_EVENT);
	headerBuffer.putInt(buffer.getSize());
	headerBuffer.flip();

	final ByteBuffer dataBuffer = buffer.getNioBufferReadable();
	arrayWithHeaderBuffer[1] = dataBuffer;

	final long bytesExpected = HEADER_LENGTH + dataBuffer.remaining();

	// The file channel implementation guarantees that all bytes are written when invoked
	// because it is a blocking channel (the implementation mentioned it as guaranteed).
	// However, the api docs leaves it somewhat open, so it seems to be an undocumented contract in the JRE.
	// We build this safety net to be on the safe side.
	if (bytesExpected < channel.write(arrayWithHeaderBuffer)) {
		writeBuffers(channel, arrayWithHeaderBuffer);
	}
	return bytesExpected;
}
 
Example 2
Source File: BufferReaderWriterUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
static long writeToByteChannel(
		FileChannel channel,
		Buffer buffer,
		ByteBuffer[] arrayWithHeaderBuffer) throws IOException {

	final ByteBuffer headerBuffer = arrayWithHeaderBuffer[0];
	headerBuffer.clear();
	headerBuffer.putShort(buffer.isBuffer() ? HEADER_VALUE_IS_BUFFER : HEADER_VALUE_IS_EVENT);
	headerBuffer.putShort(buffer.isCompressed() ? BUFFER_IS_COMPRESSED : BUFFER_IS_NOT_COMPRESSED);
	headerBuffer.putInt(buffer.getSize());
	headerBuffer.flip();

	final ByteBuffer dataBuffer = buffer.getNioBufferReadable();
	arrayWithHeaderBuffer[1] = dataBuffer;

	final long bytesExpected = HEADER_LENGTH + dataBuffer.remaining();

	// The file channel implementation guarantees that all bytes are written when invoked
	// because it is a blocking channel (the implementation mentioned it as guaranteed).
	// However, the api docs leaves it somewhat open, so it seems to be an undocumented contract in the JRE.
	// We build this safety net to be on the safe side.
	if (bytesExpected < channel.write(arrayWithHeaderBuffer)) {
		writeBuffers(channel, arrayWithHeaderBuffer);
	}
	return bytesExpected;
}
 
Example 3
Source File: BufferSpiller.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a buffer or event to the sequence of spilled buffers and events.
 *
 * @param boe The buffer or event to add and spill.
 * @throws IOException Thrown, if the buffer of event could not be spilled.
 */
@Override
public void add(BufferOrEvent boe) throws IOException {
	try {
		ByteBuffer contents;
		if (boe.isBuffer()) {
			Buffer buf = boe.getBuffer();
			contents = buf.getNioBufferReadable();
		}
		else {
			contents = EventSerializer.toSerializedEvent(boe.getEvent());
		}

		headBuffer.clear();
		headBuffer.putInt(boe.getChannelIndex());
		headBuffer.putInt(contents.remaining());
		headBuffer.put((byte) (boe.isBuffer() ? 0 : 1));
		headBuffer.flip();

		bytesWritten += (headBuffer.remaining() + contents.remaining());

		FileUtils.writeCompletely(currentChannel, headBuffer);
		FileUtils.writeCompletely(currentChannel, contents);
	}
	finally {
		if (boe.isBuffer()) {
			boe.getBuffer().recycleBuffer();
		}
	}
}
 
Example 4
Source File: BufferSpiller.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void add(BufferOrEvent boe) throws IOException {
	try {
		ByteBuffer contents;
		if (boe.isBuffer()) {
			Buffer buf = boe.getBuffer();
			contents = buf.getNioBufferReadable();
		}
		else {
			contents = EventSerializer.toSerializedEvent(boe.getEvent());
		}

		headBuffer.clear();
		headBuffer.putInt(boe.getChannelIndex());
		headBuffer.putInt(contents.remaining());
		headBuffer.put((byte) (boe.isBuffer() ? 0 : 1));
		headBuffer.flip();

		bytesWritten += (headBuffer.remaining() + contents.remaining());

		FileUtils.writeCompletely(currentChannel, headBuffer);
		FileUtils.writeCompletely(currentChannel, contents);
	}
	finally {
		if (boe.isBuffer()) {
			boe.getBuffer().recycleBuffer();
		}
	}
}