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

The following examples show how to use io.netty.buffer.ByteBuf#slice() . 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: Http2FrameCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void sendGoAway() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, 3, request, 31, false);

    Http2Stream stream = frameCodec.connection().stream(3);
    assertNotNull(stream);
    assertEquals(State.OPEN, stream.state());

    ByteBuf debugData = bb("debug");
    ByteBuf expected = debugData.copy();

    Http2GoAwayFrame goAwayFrame = new DefaultHttp2GoAwayFrame(Http2Error.NO_ERROR.code(), debugData.slice());
    goAwayFrame.setExtraStreamIds(2);

    inboundHandler.writeOutbound(goAwayFrame);
    verify(frameWriter).writeGoAway(
            eq(http2HandlerCtx), eq(7), eq(Http2Error.NO_ERROR.code()), eq(expected), anyChannelPromise());
    assertEquals(1, debugData.refCnt());
    assertEquals(State.OPEN, stream.state());
    assertTrue(channel.isActive());
    expected.release();
}
 
Example 2
Source File: MetaBlockImpl.java    From Distributed-KV with Apache License 2.0 6 votes vote down vote up
public MetaBlockImpl(FilterPolicy filterPolicy, ByteBuf result) {
	Preconditions.checkNotNull(filterPolicy);
	Preconditions.checkNotNull(result);
	this.filterPolicy = filterPolicy;
	this.result = result;
	// 获取kFilterBase
	kFilterBaseLg = result.getInt(result.readerIndex() + result.readableBytes() - Integer.BYTES);
	// 获取sum,filters的总大小
	sum = result.getInt(result.readerIndex() + result.readableBytes() - 2 * Integer.BYTES);
	// 切片得到filters
	filters = result.slice(result.readerIndex(), sum);
	// filter offset信息所占大小
	int filterOffsetSize = result.readableBytes() - 2 * Integer.BYTES - sum;
	// 根据filter offset信息所占大小,得到filter offset的数目,新建filter offset的数组
	filterOffsets = new int[filterOffsetSize / Integer.BYTES];
	// 将filter offset数组设置好
	for(int i = 0; i < filterOffsets.length; i++) {
		filterOffsets[i] = result.getInt(result.readerIndex() + sum + i * Integer.BYTES);
	}
}
 
Example 3
Source File: Http2FrameCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void goAwayLastStreamIdOverflowed() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, 5, request, 31, false);

    Http2Stream stream = frameCodec.connection().stream(5);
    assertNotNull(stream);
    assertEquals(State.OPEN, stream.state());

    ByteBuf debugData = bb("debug");
    Http2GoAwayFrame goAwayFrame = new DefaultHttp2GoAwayFrame(Http2Error.NO_ERROR.code(), debugData.slice());
    goAwayFrame.setExtraStreamIds(Integer.MAX_VALUE);

    inboundHandler.writeOutbound(goAwayFrame);
    // When the last stream id computation overflows, the last stream id should just be set to 2^31 - 1.
    verify(frameWriter).writeGoAway(eq(http2HandlerCtx), eq(Integer.MAX_VALUE), eq(Http2Error.NO_ERROR.code()),
                                    eq(debugData), anyChannelPromise());
    assertEquals(1, debugData.refCnt());
    assertEquals(State.OPEN, stream.state());
    assertTrue(channel.isActive());
}
 
Example 4
Source File: SimpleNlriRegistry.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public MpUnreachNlri parseMpUnreach(final ByteBuf buffer, final PeerSpecificParserConstraint constraint)
        throws BGPParsingException {
    final MpUnreachNlriBuilder builder = new MpUnreachNlriBuilder();
    builder.setAfi(getAfi(buffer));
    builder.setSafi(getSafi(buffer));

    if (buffer.isReadable()) {
        final ByteBuf nlri = buffer.slice();
        final BgpTableType key = createKey(builder.getAfi(), builder.getSafi());
        final NlriParser parser = this.handlers.get(key);
        if (parser == null) {
            LOG.warn(PARSER_NOT_FOUND, key);
        } else {
            parser.parseNlri(nlri, builder, constraint);
        }
    }
    return builder.build();
}
 
