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

The following examples show how to use io.netty.buffer.ByteBuf#readSlice() . 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: Ipv4NlriHandler.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
static DestinationMvpnIpv4AdvertizedCase parseIpv4ReachNlri(
    final ByteBuf nlri,
    final boolean addPathSupported) {
    final List<MvpnDestination> dests = new ArrayList<>();

    while (nlri.isReadable()) {
        final MvpnDestinationBuilder builder = new MvpnDestinationBuilder();
        if (addPathSupported) {
            builder.setPathId(PathIdUtil.readPathId(nlri));
        }
        final NlriType type = NlriType.forValue(nlri.readUnsignedByte());
        final int length = nlri.readUnsignedByte();
        final ByteBuf nlriBuf = nlri.readSlice(length);
        builder.setMvpnChoice(SimpleMvpnNlriRegistry.getInstance().parseMvpn(type, nlriBuf));
        dests.add(builder.build());
    }

    return new DestinationMvpnIpv4AdvertizedCaseBuilder()
        .setDestinationMvpn(new DestinationMvpnBuilder().setMvpnDestination(dests).build()).build();
}
 
Example 2
Source File: HandshakeV10Request.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
private static ByteBuf readCStringRetainedSlice(ByteBuf buf) {
    int bytes = buf.bytesBefore(TERMINAL);

    if (bytes < 0) {
        throw new IllegalArgumentException("buf has no C-style string");
    }

    if (bytes == 0) {
        // skip terminal
        buf.skipBytes(1);
        // use EmptyByteBuf
        return buf.alloc().buffer(0, 0);
    }

    ByteBuf result = buf.readSlice(bytes);
    buf.skipBytes(1);
    return result.retain();
}
 
Example 3
Source File: RedisDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private boolean decodeBulkStringContent(ByteBuf in, List<Object> out) throws Exception {
    final int readableBytes = in.readableBytes();
    if (readableBytes == 0 || remainingBulkLength == 0 && readableBytes < RedisConstants.EOL_LENGTH) {
        return false;
    }

    // if this is last frame.
    if (readableBytes >= remainingBulkLength + RedisConstants.EOL_LENGTH) {
        ByteBuf content = in.readSlice(remainingBulkLength);
        readEndOfLine(in);
        // Only call retain after readEndOfLine(...) as the method may throw an exception.
        out.add(new DefaultLastBulkStringRedisContent(content.retain()));
        resetDecoder();
        return true;
    }

    // chunked write.
    int toRead = Math.min(remainingBulkLength, readableBytes);
    remainingBulkLength -= toRead;
    out.add(new DefaultBulkStringRedisContent(in.readSlice(toRead).retain()));
    return true;
}
 
Example 4
Source File: HandshakeV10Request.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
private static ByteBuf readCStringRetainedSlice(ByteBuf buf) {
    int bytes = buf.bytesBefore(TERMINAL);

    if (bytes < 0) {
        throw new IllegalArgumentException("buf has no C-style string");
    }

    if (bytes == 0) {
        // skip terminal
        buf.skipBytes(1);
        // use EmptyByteBuf
        return buf.alloc().buffer(0, 0);
    }

    ByteBuf result = buf.readSlice(bytes);
    buf.skipBytes(1);
    return result.retain();
}
 
Example 5
Source File: LoginPluginResponse.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  this.id = ProtocolUtils.readVarInt(buf);
  this.success = buf.readBoolean();
  if (buf.isReadable()) {
    this.data = buf.readSlice(buf.readableBytes());
  } else {
    this.data = Unpooled.EMPTY_BUFFER;
  }
}
 
Example 6
Source File: LinkAttributesParser.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate
        .attribute.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
    for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
        final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
        unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(
            new Bandwidth(ByteArray.readAllBytes(v))).setPriority(Uint8.valueOf(i)).build());
    }
    builder.setUnreservedBandwidth(unreservedBandwidth);
    LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
}
 
Example 7
Source File: RedisDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ByteBuf readLine(ByteBuf in) {
    if (!in.isReadable(RedisConstants.EOL_LENGTH)) {
        return null;
    }
    final int lfIndex = in.forEachByte(ByteProcessor.FIND_LF);
    if (lfIndex < 0) {
        return null;
    }
    ByteBuf data = in.readSlice(lfIndex - in.readerIndex() - 1); // `-1` is for CR
    readEndOfLine(in); // validate CR LF
    return data;
}
 
Example 8
Source File: LoginPluginMessage.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  this.id = ProtocolUtils.readVarInt(buf);
  this.channel = ProtocolUtils.readString(buf);
  if (buf.isReadable()) {
    this.data = buf.readSlice(buf.readableBytes());
  } else {
    this.data = Unpooled.EMPTY_BUFFER;
  }
}
 
Example 9
Source File: InformationalFastRerouteObjectParser.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
    final LegacyFastRerouteObjectBuilder builder = new LegacyFastRerouteObjectBuilder()
            .setSetupPriority(ByteBufUtils.readUint8(byteBuf))
            .setHoldPriority(ByteBufUtils.readUint8(byteBuf))
            .setHopLimit(ByteBufUtils.readUint8(byteBuf));

    //skip reserved
    byteBuf.skipBytes(Byte.BYTES);
    final ByteBuf v = byteBuf.readSlice(METRIC_VALUE_F_LENGTH);
    builder.setBandwidth(new Bandwidth(ByteArray.readAllBytes(v)));
    builder.setIncludeAny(new AttributeFilter(ByteBufUtils.readUint32(byteBuf)));
    builder.setExcludeAny(new AttributeFilter(ByteBufUtils.readUint32(byteBuf)));
    return builder.build();
}
 
