Java Code Examples for io.netty.buffer.ByteBuf#markReaderIndex()

The following examples show how to use io.netty.buffer.ByteBuf#markReaderIndex() . 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: DefaultNettyDecoder.java    From PeonyFramwork with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
    try {
        int readAble = in.readableBytes();
        if (readAble < headSize) {
            return;
        }

        in.markReaderIndex();
        int size = in.readInt();
        if (readAble < (size + headSize)) {
            in.resetReaderIndex();
            return;
        }

        // FIXME wjm 最好改了
        try (ObjectInputStream oin = new ObjectInputStream(new ByteBufInputStream(in.readBytes(size), true))) {
            list.add(oin.readObject());
        }
    } catch (Throwable e) {
        logger.error("decode error ", e);
    }
}
 
Example 2
Source File: RpcDecoder.java    From litchi with Apache License 2.0 6 votes vote down vote up
@Override
public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	if (in.readableBytes() < 4) {
		return;
	}
	in.markReaderIndex();
	int dataLength = in.readInt();

	if (in.readableBytes() < dataLength) {
		in.resetReaderIndex();
		return;
	}
	byte[] data = new byte[dataLength];
	in.readBytes(data);

	Object obj = RpcConfig.getSerializer().decode(data, genericClass);
	out.add(obj);
}
 
Example 3
Source File: LegacyAbstractFromServerPacketDecoder.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> packets) throws Exception {
	if (!buf.isReadable()) {
		return;
	}
	buf.markReaderIndex();
	ReadableMiddlePacket transformer = registry.getTransformer(Protocol.GAME, buf.readUnsignedByte(), false);
	if (transformer != null) {
		transformer.read(buf);
		if (buf.isReadable()) {
			throw new DecoderException("Did not read all data from packet " + transformer.getClass().getName() + ", bytes left: " + buf.readableBytes());
		}
		packets.addAll(transformer.toNative());
	} else {
		buf.resetReaderIndex();
		packets.add(new PacketWrapper(null, buf.copy()));
	}
}
 
Example 4
Source File: ProtocolLengthDeserializer.java    From CloudNet with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    in.markReaderIndex();
    byte[] lengthBytes = new byte[3];

    for (int i = 0; i < 3; i++) {
        if (!in.isReadable()) {
            in.resetReaderIndex();
            return;
        }

        lengthBytes[i] = in.readByte();

        if (lengthBytes[i] >= 0) {
            ProtocolBuffer buffer = new ProtocolBuffer(Unpooled.wrappedBuffer(lengthBytes));

            try {
                int packetLength = buffer.readVarInt();

                if (in.readableBytes() < packetLength) {
                    in.resetReaderIndex();
                    return;
                }

                out.add(in.readBytes(packetLength));
            } finally {
                buffer.release();
            }

            return;
        }
    }
}
 
Example 5
Source File: ByteBufStreamInput.java    From crate with Apache License 2.0 5 votes vote down vote up
ByteBufStreamInput(ByteBuf buffer, int length) {
    if (length > buffer.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    this.buffer = buffer;
    int startIndex = buffer.readerIndex();
    endIndex = startIndex + length;
    buffer.markReaderIndex();
}
 
Example 6
Source File: PbrpcMessageDeserializer.java    From navi-pbrpc with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext,
 *      io.netty.buffer.ByteBuf, java.util.List)
 */
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // 解决半包问题,此时Nshead还没有接收全,channel中留存的字节流不做处理
    if (in.readableBytes() < NsHead.NSHEAD_LEN) {
        return;
    }

    in.markReaderIndex();

    byte[] bytes = new byte[NsHead.NSHEAD_LEN];
    in.readBytes(bytes, 0, NsHead.NSHEAD_LEN);

    NsHead nsHead = new NsHead();
    nsHead.wrap(bytes);

    // 解决半包问题,此时body还没有接收全,channel中留存的字节流不做处理,重置readerIndex
    if (in.readableBytes() < (int) nsHead.getBodyLen()) {
        in.resetReaderIndex();
        return;
    }

    // 此时接受到了足够的一个包,开始处理
    in.markReaderIndex();

    byte[] totalBytes = new byte[(int) nsHead.getBodyLen()];
    in.readBytes(totalBytes, 0, (int) nsHead.getBodyLen());

    PbrpcMsg decoded = PbrpcMsg.of(nsHead).setData(totalBytes);
    ContextHolder.putContext("_logid", nsHead.getLogId()); // TODO

    if (decoded != null) {
        out.add(decoded);
    }
    // LOG.info("Deser data " + nsHead.getLogId() + " is" + decoded + " and using "
    // + (System.nanoTime() - start) / 1000 + "us");
}
 