Example 5
Source File: PacketDecoder.java    From socketio with Apache License 2.0 5 votes vote down vote up
public static Packet decodePacket(final ByteBuf payload) throws IOException {
  int payloadSize = payload.readableBytes();

  // Decode packet type
  int typeDelimiterIndex = payload.forEachByte(packetDelimiterFinder);
  if (typeDelimiterIndex == -1) {
    return Packet.NULL_INSTANCE;
  }
  ByteBuf typeBytes = payload.slice(0, typeDelimiterIndex);
  String typeString = typeBytes.toString(CharsetUtil.UTF_8);
  int typeId = Integer.valueOf(typeString);
  PacketType type = PacketType.valueOf(typeId);

  // Skip message id
  int messageIdDelimiterIndex = payload.forEachByte(typeDelimiterIndex + 1, payloadSize - typeDelimiterIndex - 1, packetDelimiterFinder);
  if (messageIdDelimiterIndex == -1) {
    return Packet.NULL_INSTANCE;
  }

  // Skip endpoint
  int endpointDelimiterIndex = payload.forEachByte(messageIdDelimiterIndex + 1, payloadSize - messageIdDelimiterIndex - 1, packetDelimiterFinder);

  // Create instance of packet
  Packet packet = new Packet(type);

  // Decode data
  boolean messagingType = type == PacketType.MESSAGE || type == PacketType.JSON;
  if (endpointDelimiterIndex != -1 && messagingType) {
    int dataLength = payloadSize - endpointDelimiterIndex - 1;
    if (dataLength > 0) {
      ByteBuf data = payload.copy(endpointDelimiterIndex + 1, dataLength);
      packet.setData(data);
    }
  }

  return packet;
}
 
Example 6
Source File: JT808MessageDecoder.java    From jt808-server with Apache License 2.0 5 votes vote down vote up
/**
 * 校验
 */
@Override
public boolean check(ByteBuf buf) {
    byte checkCode = buf.getByte(buf.readableBytes() - 1);
    buf = buf.slice(0, buf.readableBytes() - 1);
    byte calculatedCheckCode = ByteBufUtils.xor(buf);

    return checkCode != calculatedCheckCode;
}
 
Example 7
Source File: JT808MessageDecoder.java    From jt808-server with Apache License 2.0 5 votes vote down vote up
/**
 * 截取转义前报文,并还原转义位
 */
protected ByteBuf slice(ByteBuf byteBuf, int index, int length) {
    byte second = byteBuf.getByte(index + length - 1);
    if (second == 0x02)
        byteBuf.setByte(index + length - 2, 0x7e);

    // 0x01 不做处理 p47
    // if (second == 0x01) {
    // }
    return byteBuf.slice(index, length - 1);
}
 
Example 8
Source File: GrpcRequestHandlerTest.java    From xio with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkedRequest() {
  HelloRequest grpcRequest = HelloRequest.newBuilder().setName("myName").build();
  ByteBuf grpcRequestBuffer = bufferFor(grpcRequest);
  int streamId = 234;

  int middleIndex = grpcRequestBuffer.readableBytes() / 2;
  ByteBuf firstHalf = grpcRequestBuffer.slice(0, middleIndex);
  ByteBuf secondHalf =
      grpcRequestBuffer.slice(middleIndex, grpcRequestBuffer.readableBytes() - middleIndex);

  channel.writeInbound(fullGrpcRequest(firstHalf, streamId, false));
  channel.writeInbound(fullGrpcRequest(secondHalf, streamId, true));

  Response response = channel.readOutbound();
  SegmentedData segmentedData = channel.readOutbound();

  assertEquals(HttpResponseStatus.OK, response.status());
  assertEquals(streamId, response.streamId());
  assertEquals("application/grpc+proto", response.headers().get(HttpHeaderNames.CONTENT_TYPE));

  HelloReply actualReply = protoObjectFor(segmentedData.content(), HelloReply::parseFrom);
  HelloReply expectedReply =
      HelloReply.newBuilder().setMessage(responsePrefix + grpcRequest.getName()).build();
  assertEquals(actualReply, expectedReply);

  assertEquals("0", Objects.requireNonNull(segmentedData.trailingHeaders()).get("grpc-status"));
  assertFalse(Objects.requireNonNull(segmentedData.trailingHeaders()).contains("grpc-message"));
  assertEquals(streamId, segmentedData.streamId());
  assertTrue(segmentedData.endOfMessage());
}
 
Example 9
Source File: InsertMessageDecoder.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressFBWarnings(value = {"RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"},
    justification = "Findbugs thinks ByteBuf#readerIndex(...) has no"
    + "side effect")
public InsertMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage) throws
    InvalidNamespaceException, InvalidBsonException {
  try {
    MyBsonContext context = new MyBsonContext(buffer);

    int flags = buffer.readInt();
    String fullCollectionName = stringReader.readCString(buffer, true);

    ByteBuf docBuf = buffer.slice(buffer.readerIndex(), buffer.readableBytes());
    docBuf.retain();

    buffer.readerIndex(buffer.writerIndex());

    ByteBufIterableDocumentProvider documents = new ByteBufIterableDocumentProvider(docBuf,
        docReader);

    //TODO: improve the way database and cache are pooled
    return new InsertMessage(
        requestBaseMessage,
        context,
        getDatabase(fullCollectionName).intern(),
        getCollection(fullCollectionName).intern(),
        EnumInt32FlagsUtil.isActive(Flag.CONTINUE_ON_ERROR, flags),
        documents
    );
  } catch (NettyBsonReaderException ex) {
    throw new InvalidBsonException(ex);
  }
}
 
Example 10
Source File: BloomFilter.java    From Distributed-KV with Apache License 2.0 5 votes vote down vote up
@Override
public boolean keyMayMatch(ByteBuf key, ByteBuf filter) {
	// filter信息的末尾是bitset大小信息
	int retSize = filter.readableBytes() - Integer.BYTES;
	int bitsetSize = filter.getInt(filter.readerIndex() + retSize);
	// 实际有效filter信息
	ByteBuf realFilter = filter.slice(filter.readerIndex(), retSize);
	// TODO 可优化. 建立byte数组,盛放key和附加k
	byte[] data = new byte[key.readableBytes() + Integer.BYTES];
	// 映射k次
	for (int x = 0; x < k; x++) {
		// 将key中的字节读入数组
		key.slice().readBytes(data, 0, key.readableBytes());
		// 将k组装到key的末尾
		data[key.readableBytes()] = ((k >> 24) & 0xff);
		data[key.readableBytes() + 1] = ((k >> 16) & 0xff);
		data[key.readableBytes() + 2] = ((k >> 8) & 0xff);
		data[key.readableBytes() + 3] = (k & 0xff);
		// 生成hash数据
		long hash = createHash(data);
		hash = hash % (long) bitsetSize;
		// 根据hash数据和filter信息,替代bitset来检验key的存在
		int index = Math.abs((int) hash);
		int i = index / 8;
		int j = 7 - index % 8;
		boolean ret = (realFilter.getByte(i) & (1 << j)) >> j == 1 ? true : false;
		if (!ret)
			return false;
	}
	return true;
}
 
Example 11
Source File: ProducerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Strips checksum from {@link OpSendMsg} command if present else ignore it.
 *
 * @param op
 */
private void stripChecksum(OpSendMsg op) {
    int totalMsgBufSize = op.cmd.readableBytes();
    ByteBufPair msg = op.cmd;
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        headerFrame.markReaderIndex();
        try {
            headerFrame.skipBytes(4); // skip [total-size]
            int cmdSize = (int) headerFrame.readUnsignedInt();

            // verify if checksum present
            headerFrame.skipBytes(cmdSize);

            if (!hasChecksum(headerFrame)) {
                return;
            }

            int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size]
            int checksumSize = 4 + 2; // [magic-number] [checksum-size]
            int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size]
            int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark
            int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum
            headerFrame.resetReaderIndex();
            int headerFrameSize = headerFrame.readableBytes();

            headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size]
            ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only
                                                                                                // metadata
            headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum
            metadata.readBytes(headerFrame, metadata.readableBytes());
            headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes
        } finally {
            headerFrame.resetReaderIndex();
        }
    } else {
        log.warn("[{}] Failed while casting {} into ByteBufPair", producerName,
                (op.cmd == null ? null : op.cmd.getClass().getName()));
    }
}
 
