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

The following are Jave code examples for showing how to use readSlice() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: sstable-adaptor   File: CBUtil.java   Source Code and License Vote up 6 votes
public static ByteBuffer readBoundValue(ByteBuf cb, ProtocolVersion protocolVersion)
{
    int length = cb.readInt();
    if (length < 0)
    {
        if (protocolVersion.isSmallerThan(ProtocolVersion.V4)) // backward compatibility for pre-version 4
            return null;
        if (length == -1)
            return null;
        else if (length == -2)
            return ByteBufferUtil.UNSET_BYTE_BUFFER;
        else
            throw new ProtocolException("Invalid ByteBuf length " + length);
    }
    ByteBuf slice = cb.readSlice(length);

    return ByteBuffer.wrap(readRawBytes(slice));
}
 
Example 2
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 6 votes
@Override
public ByteBuffer readBytes(ByteBuf source) {
  int len = readInt(source);
  if (len < 0) {
    return null;
  }

  ByteBuf slice = source.readSlice(len);
  // if direct byte buffer, return underlying nioBuffer.
  if (slice.isDirect()) {
    return slice.nioBuffer();
  }
  // otherwise copy to a byte array and wrap it.
  final byte[] out = new byte[slice.readableBytes()];
  source.getBytes(source.readerIndex(), out, 0, len);
  return ByteBuffer.wrap(out);
}
 
Example 3
Project: JRediClients   File: CommandDecoder.java   Source Code and License Vote up 6 votes
public ByteBuf readBytes(ByteBuf is) throws IOException {
    long l = readLong(is);
    if (l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(
                "Java only supports arrays up to " + Integer.MAX_VALUE + " in size");
    }
    int size = (int) l;
    if (size == -1) {
        return null;
    }
    ByteBuf buffer = is.readSlice(size);
    int cr = is.readByte();
    int lf = is.readByte();
    if (cr != CR || lf != LF) {
        throw new IOException("Improper line ending: " + cr + ", " + lf);
    }
    return buffer;
}
 
Example 4
Project: sstable-adaptor   File: CBUtil.java   Source Code and License Vote up 5 votes
public static ByteBuffer readValue(ByteBuf cb)
{
    int length = cb.readInt();
    if (length < 0)
        return null;
    ByteBuf slice = cb.readSlice(length);

    return ByteBuffer.wrap(readRawBytes(slice));
}
 
Example 5
Project: fastdfs-spring-boot   File: ReplierDecoder.java   Source Code and License Vote up 5 votes
protected void readContent(ByteBuf in, CompletableFuture<T> promise) {

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

        ByteBuf buf = in.readSlice((int) length);
        T result = decoder.decode(buf);
        promise.complete(result);
        atHead = true;
    }
 
Example 6
Project: JRediClients   File: JCacheEventCodec.java   Source Code and License Vote up 5 votes
@Override
    public Object decode(ByteBuf buf, State state) throws IOException {
        List<Object> result = new ArrayList<Object>();
        int keyLen;
        if (PlatformDependent.isWindows()) {
keyLen = buf.readIntLE();
        } else {
keyLen = (int) buf.readLongLE();
        }
        ByteBuf keyBuf = buf.readSlice(keyLen);
        Object key = codec.getMapKeyDecoder().decode(keyBuf, state);
        result.add(key);

        int valueLen;
        if (PlatformDependent.isWindows()) {
valueLen = buf.readIntLE();
        } else {
valueLen = (int) buf.readLongLE();
        }
        ByteBuf valueBuf = buf.readSlice(valueLen);
        Object value = codec.getMapValueDecoder().decode(valueBuf, state);
        result.add(value);
        
        if (sync) {
            double syncId = buf.order(ByteOrder.LITTLE_ENDIAN).readDouble();
            result.add(syncId);
        }
        
        return result;
    }
 
Example 7
Project: JRediClients   File: MapCacheEventCodec.java   Source Code and License Vote up 5 votes
private Object decode(ByteBuf buf, State state, Decoder<?> decoder) throws IOException {
     int keyLen;
     if (PlatformDependent.isWindows()) {
keyLen = buf.readIntLE();
     } else {
keyLen = (int) buf.readLongLE();
     }
     ByteBuf keyBuf = buf.readSlice(keyLen);
     Object key = decoder.decode(keyBuf, state);
     return key;
 }
 
