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

The following examples show how to use 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: ReceivedDelayMessageProcessor.java    From qmq with Apache License 2.0 6 votes vote down vote up
private RawMessageExtend doDeserializeRawMessagesExtend(ByteBuf body) {
    body.markReaderIndex();
    int headerStart = body.readerIndex();
    long bodyCrc = body.readLong();
    MessageHeader header = deserializeMessageHeader(body);
    header.setBodyCrc(bodyCrc);
    int bodyLen = body.readInt();
    int headerLen = body.readerIndex() - headerStart;
    int totalLen = headerLen + bodyLen;

    body.resetReaderIndex();
    ByteBuf messageBuf = body.readSlice(totalLen);
    // client config error,prefer to send after ten second
    long scheduleTime = System.currentTimeMillis() + 10000;
    if (Flags.isDelay(header.getFlag())) {
        scheduleTime = header.getExpireTime();
    }

    return new RawMessageExtend(header, messageBuf, messageBuf.readableBytes(), scheduleTime);
}
 
Example 2
Source File: Decoder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected final <T> T readContent(@Nonnull ByteBuf input, @Nonnull ChannelHandlerContext context, int contentLength, @Nonnull FullMessageConsumer<T> fullMessageConsumer) throws IOException {
  ByteBuf buffer = getBufferIfSufficient(input, contentLength, context);
  if (buffer == null) {
    return null;
  }

  boolean isCumulateBuffer = buffer != input;
  int oldReaderIndex = input.readerIndex();
  try {
    return fullMessageConsumer.contentReceived(buffer, context, isCumulateBuffer);
  }
  finally {
    if (isCumulateBuffer) {
      // cumulation buffer - release it
      buffer.release();
    }
    else {
      buffer.readerIndex(oldReaderIndex + contentLength);
    }
  }
}
 
Example 3
Source File: HAProxyMessageDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the index in the buffer of the end of header if found.
 * Returns -1 if no end of header was found in the buffer.
 */
private static int findEndOfHeader(final ByteBuf buffer) {
    final int n = buffer.readableBytes();

    // per spec, the 15th and 16th bytes contain the address length in bytes
    if (n < 16) {
        return -1;
    }

    int offset = buffer.readerIndex() + 14;

    // the total header length will be a fixed 16 byte sequence + the dynamic address information block
    int totalHeaderBytes = 16 + buffer.getUnsignedShort(offset);

    // ensure we actually have the full header available
    if (n >= totalHeaderBytes) {
        return totalHeaderBytes;
    } else {
        return -1;
    }
}
 
Example 4
Source File: Hash.java    From redisson with Apache License 2.0 6 votes vote down vote up
protected static HighwayHash calcHash(ByteBuf objectState) {
    HighwayHash h = new HighwayHash(KEY);
    int i;
    int length = objectState.readableBytes();
    int offset = objectState.readerIndex();
    byte[] data = new byte[32];
    for (i = 0; i + 32 <= length; i += 32) {
        objectState.getBytes(offset  + i, data);
        h.updatePacket(data, 0);
    }
    if ((length & 31) != 0) {
        data = new byte[length & 31];
        objectState.getBytes(offset  + i, data);
        h.updateRemainder(data, 0, length & 31);
    }
    return h;
}
 
Example 5
Source File: MultipleServicePacketService.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
private ByteBuf[] decode(ByteBuf buffer) {
    int dataStartIndex = buffer.readerIndex();
    int serviceCount = buffer.readUnsignedShort();

    int[] offsets = new int[serviceCount];
    for (int i = 0; i < serviceCount; i++) {
        offsets[i] = buffer.readUnsignedShort();
    }

    ByteBuf[] serviceData = new ByteBuf[serviceCount];
    for (int i = 0; i < serviceCount; i++) {
        int offset = offsets[i];

        int length = (i + 1 < serviceCount) ?
            offsets[i + 1] - offset :
            buffer.readableBytes();

        serviceData[i] = buffer.slice(dataStartIndex + offsets[i], length).retain();

        buffer.skipBytes(length);
    }

    return serviceData;
}
 