Example 7
Source File: Snappy.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a compressed reference offset and length from the supplied input
 * buffer, seeks back to the appropriate place in the input buffer and
 * writes the found data to the supplied output stream.
 *
 * @param tag The tag used to identify this as a copy is also used to encode
 *     the length and part of the offset
 * @param in The input buffer to read from
 * @param out The output buffer to write to
 * @return The number of bytes appended to the output buffer, or -1 to indicate
 *     "try again later"
 * @throws DecompressionException If the read offset is invalid
 */
private static int decodeCopyWith4ByteOffset(byte tag, ByteBuf in, ByteBuf out, int writtenSoFar) {
    if (in.readableBytes() < 4) {
        return NOT_ENOUGH_INPUT;
    }

    int initialIndex = out.writerIndex();
    int length = 1 + (tag >> 2 & 0x03F);
    int offset = ByteBufUtil.swapInt(in.readInt());

    validateOffset(offset, writtenSoFar);

    out.markReaderIndex();
    if (offset < length) {
        int copies = length / offset;
        for (; copies > 0; copies--) {
            out.readerIndex(initialIndex - offset);
            out.readBytes(out, offset);
        }
        if (length % offset != 0) {
            out.readerIndex(initialIndex - offset);
            out.readBytes(out, length % offset);
        }
    } else {
        out.readerIndex(initialIndex - offset);
        out.readBytes(out, length);
    }
    out.resetReaderIndex();

    return length;
}
 
Example 8
Source File: AmqpDecoder.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private void parseFrame(ByteBuf buffer, List<Object> out) throws Exception {
    buffer.markReaderIndex();
    if (buffer.readableBytes() > FRAME_SIZE_WITHOUT_PAYLOAD) {
        byte type = buffer.readByte();
        int channel = buffer.readShort();
        long payloadSize = buffer.readInt();

        long estimatedRemainingSize = payloadSize + 1;
        if (buffer.readableBytes() < estimatedRemainingSize) {
            buffer.resetReaderIndex();
            return;
        }

        GeneralFrame frame = null;
        switch (type) {
            case 1: // Method
                short amqpClass = buffer.readShort();
                short amqpMethod = buffer.readShort();
                AmqMethodBodyFactory factory = methodRegistry.getFactory(amqpClass, amqpMethod);

                frame = factory.newInstance(buffer, channel, payloadSize);
                break;
            case 2: // Header
                frame = HeaderFrame.parse(buffer, channel);
                break;
            case 3: // Body
                frame = ContentFrame.parse(buffer, channel, payloadSize);
                break;
            case 4: // Heartbeat
                throw new Exception("Method Not implemented");
        }

        byte frameEnd = buffer.readByte();
        if (frameEnd != (byte) GeneralFrame.FRAME_END) {
            throw new Exception("Invalid AMQP frame");
        }

        out.add(frame);
    }
}
 
Example 9
Source File: ErrorFrameCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
public static ByteBuf data(ByteBuf byteBuf) {
  byteBuf.markReaderIndex();
  byteBuf.skipBytes(FrameHeaderCodec.size() + Integer.BYTES);
  ByteBuf slice = byteBuf.slice();
  byteBuf.resetReaderIndex();
  return slice;
}
 
Example 10
Source File: RpcNettyDecoder.java    From hasting with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
		List<Object> out) throws Exception {
	int readableBytes = in.readableBytes();
	if(readableBytes>=NioUtils.RPC_PROTOCOL_HEAD_LEN){
		in.markReaderIndex();
		int type = in.readInt();//type
		long threadId = in.readLong();//threadId
		int index = in.readInt();//index
		int length = in.readInt();// data length
		if(length>RpcUtils.MEM_1M){
			throw new RpcException("rpc data too long "+ length);
		}
		if(in.readableBytes()>=length){
			byte[] buf = new byte[length];
			if(length>0){
				in.readBytes(buf);
			}
			RpcObject rpc = new RpcObject();
			rpc.setType(RpcType.getByType(type));
			rpc.setThreadId(threadId);
			rpc.setIndex(index);
			rpc.setLength(length);
			rpc.setData(buf);
			out.add(rpc);
		}else{
			in.resetReaderIndex();
		}
	}
}
 