Example 10
Source File: FrameBodyCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
static ByteBuf dataWithoutMarking(ByteBuf byteBuf, boolean hasMetadata) {
  if (hasMetadata) {
    /*moves reader index*/
    int length = decodeLength(byteBuf);
    byteBuf.skipBytes(length);
  }
  if (byteBuf.readableBytes() > 0) {
    return byteBuf.readSlice(byteBuf.readableBytes());
  } else {
    return Unpooled.EMPTY_BUFFER;
  }
}
 
Example 11
Source File: ZMTPSocket.java    From netty-zmtp with Apache License 2.0 5 votes vote down vote up
@Override
public void content(final ChannelHandlerContext ctx, final ByteBuf data,
                    final List<Object> out) {
  if (data.readableBytes() < frameLength) {
    return;
  }
  if (frameLength == 0) {
    frames.add(DELIMITER);
    return;
  }
  final ByteBuf frame = data.readSlice(frameLength);
  frame.retain();
  frames.add(frame);
}
 
Example 12
Source File: LoginSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LoginPacket packet) {
    packet.setProtocolVersion(buffer.readInt());

    ByteBuf jwt = buffer.readSlice(VarInts.readUnsignedInt(buffer)); // Get the JWT.
    packet.setChainData(BedrockUtils.readLEAsciiString(jwt));
    packet.setSkinData(BedrockUtils.readLEAsciiString(jwt));
}
 
Example 13
Source File: LoginSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LoginPacket packet) {
    packet.setProtocolVersion(buffer.readInt());

    ByteBuf jwt = buffer.readSlice(VarInts.readUnsignedInt(buffer)); // Get the JWT.
    packet.setChainData(BedrockUtils.readLEAsciiString(jwt));
    packet.setSkinData(BedrockUtils.readLEAsciiString(jwt));
}
 
Example 14
Source File: ArraySerializer.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
public static ByteBuf readVarIntByteArraySlice(ByteBuf from, int limit) {
	int length = VarNumberSerializer.readVarInt(from);
	MiscSerializer.checkLimit(length, limit);
	return from.readSlice(length);
}
 
Example 15
Source File: SendMessageCommandV6.java    From hermes with Apache License 2.0 4 votes vote down vote up
private MessageBatchWithRawData readMsgs(String topic, HermesPrimitiveCodec codec, ByteBuf buf) {
	int size = codec.readInt();

	List<Integer> msgSeqs = new ArrayList<Integer>();

	for (int j = 0; j < size; j++) {
		msgSeqs.add(codec.readInt());
	}

	int payloadLen = codec.readInt();

	ByteBuf rawData = buf.readSlice(payloadLen);

	return new MessageBatchWithRawData(topic, msgSeqs, rawData, getTargetIdc());

}
 
Example 16
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@Override
public final ByteBuf readSlice(final ByteBuf in) {
    return in.readSlice(Integer.BYTES);
}
 
Example 17
Source File: HAProxyMessage.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static HAProxyTLV readNextTLV(final ByteBuf header) {

        // We need at least 4 bytes for a TLV
        if (header.readableBytes() < 4) {
            return null;
        }

        final byte typeAsByte = header.readByte();
        final HAProxyTLV.Type type = HAProxyTLV.Type.typeForByteValue(typeAsByte);

        final int length = header.readUnsignedShort();
        switch (type) {
        case PP2_TYPE_SSL:
            final ByteBuf rawContent = header.retainedSlice(header.readerIndex(), length);
            final ByteBuf byteBuf = header.readSlice(length);
            final byte client = byteBuf.readByte();
            final int verify = byteBuf.readInt();

            if (byteBuf.readableBytes() >= 4) {

                final List<HAProxyTLV> encapsulatedTlvs = new ArrayList<HAProxyTLV>(4);
                do {
                    final HAProxyTLV haProxyTLV = readNextTLV(byteBuf);
                    if (haProxyTLV == null) {
                        break;
                    }
                    encapsulatedTlvs.add(haProxyTLV);
                } while (byteBuf.readableBytes() >= 4);

                return new HAProxySSLTLV(verify, client, encapsulatedTlvs, rawContent);
            }
            return new HAProxySSLTLV(verify, client, Collections.<HAProxyTLV>emptyList(), rawContent);
        // If we're not dealing with a SSL Type, we can use the same mechanism
        case PP2_TYPE_ALPN:
        case PP2_TYPE_AUTHORITY:
        case PP2_TYPE_SSL_VERSION:
        case PP2_TYPE_SSL_CN:
        case PP2_TYPE_NETNS:
        case OTHER:
            return new HAProxyTLV(type, typeAsByte, header.readRetainedSlice(length));
        default:
            return null;
        }
    }
 
Example 18
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@Override
public final ByteBuf readSlice(final ByteBuf in) {
    return in.readSlice(java.lang.Byte.BYTES + in.getUnsignedByte(in.readerIndex()));
}
 
Example 19
Source File: MiddleCustomPayload.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void readServerData(ByteBuf serverdata) {
	tag = StringSerializer.readVarIntUTF8String(serverdata);
	data = serverdata.readSlice(serverdata.readableBytes());
}
 
Example 20
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@Override
public ByteBuf readSlice(final ByteBuf in) {
    final int length = in.getUnsignedShortLE(in.readerIndex());
    return in.readSlice(Short.BYTES + length);
}