Example 8
Project: tasfe-framework   File: HttpContentInputStream.java   Source Code and License Vote up 5 votes
private ByteBuf readContent(int length) throws IOException {
    ByteBuf content = current.content();
    if (length < content.readableBytes()) {
        return content.readSlice(length);
    } else {
        return content;
    }
}
 
Example 9
Project: azeroth   File: ReplierDecoder.java   Source Code and License Vote up 5 votes
protected void readContent(ByteBuf in, CompletableFuture<T> promise) {

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

        ByteBuf buf = in.readSlice((int) length);
        T result = decoder.decode(buf);
        promise.complete(result);
        atHead = true;
    }
 
Example 10
Project: neoscada   File: APDUDecoder.java   Source Code and License Vote up 4 votes
@Override
protected void decode ( final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out ) throws Exception
{
    if ( logger.isTraceEnabled () )
    {
        logger.trace ( "decode - bytes: {}", ByteBufUtil.hexDump ( in ) );
    }

    if ( in.readableBytes () < Constants.APCI_MIN_LENGTH )
    {
        return;
    }

    final byte start = in.getByte ( in.readerIndex () + 0 );
    if ( start != Constants.START_BYTE )
    {
        throw new DecoderException ( String.format ( "ACPI must start with 0x%02x but did with 0x%02x", Constants.START_BYTE, start ) );
    }

    final short len = in.getUnsignedByte ( in.readerIndex () + 1 );

    if ( len > Constants.APCI_MAX_DATA_LENGTH )
    {
        throw new DecoderException ( String.format ( "APCI has a maximum length of %s bytes, but we received %s", Constants.APCI_MAX_DATA_LENGTH, len ) );
    }

    if ( in.readableBytes () < len + 2 )
    {
        return;
    }

    // we have the full InformationTransfer

    // skip start & len
    in.skipBytes ( 2 );

    // read control fields
    final ByteBuf controlFields = in.readSlice ( 4 );

    final ByteBuf data;
    if ( len > 4 )
    {
        data = Unpooled.copiedBuffer ( in.readSlice ( len - 4 ) ).order ( ByteOrder.LITTLE_ENDIAN );
    }
    else
    {
        data = null;
    }

    // now add the PDU

    out.add ( decode ( controlFields, data ) );
}
 
Example 11
Project: athena   File: JsonRpcReaderUtil.java   Source Code and License Vote up 4 votes
/**
 * Decode the bytes to Json object.
 * @param in input of bytes
 * @param out ouput of Json object list
 * @param jrContext context for the last decoding process
 * @throws IOException IOException
 * @throws JsonParseException JsonParseException
 */
public static void readToJsonNode(ByteBuf in, List<Object> out, JsonReadContext jrContext)
        throws JsonParseException, IOException {
    int lastReadBytes = jrContext.getLastReadBytes();
    if (lastReadBytes == 0) {
        if (in.readableBytes() < 4) {
            return;
        }
        checkEncoding(in);
    }

    int i = lastReadBytes + in.readerIndex();
    Stack<Byte> bufStack = jrContext.getBufStack();
    for (; i < in.writerIndex(); i++) {
        byte b = in.getByte(i);
        switch (b) {
        case '{':
            if (!isDoubleQuote(bufStack)) {
                bufStack.push(b);
                jrContext.setStartMatch(true);
            }
            break;
        case '}':
            if (!isDoubleQuote(bufStack)) {
                bufStack.pop();
            }
            break;
        case '"':
            if (in.getByte(i - 1) != '\\') {
                if (!bufStack.isEmpty() && bufStack.peek() != '"') {
                    bufStack.push(b);
                } else {
                    bufStack.pop();
                }
            }
            break;
        default:
            break;
        }

        if (jrContext.isStartMatch() && bufStack.isEmpty()) {
            ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
            JsonParser jf = new MappingJsonFactory().createParser(new ByteBufInputStream(buf));
            JsonNode jsonNode = jf.readValueAsTree();
            out.add(jsonNode);
            lastReadBytes = 0;
            jrContext.setLastReadBytes(lastReadBytes);
            break;
        }
    }

    if (i >= in.writerIndex()) {
        lastReadBytes = in.readableBytes();
        jrContext.setLastReadBytes(lastReadBytes);
    }
}