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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readerIndex() . 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: NettyMessage.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: NettyMessage.java    From flink with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: NettyMessage.java    From flink with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
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 5
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example 6
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testWriteReadCharSequence(Charset charset) {
    ByteBuf buf = newBuffer(1024);
    CharBuffer sequence = CharsetUtil.US_ASCII.equals(charset)
            ? ASCII_CHARS : EXTENDED_ASCII_CHARS;
    buf.writerIndex(1);
    int bytes = buf.writeCharSequence(sequence, charset);
    buf.readerIndex(1);
    assertEquals(sequence, CharBuffer.wrap(buf.readCharSequence(bytes, charset)));
    buf.release();
}
 
Example 7
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example 8
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testWriteReadCharSequence(Charset charset) {
    ByteBuf buf = newBuffer(1024);
    CharBuffer sequence = CharsetUtil.US_ASCII.equals(charset)
            ? ASCII_CHARS : EXTENDED_ASCII_CHARS;
    buf.writerIndex(1);
    int bytes = buf.writeCharSequence(sequence, charset);
    buf.readerIndex(1);
    assertEquals(sequence, CharBuffer.wrap(buf.readCharSequence(bytes, charset)));
    buf.release();
}
 
Example 9
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example 10
Source File: BufferResponseDecoder.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DecodingResult onChannelRead(ByteBuf data) throws Exception {
	if (bufferResponse == null) {
		decodeMessageHeader(data);
	}

	if (bufferResponse != null) {
		int remainingBufferSize = bufferResponse.bufferSize - decodedDataBufferSize;
		int actualBytesToDecode = Math.min(data.readableBytes(), remainingBufferSize);

		// For the case of data buffer really exists in BufferResponse now.
		if (actualBytesToDecode > 0) {
			// For the case of released input channel, the respective data buffer part would be
			// discarded from the received buffer.
			if (bufferResponse.getBuffer() == null) {
				data.readerIndex(data.readerIndex() + actualBytesToDecode);
			} else {
				bufferResponse.getBuffer().asByteBuf().writeBytes(data, actualBytesToDecode);
			}

			decodedDataBufferSize += actualBytesToDecode;
		}

		if (decodedDataBufferSize == bufferResponse.bufferSize) {
			BufferResponse result = bufferResponse;
			clearState();
			return DecodingResult.fullMessage(result);
		}
	}

	return DecodingResult.NOT_FINISHED;
}
 
Example 11
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testWriteReadCharSequence(Charset charset) {
    ByteBuf buf = newBuffer(1024);
    CharBuffer sequence = CharsetUtil.US_ASCII.equals(charset)
            ? ASCII_CHARS : EXTENDED_ASCII_CHARS;
    buf.writerIndex(1);
    int bytes = buf.writeCharSequence(sequence, charset);
    buf.readerIndex(1);
    assertEquals(sequence, CharBuffer.wrap(buf.readCharSequence(bytes, charset)));
    buf.release();
}