Example 11
Source File: ReplicationPacketDecoder.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // do we have length field in buffer ?
    if (!in.isReadable(4)) {
        return;
    }

    // do we have entire packet in the buffer?
    
    in.markReaderIndex();
    int size = BufferUtils.readLongInt(in) + 1;
    if (size == (0x00ffffff+1)) {
        return;
    }
    if (!in.isReadable(size)) {
        in.resetReaderIndex();
        return;
    }

    int pos = in.readerIndex(); 
    try {
    	ReplicationPacket packet = readPacket(in, size);
        out.add(packet);
    }
    finally {
        int currentPos = in.readerIndex();
        in.skipBytes(size - (currentPos - pos));
    }
}
 
Example 12
Source File: GenericFrameCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
static ByteBuf dataWithRequestN(ByteBuf byteBuf) {
  boolean hasMetadata = FrameHeaderCodec.hasMetadata(byteBuf);
  byteBuf.markReaderIndex();
  byteBuf.skipBytes(FrameHeaderCodec.size() + Integer.BYTES);
  ByteBuf data = FrameBodyCodec.dataWithoutMarking(byteBuf, hasMetadata);
  byteBuf.resetReaderIndex();
  return data;
}
 
Example 13
Source File: ErrorFrameCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
public static int errorCode(ByteBuf byteBuf) {
  byteBuf.markReaderIndex();
  byteBuf.skipBytes(FrameHeaderCodec.size());
  int i = byteBuf.readInt();
  byteBuf.resetReaderIndex();
  return i;
}
 
Example 14
Source File: AppMessageDecoder.java    From cim with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {
	
	/*
	 * 消息体不足3位,发生断包情况
	 */
	if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
		return;
	}

	buffer.markReaderIndex();

	byte type = buffer.readByte();

	byte lv = buffer.readByte();
	byte hv = buffer.readByte();
	int length = getContentLength(lv, hv);

	/*
	 * 发生断包情况,等待接收完成
	 */
	if (buffer.readableBytes() < length) {
		buffer.resetReaderIndex();
		return;
	}
	 
	byte[] dataBytes = new byte[length];
	buffer.readBytes(dataBytes);


	Object message = mappingMessageObject(dataBytes, type);
	
	queue.add(message);
}
 
Example 15
Source File: AuthMetadataFlyweightTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
void shouldEncodeBearerMetadata() {
  String testToken = TEST_BEARER_TOKEN;

  ByteBuf byteBuf =
      AuthMetadataFlyweight.encodeBearerMetadata(
          ByteBufAllocator.DEFAULT, testToken.toCharArray());

  byteBuf.markReaderIndex();
  checkBearerAuthMetadataEncoding(testToken, byteBuf);
  byteBuf.resetReaderIndex();
  checkBearerAuthMetadataEncodingUsingDecoders(testToken, byteBuf);
}
 
Example 16
Source File: NumberUtilsTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
void encodeUnsignedMedium() {
  ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
  NumberUtils.encodeUnsignedMedium(buffer, 129);
  buffer.markReaderIndex();

  assertThat(buffer.readUnsignedMedium()).as("reading as unsigned medium").isEqualTo(129);

  buffer.resetReaderIndex();
  assertThat(buffer.readMedium()).as("reading as signed medium").isEqualTo(129);
}
 
Example 17
Source File: TracingMetadataCodec.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
public static TracingMetadata decode(ByteBuf byteBuf) {
  byteBuf.markReaderIndex();
  try {
    byte flags = byteBuf.readByte();
    boolean isNotSampled = (flags & FLAG_NOT_SAMPLED) == FLAG_NOT_SAMPLED;
    boolean isSampled = (flags & FLAG_SAMPLED) == FLAG_SAMPLED;
    boolean isDebug = (flags & FLAG_DEBUG) == FLAG_DEBUG;
    boolean isIDSet = (flags & FLAG_IDS_SET) == FLAG_IDS_SET;

    if (!isIDSet) {
      return new TracingMetadata(0, 0, 0, false, 0, true, isNotSampled, isSampled, isDebug);
    }

    boolean extendedTraceId =
        (flags & FLAG_EXTENDED_TRACE_ID_SIZE) == FLAG_EXTENDED_TRACE_ID_SIZE;

    long traceIdHigh;
    if (extendedTraceId) {
      traceIdHigh = byteBuf.readLong();
    } else {
      traceIdHigh = 0;
    }

    long traceId = byteBuf.readLong();
    long spanId = byteBuf.readLong();

    boolean includesParent = (flags & FLAG_INCLUDE_PARENT_ID) == FLAG_INCLUDE_PARENT_ID;

    long parentId;
    if (includesParent) {
      parentId = byteBuf.readLong();
    } else {
      parentId = 0;
    }

    return new TracingMetadata(
        traceIdHigh,
        traceId,
        spanId,
        includesParent,
        parentId,
        false,
        isNotSampled,
        isSampled,
        isDebug);
  } finally {
    byteBuf.resetReaderIndex();
  }
}
 
