Example usage for io.netty.buffer ByteBuf readByte

List of usage examples for io.netty.buffer ByteBuf readByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readByte.

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:com.spotify.folsom.client.binary.RequestTestTemplate.java

License:Apache License

protected void assertHeader(final ByteBuf b, final int opcode, final int keyLength, final int extrasLength,
        final int totalLength, final int opaque, final long cas) {
    assertByte(0x80, b.readByte());
    assertByte(opcode, b.readByte());//from   w ww  .  java2s .  c o  m
    assertShort(keyLength, b.readShort());
    assertShort(extrasLength, b.readByte());
    assertZeros(b, 3);
    assertEquals(totalLength, b.readInt());
    assertEquals(opaque, b.readInt());
    assertEquals(cas, b.readLong());
}

From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java

License:Apache License

public static String parseString(final ByteBuf frame, final int size) {
    final byte[] buffer = new byte[size - 5];
    frame.readBytes(buffer);/*from   www .  jav a2s  . c  o  m*/

    if (frame.readByte() != '\0') {
        throw new RuntimeException("expected null byte");
    }

    return new String(buffer, UTF8);
}

From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java

License:Apache License

public static List<CollectdValue> parseValues(final ByteBuf frame, final int size) {
    final int n = frame.readUnsignedShort();

    final List<Integer> types = ImmutableList.copyOf(IntStream.range(0, n).map(i -> {
        return frame.readByte();
    }).iterator());//  ww  w  .  ja v  a 2s  .  c o  m

    final ImmutableList.Builder<CollectdValue> values = ImmutableList.builder();

    for (final int type : types) {
        switch (type) {
        case CollectdSample.COUNTER:
            final long c = frame.readLong();

            if (c < 0) {
                throw new IllegalArgumentException("value too large for signed type");
            }

            values.add(new Counter(c));
            break;
        case CollectdSample.GAUGE:
            frame.order(ByteOrder.LITTLE_ENDIAN);
            values.add(new CollectdValue.Gauge(frame.readDouble()));
            frame.order(ByteOrder.BIG_ENDIAN);
            break;
        case CollectdSample.DERIVE:
            values.add(new CollectdValue.Derive(frame.readLong()));
            break;
        case CollectdSample.ABSOLUTE:
            final long a = frame.readLong();

            if (a < 0) {
                throw new IllegalArgumentException("value too large for signed type");
            }

            values.add(new CollectdValue.Absolute(a));
            break;
        default:
            throw new IllegalArgumentException("invalid sample type: " + type);
        }
    }

    return values.build();
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParser.java

License:Apache License

/**
 * Parse a frame header.//from  www .  j a  va  2  s.  c  o m
 */
private boolean parseZMTP1Header(final ByteBuf buffer) throws ZMTPMessageParsingException {
    final long len = ZMTPUtils.decodeLength(buffer);

    if (len > Integer.MAX_VALUE) {
        throw new ZMTPMessageParsingException("Received too large frame: " + len);
    }

    if (len == -1) {
        return false;
    }

    if (len == 0) {
        throw new ZMTPMessageParsingException("Received frame with zero length");
    }

    // Read if we have more frames from flag byte
    if (buffer.readableBytes() < 1) {
        // Wait for more data to decode
        return false;
    }

    frameSize = (int) len - 1;
    hasMore = (buffer.readByte() & MORE_FLAG) == MORE_FLAG;

    return true;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParser.java

License:Apache License

private boolean parseZMTP2Header(ByteBuf buffer) throws ZMTPMessageParsingException {
    if (buffer.readableBytes() < 2) {
        return false;
    }/* w w w. ja v  a 2  s  .c  o  m*/
    int flags = buffer.readByte();
    hasMore = (flags & MORE_FLAG) == MORE_FLAG;
    if ((flags & LONG_FLAG) != LONG_FLAG) {
        frameSize = buffer.readByte() & 0xff;
        return true;
    }
    if (buffer.readableBytes() < 8) {
        return false;
    }
    long len;
    if (buffer.order() == BIG_ENDIAN) {
        len = buffer.readLong();
    } else {
        len = ByteBufUtil.swapLong(buffer.readLong());
    }
    if (len > Integer.MAX_VALUE) {
        throw new ZMTPMessageParsingException("Received too large frame: " + len);
    }
    frameSize = (int) len;
    return true;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Helper to decode a ZMTP/1.0 length field
 *
 * @return length//from www. j a v a2  s  .c  o m
 * @throws IndexOutOfBoundsException if there is not enough octets to be read.
 */
static public long decodeLength(final ByteBuf in) {
    long size = in.readByte() & 0xFF;
    if (size == 0xFF) {
        if (in.readableBytes() < 8) {
            return -1;
        }
        if (in.order() == BIG_ENDIAN) {
            size = in.readLong();
        } else {
            size = ByteBufUtil.swapLong(in.readLong());
        }
    }

    return size;
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP10WireFormat.java

License:Apache License

/**
 * Read a ZMTP/1.0 frame length.//w  w  w  .  j a  v  a 2s  . c om
 */
static long readLength(final ByteBuf in) {
    if (in.readableBytes() < 1) {
        return -1;
    }
    long size = in.readByte() & 0xFF;
    if (size == 0xFF) {
        if (in.readableBytes() < 8) {
            return -1;
        }
        size = in.readLong();
    }

    return size;
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.java

License:Apache License

/**
 * Read a ZMTP/2.0 greeting.//from ww  w. ja va  2  s  .co  m
 *
 * @param in The buffer to read the greeting from.
 * @return A {@link com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.Greeting}.
 * @throws ZMTPParsingException      If the greeting is malformed.
 * @throws IndexOutOfBoundsException If there is not enough readable bytes to read an entire
 *                                   greeting.
 */
static Greeting readGreeting(ByteBuf in) throws ZMTPParsingException {
    if (in.readByte() != (byte) 0xff) {
        throw new ZMTPParsingException("Illegal ZMTP/2.0 greeting, first octet not 0xff");
    }
    in.skipBytes(9);
    return readGreetingBody(in);
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.java

License:Apache License

/**
 * Read a ZMTP/2.0 greeting body./*from   www .  jav a  2 s  .  c  om*/
 *
 * @param in The buffer to read the greeting from.
 * @return A {@link com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.Greeting}.
 * @throws ZMTPParsingException      If the greeting is malformed.
 * @throws IndexOutOfBoundsException If there is not enough readable bytes to read an entire
 *                                   greeting.
 */
static Greeting readGreetingBody(final ByteBuf in) throws ZMTPParsingException {
    final int revision = in.readByte();
    final ZMTPSocketType socketType = readSocketType(in);
    final int flags = in.readByte();
    if (flags != 0x00) {
        throw new ZMTPParsingException(
                format("Malformed ZMTP/2.0 greeting. Flags (byte 13) expected to be 0x00, was 0x%02x", flags));
    }
    final int len = in.readByte();
    final byte[] identity = new byte[len];
    in.readBytes(identity);
    return new Greeting(revision, socketType, ByteBuffer.wrap(identity));
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.java

License:Apache License

/**
 * Read a ZMTP/2.0 socket type.//from ww w .  jav  a2  s.  c  o m
 *
 * @param in The buffer to read the socket type from.
 * @return A {@link ZMTPSocketType}.
 * @throws ZMTPParsingException      If the socket type is malformed.
 * @throws IndexOutOfBoundsException If there is not enough readable bytes to read an entire
 *                                   socket type.
 */
static ZMTPSocketType readSocketType(final ByteBuf in) throws ZMTPParsingException {
    return socketType((int) in.readByte());
}