Example 6
Source File: DapDecoder.java    From karate with MIT License 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (remaining > 0 && in.readableBytes() >= remaining) {
        out.add(encode(in, remaining));
        remaining = 0;
    }
    int pos;
    while ((pos = findCrLfCrLf(in)) != -1) {
        int delimiterPos = pos;
        while (in.getByte(--pos) != ':') {
            // skip backwards
        }
        in.readerIndex(++pos);
        CharSequence lengthString = in.readCharSequence(delimiterPos - pos, FileUtils.UTF8);
        int length = Integer.valueOf(lengthString.toString().trim());
        in.readerIndex(delimiterPos + 4);
        if (in.readableBytes() >= length) {
            out.add(encode(in, length));
            remaining = 0;
        } else {
            remaining = length;
        }
    }
}
 
Example 7
Source File: ArmeriaClientCall.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static HttpHeaders parseGrpcWebTrailers(ByteBuf buf) {
    final HttpHeadersBuilder trailers = HttpHeaders.builder();
    while (buf.readableBytes() > 0) {
        int start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE);
        if (start == -1) {
            return null;
        }
        int endExclusive;
        if (buf.getByte(start) == ':') {
            // We need to skip the pseudoheader colon when searching for the separator.
            buf.skipBytes(1);
            endExclusive = buf.forEachByte(FIND_COLON);
            buf.readerIndex(start);
        } else {
            endExclusive = buf.forEachByte(FIND_COLON);
        }
        if (endExclusive == -1) {
            return null;
        }
        final CharSequence name = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8);
        buf.readerIndex(endExclusive + 1);
        start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE);
        buf.readerIndex(start);
        endExclusive = buf.forEachByte(ByteProcessor.FIND_CRLF);
        final CharSequence value = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8);
        trailers.add(name, value.toString());
        start = buf.forEachByte(ByteProcessor.FIND_NON_CRLF);
        if (start != -1) {
            buf.readerIndex(start);
        } else {
            // Nothing but CRLF remaining, we're done.
            buf.skipBytes(buf.readableBytes());
        }
    }
    return trailers.build();
}
 
Example 8
Source File: FlexBase64.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private void drain(ByteBuf source, int b, int state, int last) {
    while (b != DONE && source.isReadable()) {
        try {
            b = nextByte(source, state, last, true);
        } catch (IOException e) {
            b = 0;
        }

        if (b == MARK) {
            last = MARK;
            continue;
        }

        // Not WS/pad
        if ((b & 0xF000) == 0) {
            source.readerIndex(source.readerIndex() - 1);
            break;
        }
    }

    if (b == DONE) {
        // SKIP one line of trailing whitespace
        while (source.isReadable()) {
            b = source.readByte();
            if (b == '\n') {
                break;
            } else if (b != ' ' && b != '\t' && b != '\r') {
                source.readerIndex(source.readerIndex() - 1);
                break;
            }

        }
    }
}
 
Example 9
Source File: HpackDecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = Http2Exception.class)
public void testDecodeULE128LongOverflow1() throws Http2Exception {
    byte[] input = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                    (byte) 0xFF, (byte) 0xFF};
    ByteBuf in = Unpooled.wrappedBuffer(input);
    final int readerIndex = in.readerIndex();
    try {
        decodeULE128(in, 0L);
    } finally {
        assertEquals(readerIndex, in.readerIndex());
        in.release();
    }
}
 
Example 10
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static boolean skipControlCharacters(ByteBuf buffer) {
    boolean skiped = false;
    final int wIdx = buffer.writerIndex();
    int rIdx = buffer.readerIndex();
    while (wIdx > rIdx) {
        int c = buffer.getUnsignedByte(rIdx++);
        if (!Character.isISOControl(c) && !Character.isWhitespace(c)) {
            rIdx--;
            skiped = true;
            break;
        }
    }
    buffer.readerIndex(rIdx);
    return skiped;
}
 
