List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:org.apache.drill.exec.rpc.ProtobufLengthDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!ctx.channel().isOpen()) { if (in.readableBytes() > 0) { logger.info("Channel is closed, discarding remaining {} byte(s) in buffer.", in.readableBytes()); }/* ww w .j a v a2 s. c om*/ in.skipBytes(in.readableBytes()); return; } in.markReaderIndex(); final byte[] buf = new byte[5]; for (int i = 0; i < buf.length; i++) { if (!in.isReadable()) { in.resetReaderIndex(); return; } buf[i] = in.readByte(); if (buf[i] >= 0) { int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32(); if (length < 0) { throw new CorruptedFrameException("negative length: " + length); } if (length == 0) { throw new CorruptedFrameException("Received a message of length 0."); } if (in.readableBytes() < length) { in.resetReaderIndex(); return; } else { // need to make buffer copy, otherwise netty will try to refill this buffer if we move the readerIndex forward... // TODO: Can we avoid this copy? ByteBuf outBuf; try { outBuf = allocator.buffer(length); } catch (OutOfMemoryException e) { logger.warn( "Failure allocating buffer on incoming stream due to memory limits. Current Allocation: {}.", allocator.getAllocatedMemory()); in.resetReaderIndex(); outOfMemoryHandler.handle(); return; } outBuf.writeBytes(in, in.readerIndex(), length); in.skipBytes(length); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug(String.format( "ReaderIndex is %d after length header of %d bytes and frame body of length %d bytes.", in.readerIndex(), i + 1, length)); } out.add(outBuf); return; } } } // Couldn't find the byte whose MSB is off. throw new CorruptedFrameException("length wider than 32-bit"); }
From source file:org.apache.drill.exec.rpc.ZeroCopyProtobufLengthDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!ctx.channel().isOpen()) { if (in.readableBytes() > 0) logger.info("Channel is closed, discarding remaining {} byte(s) in buffer.", in.readableBytes()); in.skipBytes(in.readableBytes()); return;/*w ww. ja v a 2 s .com*/ } in.markReaderIndex(); final byte[] buf = new byte[5]; for (int i = 0; i < buf.length; i++) { if (!in.isReadable()) { in.resetReaderIndex(); return; } buf[i] = in.readByte(); if (buf[i] >= 0) { int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32(); if (length < 0) { throw new CorruptedFrameException("negative length: " + length); } if (length == 0) { throw new CorruptedFrameException("Received a message of length 0."); } if (in.readableBytes() < length) { in.resetReaderIndex(); return; } else { // need to make buffer copy, otherwise netty will try to refill this buffer if we move the readerIndex forward... // TODO: Can we avoid this copy? ByteBuf outBuf = in.copy(in.readerIndex(), length); in.skipBytes(length); if (RpcConstants.EXTRA_DEBUGGING) logger.debug(String.format( "ReaderIndex is %d after length header of %d bytes and frame body of length %d bytes.", in.readerIndex(), i + 1, length)); out.add(outBuf); return; } } } // Couldn't find the byte whose MSB is off. throw new CorruptedFrameException("length wider than 32-bit"); }
From source file:org.apache.hive.spark.client.rpc.KryoMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;//from ww w. j av a 2 s. co m } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer)); Object msg = kryos.get().readClassAndObject(kryoIn); LOG.debug("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } finally { in.skipBytes(msgSize); } }
From source file:org.apache.pulsar.common.api.PulsarDecoder.java
License:Apache License
@Override final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // Get a buffer that contains the full frame ByteBuf buffer = (ByteBuf) msg; BaseCommand cmd = null;/*from ww w. j a v a 2 s .c o m*/ BaseCommand.Builder cmdBuilder = null; try { // De-serialize the command int cmdSize = (int) buffer.readUnsignedInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer); cmdBuilder = BaseCommand.newBuilder(); cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); buffer.writerIndex(writerIndex); cmdInputStream.recycle(); if (log.isDebugEnabled()) { log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType()); } messageReceived(); switch (cmd.getType()) { case PARTITIONED_METADATA: checkArgument(cmd.hasPartitionMetadata()); handlePartitionMetadataRequest(cmd.getPartitionMetadata()); cmd.getPartitionMetadata().recycle(); break; case PARTITIONED_METADATA_RESPONSE: checkArgument(cmd.hasPartitionMetadataResponse()); handlePartitionResponse(cmd.getPartitionMetadataResponse()); cmd.getPartitionMetadataResponse().recycle(); break; case LOOKUP: checkArgument(cmd.hasLookupTopic()); handleLookup(cmd.getLookupTopic()); cmd.getLookupTopic().recycle(); break; case LOOKUP_RESPONSE: checkArgument(cmd.hasLookupTopicResponse()); handleLookupResponse(cmd.getLookupTopicResponse()); cmd.getLookupTopicResponse().recycle(); break; case ACK: checkArgument(cmd.hasAck()); handleAck(cmd.getAck()); cmd.getAck().getMessageId().recycle(); cmd.getAck().recycle(); break; case CLOSE_CONSUMER: checkArgument(cmd.hasCloseConsumer()); handleCloseConsumer(cmd.getCloseConsumer()); cmd.getCloseConsumer().recycle(); break; case CLOSE_PRODUCER: checkArgument(cmd.hasCloseProducer()); handleCloseProducer(cmd.getCloseProducer()); cmd.getCloseProducer().recycle(); break; case CONNECT: checkArgument(cmd.hasConnect()); handleConnect(cmd.getConnect()); cmd.getConnect().recycle(); break; case CONNECTED: checkArgument(cmd.hasConnected()); handleConnected(cmd.getConnected()); cmd.getConnected().recycle(); break; case ERROR: checkArgument(cmd.hasError()); handleError(cmd.getError()); cmd.getError().recycle(); break; case FLOW: checkArgument(cmd.hasFlow()); handleFlow(cmd.getFlow()); cmd.getFlow().recycle(); break; case MESSAGE: { checkArgument(cmd.hasMessage()); handleMessage(cmd.getMessage(), buffer); cmd.getMessage().recycle(); break; } case PRODUCER: checkArgument(cmd.hasProducer()); handleProducer(cmd.getProducer()); cmd.getProducer().recycle(); break; case SEND: { checkArgument(cmd.hasSend()); // Store a buffer marking the content + headers ByteBuf headersAndPayload = buffer.markReaderIndex(); handleSend(cmd.getSend(), headersAndPayload); cmd.getSend().recycle(); break; } case SEND_ERROR: checkArgument(cmd.hasSendError()); handleSendError(cmd.getSendError()); cmd.getSendError().recycle(); break; case SEND_RECEIPT: checkArgument(cmd.hasSendReceipt()); handleSendReceipt(cmd.getSendReceipt()); cmd.getSendReceipt().recycle(); break; case SUBSCRIBE: checkArgument(cmd.hasSubscribe()); handleSubscribe(cmd.getSubscribe()); cmd.getSubscribe().recycle(); break; case SUCCESS: checkArgument(cmd.hasSuccess()); handleSuccess(cmd.getSuccess()); cmd.getSuccess().recycle(); break; case PRODUCER_SUCCESS: checkArgument(cmd.hasProducerSuccess()); handleProducerSuccess(cmd.getProducerSuccess()); cmd.getProducerSuccess().recycle(); break; case UNSUBSCRIBE: checkArgument(cmd.hasUnsubscribe()); handleUnsubscribe(cmd.getUnsubscribe()); cmd.getUnsubscribe().recycle(); break; case PING: checkArgument(cmd.hasPing()); handlePing(cmd.getPing()); cmd.getPing().recycle(); break; case PONG: checkArgument(cmd.hasPong()); handlePong(cmd.getPong()); cmd.getPong().recycle(); break; case REDELIVER_UNACKNOWLEDGED_MESSAGES: checkArgument(cmd.hasRedeliverUnacknowledgedMessages()); handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages()); cmd.getRedeliverUnacknowledgedMessages().recycle(); break; case CONSUMER_STATS: checkArgument(cmd.hasConsumerStats()); handleConsumerStats(cmd.getConsumerStats()); cmd.getConsumerStats().recycle(); break; case CONSUMER_STATS_RESPONSE: checkArgument(cmd.hasConsumerStatsResponse()); handleConsumerStatsResponse(cmd.getConsumerStatsResponse()); cmd.getConsumerStatsResponse().recycle(); break; case REACHED_END_OF_TOPIC: checkArgument(cmd.hasReachedEndOfTopic()); handleReachedEndOfTopic(cmd.getReachedEndOfTopic()); cmd.getReachedEndOfTopic().recycle(); break; } } finally { if (cmdBuilder != null) { cmdBuilder.recycle(); } if (cmd != null) { cmd.recycle(); } buffer.release(); } }
From source file:org.apache.spark.sql.hive.thriftserver.rsc.KryoMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;//from ww w . j ava2s . c o m } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Object msg = serializer.deserialize(nioBuffer); LOG.info("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } catch (Exception e) { Throwable throwable = e; while (throwable != null) { LOG.info("tlitest cause: " + throwable.getCause()); LOG.info("tlitest message: " + throwable.getMessage()); StringBuilder builder = new StringBuilder(); for (StackTraceElement elem : throwable.getStackTrace()) { builder.append(elem); builder.append("\n"); } LOG.info(builder.toString()); throwable = throwable.getCause(); } throw e; } finally { in.skipBytes(msgSize); } }
From source file:org.beaconmc.network.socket.pipeline.PacketFraming.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (!readableVarInt(in)) { return;/*from w w w . jav a2s . co m*/ } int length = readVarInt(in); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } ByteBuf buf = ctx.alloc().buffer(length); in.readBytes(buf, length); out.add(buf); }
From source file:org.beaconmc.network.socket.pipeline.PacketLegacy.java
License:Open Source License
@Override protected void messageReceived(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { byteBuf.markReaderIndex(); boolean repliedPing = true; try {/*from ww w .ja va 2s .c o m*/ short packetId = byteBuf.readUnsignedByte(); if (packetId == 254) { int length = byteBuf.readableBytes(); AsyncServerPingEvent asyncServerPingEvent = EventFactory .callAsyncServerPingEvent(this.server.createServerPing()); if (asyncServerPingEvent.getPing() != null) { switch (length) { case 0: this.sendPingAndClose(channelHandlerContext, this.toArray(String.format("%s%d%d", asyncServerPingEvent.getPing().getDescription().getText(), asyncServerPingEvent.getPing().getPlayers().getOnline(), asyncServerPingEvent.getPing().getPlayers().getMax()))); break; case 1: if (byteBuf.readUnsignedByte() != 1) { return; } this.sendPingAndClose(channelHandlerContext, this.toArray(String.format("1\0%d\0%s\0%s\0%d\0%d", 127, asyncServerPingEvent.getPing().getVersion().getName(), asyncServerPingEvent.getPing().getDescription().getText(), asyncServerPingEvent.getPing().getPlayers().getOnline(), asyncServerPingEvent.getPing().getPlayers().getMax()))); break; default: boolean checkFlag = byteBuf.readUnsignedByte() == 1; checkFlag &= byteBuf.readUnsignedByte() == 250; checkFlag &= "MC|PingHost".equals( new String(byteBuf.readBytes(byteBuf.readShort() * 2).array(), Charsets.UTF_16)); int checkShort = byteBuf.readShort(); checkFlag &= byteBuf.readUnsignedByte() >= 73; checkFlag &= 3 + byteBuf.readBytes(byteBuf.readShort() * 2).array().length + 4 == checkShort; checkFlag &= byteBuf.readInt() <= '\uffff'; checkFlag &= byteBuf.readableBytes() == 0; if (!checkFlag) { return; } this.sendPingAndClose(channelHandlerContext, this.toArray(String.format("1\0%d\0%s\0%s\0%d\0%d", 127, asyncServerPingEvent.getPing().getVersion().getName(), asyncServerPingEvent.getPing().getDescription().getText(), asyncServerPingEvent.getPing().getPlayers().getOnline(), asyncServerPingEvent.getPing().getPlayers().getMax()))); break; } } else { this.close(channelHandlerContext); } repliedPing = false; } else if (packetId == 0x02 && byteBuf.isReadable()) { this.sendPingAndClose(channelHandlerContext, this.toArray(ChatColor.RED + "Outdated Client")); repliedPing = false; } } catch (Exception e) { return; } finally { if (repliedPing) { byteBuf.resetReaderIndex(); channelHandlerContext.channel().pipeline().remove("legacy_ping"); channelHandlerContext.fireChannelRead(byteBuf.retain()); } } }
From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java
License:Apache License
public static Capability decodeCapability(ByteBuf buffer) { Capability cap = null;//from w ww. j av a 2 s . c o m try { buffer.markReaderIndex(); int type = buffer.readUnsignedByte(); switch (type) { case BGPv4Constants.BGP_CAPABILITY_TYPE_MULTIPROTOCOL: cap = decodeMultiProtocolCapability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_ROUTE_REFRESH: cap = decodeRouteRefreshCapability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_AS4_NUMBERS: cap = decodeAutonomousSystem4Capability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_OUTBOUND_ROUTE_FILTERING: cap = decodeOutboundRouteFilteringCapability(buffer); break; default: cap = decodeUnknownCapability(type, buffer); break; } } catch (CapabilityException e) { buffer.resetReaderIndex(); int type = buffer.readUnsignedByte(); int capLength = buffer.readUnsignedByte(); byte[] capPacket = new byte[capLength + 2]; buffer.readBytes(capPacket, 2, capLength); capPacket[0] = (byte) type; capPacket[1] = (byte) capLength; e.setCapability(capPacket); throw e; } return cap; }
From source file:org.bgp4j.netty.protocol.update.UpdatePacketDecoder.java
License:Apache License
private List<PathAttribute> decodePathAttributes(ByteBuf buffer) { List<PathAttribute> attributes = new LinkedList<PathAttribute>(); while (buffer.isReadable()) { buffer.markReaderIndex(); try {/*from www.ja v a2s . c o m*/ int flagsType = buffer.readUnsignedShort(); boolean optional = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_OPTIONAL_BIT) != 0); boolean transitive = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TRANSITIVE_BIT) != 0); boolean partial = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_PARTIAL_BIT) != 0); int typeCode = (flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MASK); int valueLength = 0; if ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_EXTENDED_LENGTH_BIT) != 0) valueLength = buffer.readUnsignedShort(); else valueLength = buffer.readUnsignedByte(); ByteBuf valueBuffer = buffer.readSlice(valueLength); PathAttribute attr = null; switch (typeCode) { case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AGGREGATOR: attr = decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_AGGREGATOR: attr = decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_PATH: attr = decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS_PATH: attr = decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ATOMIC_AGGREGATE: attr = decodeAtomicAggregatePathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_COMMUNITIES: attr = decodeCommunityPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_LOCAL_PREF: attr = decodeLocalPrefPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MULTI_EXIT_DISC: attr = decodeMultiExitDiscPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_NEXT_HOP: attr = decodeNextHopPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGIN: attr = decodeOriginPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_REACH_NLRI: attr = decodeMpReachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_UNREACH_NLRI: attr = decodeMpUnreachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGINATOR_ID: attr = decodeOriginatorIDPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_CLUSTER_LIST: attr = decodeClusterListPathAttribute(valueBuffer); break; default: { byte[] value = new byte[valueBuffer.readableBytes()]; valueBuffer.readBytes(value); attr = new UnknownPathAttribute(typeCode, value); } break; } attr.setOptional(optional); attr.setTransitive(transitive); attr.setPartial(partial); attributes.add(attr); } catch (AttributeException ex) { int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); int attributeLength = endReadIndex - buffer.readerIndex(); byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); ex.setOffendingAttribute(packet); throw ex; } catch (IndexOutOfBoundsException ex) { int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); int attributeLength = endReadIndex - buffer.readerIndex(); byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); throw new AttributeLengthException(packet); } } return attributes; }
From source file:org.diorite.impl.connection.packets.PacketSizer.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext context, final ByteBuf byteBuf, final List<Object> objects) { byteBuf.markReaderIndex(); final byte[] arrayOfByte = new byte[3]; for (int i = 0; i < arrayOfByte.length; i++) { if (!byteBuf.isReadable()) { byteBuf.resetReaderIndex();// w w w. jav a 2 s .c o m return; } arrayOfByte[i] = byteBuf.readByte(); if (arrayOfByte[i] >= 0) { final PacketDataSerializer dataSerializer = new PacketDataSerializer( Unpooled.wrappedBuffer(arrayOfByte)); try { final int size = dataSerializer.readVarInt(); if (byteBuf.readableBytes() < size) { byteBuf.resetReaderIndex(); return; } objects.add(byteBuf.readBytes(size)); return; } finally { dataSerializer.release(); } } } throw new CorruptedFrameException("length wider than 21-bit"); }