Example 12
Source File: AuthVerifyCheckDelimiterInboundhandler.java    From HttpProxy with MIT License 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    String delimiter= Config.delimiter();
    ByteBuf buf=(ByteBuf)msg;
    boolean valid=true;
    if(buf.readableBytes()<delimiter.length()){
        valid=false;
    }else{
        ByteBuf slice=buf.slice(buf.readableBytes()-delimiter.length(),2);
        byte[] sliceBytes=new byte[delimiter.length()];
        slice.readBytes(sliceBytes);
        if(!new String(sliceBytes).equals(delimiter)){
            valid=false;
        }

    }
    if(valid){
        logger.info("验证通过");
        ctx.pipeline().remove(this.getClass());
    }else{
        logger.info("验证失败,关闭channel。可能遭到嗅探");
        //ctx.writeAndFlush(PooledByteBufAllocator.DEFAULT.buffer().writeBytes(HttpResponse.ERROR404()));
        ctx.channel().close();
        return;
    }
    super.channelRead(ctx, msg);
}
 
Example 13
Source File: AbstractBulkStringEoFJudger.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
@Override
public JudgeResult doEnd(ByteBuf raw) {
	
	if(alreadyFinished){
		throw new IllegalStateException("doEnd already ended:" + this);
	}
	
	ByteBuf used = raw.slice();
	int readable = used.readableBytes();
	
	if(readable > MARK_LENGTH){
		int writeIndex = used.writerIndex();
		used.readerIndex(writeIndex - MARK_LENGTH);
	}
	
	int dataLen = used.readableBytes();
	int remLen = MARK_LENGTH - dataLen;
	
	System.arraycopy(lastData, dataLen, lastData, 0, remLen);
	used.readBytes(lastData, remLen, dataLen);
	
	boolean ends = Arrays.equals(eofmark, lastData);
	if(ends){
		alreadyFinished = true;
	}
	return new JudgeResult(ends, readable);
}
 
Example 14
Source File: MarshallingDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
    return buffer.slice(index, length);
}
 
Example 15
Source File: NovaPacket.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
	data = buffer.slice();
}
 
Example 16
Source File: NovaPacket.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
	data = buffer.slice();
}
 
Example 17
Source File: SSTableBuilderImpl.java    From Distributed-KV with Apache License 2.0 4 votes vote down vote up
/**
   * TODO 待测试
   * 获取不小于start并且不大于limit的ByteBuf作为两个data block之间的分隔符
   * @param start 下界
   * @param limit 上界
   * @return
   */
  public static ByteBuf getSeparator(ByteBuf start, ByteBuf limit)
  {
  	Preconditions.checkNotNull(start);
  	if(limit == null) {
  		return start.slice();
  	}
  	ByteBuf separator = null;
  	ByteBuf initialStart = start;
  	// 计算公共前缀长度
  	limit = limit.copy();
start = start.copy();
// 公共前缀长度
int len = 0;
// 当两个bytebuf还有剩余数据的时候,逐一查看其字节是否相等
while(start.readableBytes() > 0 && limit.readableBytes() > 0) {
	if(start.readByte() == limit.readByte()) {
		len++;
		// 如果start完全是limit的前缀,则以start为分隔符
		if(start.readableBytes() == 0) {
			separator = initialStart.slice();
			return separator;
		}
	}
	else {
		break;
	}
}

  	
  	// 把start非公共部分的第一个字节+1,得到一个新的key,如果这个key不大于limit,则key为分隔符
// 获取当前位置的字节
Byte oldByte = start.getByte(len);
// 如果字节达到最大值
if(oldByte == Byte.MAX_VALUE) {
	separator = initialStart.slice();
}
else {
	Byte newByte = (byte) (oldByte + 1);
	// 如果字节+1后超过了limit同位置上的数据
	if(newByte > limit.getByte(len)) {
		separator = initialStart.slice();
	}
	else {
		// 拷贝可读数据,将第一个字节设置为newByte
		separator = initialStart.copy();
		separator.setByte(len, newByte);
		separator = separator.slice(0, len + 1);
	}
}

  	// 否则,返回start作为分隔符
      return separator;
  }
 
Example 18
Source File: MixMessageDecoder.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Override
protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index,
        int length) {
    return buffer.slice(index, length);
}
 
Example 19
Source File: TestFrameDecoder.java    From simulacron with Apache License 2.0 4 votes vote down vote up
@Override
protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
  // use slice instead of retainedSlice (what super does) so don't need to release later.
  return buffer.slice(index, length);
}
 