Example 11
Source File: Base64.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public static ByteBuf decode(ByteBuf src, Base64Dialect dialect) {
    if (src == null) {
        throw new NullPointerException("src");
    }

    ByteBuf dest = decode(src, src.readerIndex(), src.readableBytes(), dialect);
    src.readerIndex(src.writerIndex());
    return dest;
}
 
Example 12
Source File: AbstractByteBufPool.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a pooled entry if possible, a new one otherwise.
 * <p>
 * The {@code byteBuf}'s {@link ByteBuf#readerIndex()} is incremented by {@code length} after it.
 */
public final T getOrCreate(final ByteBuf byteBuf) {
   final int length = byteBuf.readInt();
   if (!canPool(byteBuf, length)) {
      return create(byteBuf, length);
   } else {
      if (!byteBuf.isReadable(length)) {
         throw new IndexOutOfBoundsException();
      }
      final int bytesOffset = byteBuf.readerIndex();
      final int hashCode = hashCode(byteBuf, bytesOffset, length);
      //fast % operation with power of 2 entries.length
      final int firstIndex = hashCode & mask;
      final T firstEntry = entries[firstIndex];
      if (isEqual(firstEntry, byteBuf, bytesOffset, length)) {
         byteBuf.skipBytes(length);
         return firstEntry;
      }
      final int secondIndex = (hashCode >> shift) & mask;
      final T secondEntry = entries[secondIndex];
      if (isEqual(secondEntry, byteBuf, bytesOffset, length)) {
         byteBuf.skipBytes(length);
         return secondEntry;
      }
      final T internedEntry = create(byteBuf, length);
      final int entryIndex = firstEntry == null ? firstIndex : secondIndex;
      entries[entryIndex] = internedEntry;
      return internedEntry;
   }
}
 
Example 13
Source File: JT808Util.java    From jt808-netty with MIT License 5 votes vote down vote up
/**
 * 根据byteBuf的readerIndex和writerIndex计算校验码
 * 校验码规则:从消息头开始,同后一字节异或,直到校验码前一个字节,占用 1 个字节
 * @param byteBuf
 * @return
 */
public static byte XorSumBytes(ByteBuf byteBuf) {
    byte sum = byteBuf.getByte(byteBuf.readerIndex());
    for (int i = byteBuf.readerIndex() + 1; i < byteBuf.writerIndex(); i++) {
        sum = (byte) (sum ^ byteBuf.getByte(i));
    }
    return sum;
}
 
Example 14
Source File: MinecraftVarintFrameDecoder.java    From Velocity with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (!in.isReadable()) {
    return;
  }

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

    byte read = in.readByte();
    if (read >= 0) {
      // Make sure reader index of length buffer is returned to the beginning
      in.readerIndex(origReaderIndex);
      int packetLength = ProtocolUtils.readVarInt(in);
      if (packetLength == 0) {
        return;
      }

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

      out.add(in.readRetainedSlice(packetLength));
      return;
    }
  }

  throw new CorruptedFrameException("VarInt too big");
}
 
Example 15
Source File: ByteBufUtils.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static String byteBufToString(ByteBuf buf) {
    StringBuilder sb = new StringBuilder();
    if (buf.readableBytes() == 0) {
        return sb.toString();
    }
    for (int i = buf.readerIndex(); i < buf.readerIndex() + buf.readableBytes(); i++) {
        sb.append(buf.getUnsignedByte(i)).append(" ");
    }
    return sb.toString();
}
 
Example 16
Source File: PduCodec.java    From herddb with Apache License 2.0 5 votes vote down vote up
public static String readTablename(Pdu pdu) {
    ByteBuf buffer = pdu.buffer;
    buffer.readerIndex(VERSION_SIZE
            + FLAGS_SIZE
            + TYPE_SIZE
            + MSGID_SIZE
    );
    ByteBufUtils.skipArray(buffer); // tablespace
    return ByteBufUtils.readString(buffer);
}
 