Example 18
Source File: ProtobufVarint32FrameDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Reads variable length 32bit int from buffer 从缓冲区读取可变长度32位整型
 *
 * @return decoded int if buffers readerIndex has been forwarded else nonsense value
 */
private static int readRawVarint32(ByteBuf buffer) {
    if (!buffer.isReadable()) {
        return 0;
    }
    buffer.markReaderIndex();
    byte tmp = buffer.readByte();
    if (tmp >= 0) {
        return tmp;
    } else {
        int result = tmp & 127;
        if (!buffer.isReadable()) {
            buffer.resetReaderIndex();
            return 0;
        }
        if ((tmp = buffer.readByte()) >= 0) {
            result |= tmp << 7;
        } else {
            result |= (tmp & 127) << 7;
            if (!buffer.isReadable()) {
                buffer.resetReaderIndex();
                return 0;
            }
            if ((tmp = buffer.readByte()) >= 0) {
                result |= tmp << 14;
            } else {
                result |= (tmp & 127) << 14;
                if (!buffer.isReadable()) {
                    buffer.resetReaderIndex();
                    return 0;
                }
                if ((tmp = buffer.readByte()) >= 0) {
                    result |= tmp << 21;
                } else {
                    result |= (tmp & 127) << 21;
                    if (!buffer.isReadable()) {
                        buffer.resetReaderIndex();
                        return 0;
                    }
                    result |= (tmp = buffer.readByte()) << 28;
                    if (tmp < 0) {
                        throw new CorruptedFrameException("malformed varint.");
                    }
                }
            }
        }
        return result;
    }
}
 
Example 19
Source File: SourcePacketBuilder.java    From async-gamequery-lib with MIT License 4 votes vote down vote up
@Override
public <T extends SourceServerPacket> T construct(ByteBuf data) {
    //Mark Index
    data.markReaderIndex();

    try {
        //Reset the index
        data.readerIndex(0);

        //Verify size
        if (data.readableBytes() < 5)
            throw new IllegalStateException("Cannot continue processing buffer with less than or equal to 4 bytes");

        //Read protocol header
        int protocolHeader = data.readIntLE();

        //Check if this is a split packet
        if (protocolHeader == 0xFFFFFFFE)
            throw new IllegalStateException("Cannot construct a response from a partial/split packet.");

        //Verify that we have a valid header
        if (protocolHeader != 0xFFFFFFFF)
            throw new IllegalStateException("Protocol header not supported.");

        //Read packet header
        byte packetHeader = data.readByte();

        //Read payload
        byte[] payload = new byte[data.readableBytes()];
        data.readBytes(payload);

        //Verify if packet header is valid
        SourceServerPacket packet = createResponsePacketFromHeader(packetHeader);

        //If packet is empty, means the supplied packet header is not supported
        if (packet == null)
            return null;

        packet.setProtocolHeader(ByteUtils.byteArrayFromInteger(protocolHeader));
        packet.setHeader(packetHeader);
        packet.setPayload(payload);

        return (T) packet;
    } finally {
        data.resetReaderIndex();
    }
}
 
Example 20
Source File: AbstractMessageDecoder.java    From atomix with Apache License 2.0 4 votes vote down vote up
static int readIntSlow(ByteBuf buffer) {
  if (buffer.readableBytes() == 0) {
    throw ESCAPE;
  }
  buffer.markReaderIndex();
  int b = buffer.readByte();
  int result = b & 0x7F;
  if ((b & 0x80) != 0) {
    if (buffer.readableBytes() == 0) {
      buffer.resetReaderIndex();
      throw ESCAPE;
    }
    b = buffer.readByte();
    result |= (b & 0x7F) << 7;
    if ((b & 0x80) != 0) {
      if (buffer.readableBytes() == 0) {
        buffer.resetReaderIndex();
        throw ESCAPE;
      }
      b = buffer.readByte();
      result |= (b & 0x7F) << 14;
      if ((b & 0x80) != 0) {
        if (buffer.readableBytes() == 0) {
          buffer.resetReaderIndex();
          throw ESCAPE;
        }
        b = buffer.readByte();
        result |= (b & 0x7F) << 21;
        if ((b & 0x80) != 0) {
          if (buffer.readableBytes() == 0) {
            buffer.resetReaderIndex();
            throw ESCAPE;
          }
          b = buffer.readByte();
          result |= (b & 0x7F) << 28;
        }
      }
    }
  }
  return result;
}