Example 20
Source File: CompositeMetadataCodec.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
/**
 * Decode the next metadata entry (a mime header + content pair of {@link ByteBuf}) from a {@link
 * ByteBuf} that contains at least enough bytes for one more such entry. These buffers are
 * actually slices of the full metadata buffer, and this method doesn't move the full metadata
 * buffer's {@link ByteBuf#readerIndex()}. As such, it requires the user to provide an {@code
 * index} to read from. The next index is computed by calling {@link #computeNextEntryIndex(int,
 * ByteBuf, ByteBuf)}. Size of the first buffer (the "header buffer") drives which decoding method
 * should be further applied to it.
 *
 * <p>The header buffer is either:
 *
 * <ul>
 *   <li>made up of a single byte: this represents an encoded mime id, which can be further
 *       decoded using {@link #decodeMimeIdFromMimeBuffer(ByteBuf)}
 *   <li>made up of 2 or more bytes: this represents an encoded mime String + its length, which
 *       can be further decoded using {@link #decodeMimeTypeFromMimeBuffer(ByteBuf)}. Note the
 *       encoded length, in the first byte, is skipped by this decoding method because the
 *       remaining length of the buffer is that of the mime string.
 * </ul>
 *
 * @param compositeMetadata the source {@link ByteBuf} that originally contains one or more
 *     metadata entries
 * @param entryIndex the {@link ByteBuf#readerIndex()} to start decoding from. original reader
 *     index is kept on the source buffer
 * @param retainSlices should produced metadata entry buffers {@link ByteBuf#slice() slices} be
 *     {@link ByteBuf#retainedSlice() retained}?
 * @return a {@link ByteBuf} array of length 2 containing the mime header buffer
 *     <strong>slice</strong> and the content buffer <strong>slice</strong>, or one of the
 *     zero-length error constant arrays
 */
public static ByteBuf[] decodeMimeAndContentBuffersSlices(
    ByteBuf compositeMetadata, int entryIndex, boolean retainSlices) {
  compositeMetadata.markReaderIndex();
  compositeMetadata.readerIndex(entryIndex);

  if (compositeMetadata.isReadable()) {
    ByteBuf mime;
    int ridx = compositeMetadata.readerIndex();
    byte mimeIdOrLength = compositeMetadata.readByte();
    if ((mimeIdOrLength & STREAM_METADATA_KNOWN_MASK) == STREAM_METADATA_KNOWN_MASK) {
      mime =
          retainSlices
              ? compositeMetadata.retainedSlice(ridx, 1)
              : compositeMetadata.slice(ridx, 1);
    } else {
      // M flag unset, remaining 7 bits are the length of the mime
      int mimeLength = Byte.toUnsignedInt(mimeIdOrLength) + 1;

      if (compositeMetadata.isReadable(
          mimeLength)) { // need to be able to read an extra mimeLength bytes
        // here we need a way for the returned ByteBuf to differentiate between a
        // 1-byte length mime type and a 1 byte encoded mime id, preferably without
        // re-applying the byte mask. The easiest way is to include the initial byte
        // and have further decoding ignore the first byte. 1 byte buffer == id, 2+ byte
        // buffer == full mime string.
        mime =
            retainSlices
                ?
                // we accommodate that we don't read from current readerIndex, but
                // readerIndex - 1 ("0"), for a total slice size of mimeLength + 1
                compositeMetadata.retainedSlice(ridx, mimeLength + 1)
                : compositeMetadata.slice(ridx, mimeLength + 1);
        // we thus need to skip the bytes we just sliced, but not the flag/length byte
        // which was already skipped in initial read
        compositeMetadata.skipBytes(mimeLength);
      } else {
        compositeMetadata.resetReaderIndex();
        throw new IllegalStateException("metadata is malformed");
      }
    }

    if (compositeMetadata.isReadable(3)) {
      // ensures the length medium can be read
      final int metadataLength = compositeMetadata.readUnsignedMedium();
      if (compositeMetadata.isReadable(metadataLength)) {
        ByteBuf metadata =
            retainSlices
                ? compositeMetadata.readRetainedSlice(metadataLength)
                : compositeMetadata.readSlice(metadataLength);
        compositeMetadata.resetReaderIndex();
        return new ByteBuf[] {mime, metadata};
      } else {
        compositeMetadata.resetReaderIndex();
        throw new IllegalStateException("metadata is malformed");
      }
    } else {
      compositeMetadata.resetReaderIndex();
      throw new IllegalStateException("metadata is malformed");
    }
  }
  compositeMetadata.resetReaderIndex();
  throw new IllegalArgumentException(
      String.format("entry index %d is larger than buffer size", entryIndex));
}