Example 17
Source File: HpackDecoderULE128Benchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static int decodeULE128UsingLong(ByteBuf in, int result) throws Http2Exception {
    final int readerIndex = in.readerIndex();
    final long v = decodeULE128(in, (long) result);
    if (v > Integer.MAX_VALUE) {
        in.readerIndex(readerIndex);
        throw DECODE_ULE_128_TO_INT_DECOMPRESSION_EXCEPTION;
    }
    return (int) v;
}
 
Example 18
Source File: Decoder.java    From Almost-Famous with MIT License 4 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    in.markReaderIndex();
    int preIndex = in.readerIndex();

    try {
        int length = readInt(in);
        if (preIndex == in.readerIndex()) {
            return;
        }
        if (length <= 0) {
            in.resetReaderIndex();
            return;
        }

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

        preIndex = in.readerIndex();
        int type = readInt(in);
        if (preIndex == in.readerIndex()) {
            return;
        }
        if (type <= 0) {
            in.resetReaderIndex();
            return;
        }
        Class<Protocol> cls = RegistryProtocol.protocolMap.get(type);
        if (cls == null) {
            LOG.error("未找到相应协议号{}.", type);
            return;
        }

        preIndex = in.readerIndex();

        byte[] msg = new byte[length - 4];
        in.readBytes(msg);
        Protocol p = cls.newInstance();
        p.setType(type);
        p.setMsg(msg);
        p.setChannel(ctx.channel());
        out.add(p);

        if (LOG.isInfoEnabled()) {
            LOG.info("request protocol sid={}, type={}, length={}, msg={}", Misc.getSid(ctx.channel()), type, length, msg);
        }
    } catch (Exception e) {
        in.resetReaderIndex();
        LOG.error("battle decoder error.", e);
        LinkMgr.closeOnFlush(ctx.channel(), ConnState.CLOSE_PROTOCOL_ERROR);
    }
}
 
Example 19
Source File: StringCodec.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public Object decode(ByteBuf buf, State state) {
    String str = buf.toString(charset);
    buf.readerIndex(buf.readableBytes());
    return str;
}
 
Example 20
Source File: ChunkSerializationTest.java    From opc-ua-stack with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "getSymmetricSecurityParameters")
public void testSymmetricMessage(SecurityPolicy securityPolicy,
                                 MessageSecurityMode messageSecurity,
                                 int messageSize) throws Exception {

    logger.info("Symmetric chunk serialization, securityPolicy={}, messageSecurityMode={}, messageSize={}",
            securityPolicy, messageSecurity, messageSize);

    ChunkEncoder encoder = new ChunkEncoder(parameters);
    ChunkDecoder decoder = new ChunkDecoder(parameters);

    SecureChannel[] channels = generateChannels(securityPolicy, messageSecurity);
    ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
    ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];

    clientChannel
            .attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE)
            .setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));

    LongSequence requestId = clientChannel
            .attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();

    byte[] messageBytes = new byte[messageSize];
    for (int i = 0; i < messageBytes.length; i++) {
        messageBytes[i] = (byte) i;
    }

    ByteBuf messageBuffer = BufferUtil.buffer().writeBytes(messageBytes);

    List<ByteBuf> chunkBuffers = encoder.encodeSymmetric(
            clientChannel,
            MessageType.SecureMessage,
            messageBuffer,
            requestId.getAndIncrement()
    );

    ByteBuf decodedBuffer = decoder.decodeSymmetric(
            serverChannel,
            chunkBuffers
    );

    ReferenceCountUtil.releaseLater(messageBuffer);
    ReferenceCountUtil.releaseLater(decodedBuffer);

    messageBuffer.readerIndex(0);
    assertEquals(decodedBuffer, messageBuffer);
}