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

The following examples show how to use io.netty.buffer.ByteBuf#readSlice() .
Example 1
Project: bgpcep   File: Ipv4NlriHandler.java    License: 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
Project: r2dbc-mysql   File: HandshakeV10Request.java    License: 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
Project: netty-4.1.22   File: RedisDecoder.java    License: 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
Project: r2dbc-mysql   File: HandshakeV10Request.java    License: 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
Project: Protocol   File: LoginSerializer_v313.java    License: 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 6
Project: Velocity   File: LoginPluginResponse.java    License: 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 7
Project: bgpcep   File: LinkAttributesParser.java    License: 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 8
Project: netty-4.1.22   File: RedisDecoder.java    License: 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 9
Project: Velocity   File: LoginPluginMessage.java    License: 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 10
@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 11
Project: rsocket-java   File: FrameBodyCodec.java    License: 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 12
Project: netty-zmtp   File: ZMTPSocket.java    License: 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 13
Project: Protocol   File: LoginSerializer_v361.java    License: 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
Project: azure-cosmosdb-java   File: RntbdTokenType.java    License: MIT License 4 votes vote down vote up
@Override
public final ByteBuf readSlice(final ByteBuf in) {
    return in.readSlice(Integer.BYTES);
}
 
Example 15
Project: ProtocolSupport   File: ArraySerializer.java    License: 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 16
Project: hermes   File: SendMessageCommandV6.java    License: 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 17
Project: netty-4.1.22   File: HAProxyMessage.java    License: 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
Project: azure-cosmosdb-java   File: RntbdTokenType.java    License: 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
Project: ProtocolSupport   File: MiddleCustomPayload.java    License: 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
Project: azure-cosmosdb-java   File: RntbdTokenType.java    License: 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);
}