Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

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

License:Apache License

protected void assertBytes(final ByteBuf buf, final byte[] zs) {
    final byte[] bs = new byte[zs.length];
    buf.readBytes(bs);
    assertArrayEquals(zs, bs);//from w  ww  . ja  va2s.  c om
}

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);

    if (frame.readByte() != '\0') {
        throw new RuntimeException("expected null byte");
    }/* ww w .  j  a  v  a2 s  .c o  m*/

    return new String(buffer, UTF8);
}

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

License:Apache License

/**
 * Create a new frame from a channel buffer.
 *//*from  w  w  w  .  j a v a  2s  . c o  m*/
public static ZMTPFrame create(ByteBuf buf) {
    if (!buf.isReadable()) {
        return EMPTY_FRAME;
    } else {
        byte[] dst = new byte[buf.capacity()];
        buf.readBytes(dst);
        return new ZMTPFrame(dst);
    }
}

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

License:Apache License

private static ByteBuffer readPayload(final ByteBuf data, final int size) {
    if (size == 0) {
        return EMPTY_BUFFER;
    }/*from w  w w  .  j  ava 2s.  c  o m*/
    final ByteBuffer buffer = ByteBuffer.allocate(size);
    data.readBytes(buffer);
    buffer.flip();
    return buffer;
}

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

License:Apache License

private static CharSequence readAscii(final ByteBuf data, final int size) {
    final byte[] chars = new byte[size];
    data.readBytes(chars);
    return new AsciiString(chars);
}

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

License:Apache License

@Override
public void content(final ChannelHandlerContext ctx, final ByteBuf data, final List<Object> out) {
    if (data.readableBytes() < frameSize) {
        return;//from  w w w .  ja va  2 s  .c  o  m
    }
    final ByteBuf expectedFrame = expected.message.frame(readIndex);
    final ByteBuf frame = data.readBytes((int) frameSize);
    if (!expectedFrame.equals(frame)) {
        throw new IllegalStateException("read frame did not match expected frame: " + "readIndex=" + readIndex
                + ", " + "expected frame=" + expectedFrame + "read frame=" + frame);
    }
    readIndex++;
}

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

License:Apache License

/**
 * Read the remote identity octets from a ZMTP/1.0 greeting.
 *///from  w  ww . j  a  v a2 s  .  c  o  m
static ByteBuffer readIdentity(final ByteBuf buffer) throws ZMTPParsingException {
    final long length = readLength(buffer);
    if (length == -1) {
        return null;
    }
    final long identityLength = length - 1;
    if (identityLength < 0 || identityLength > 255) {
        throw new ZMTPParsingException("Bad remote identity length: " + length);
    }

    // skip the flags byte
    buffer.skipBytes(1);

    final byte[] identity = new byte[(int) identityLength];
    buffer.readBytes(identity);
    return ByteBuffer.wrap(identity);
}

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

License:Apache License

/**
 * Read a ZMTP/2.0 greeting body.//  w  ww  .ja  v  a2s . c o 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 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.ZMTPCodec.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {

    // Discard input if handshake failed. It is expected that the user will close the channel.
    if (session.handshakeFuture().isDone()) {
        assert !session.handshakeFuture().isSuccess();
        in.skipBytes(in.readableBytes());
    }/*from  ww  w .j ava2 s  . co  m*/

    // Shake hands
    final ZMTPHandshake handshake;
    try {
        handshake = handshaker.handshake(in, ctx);
        if (handshake == null) {
            // Handshake is not yet done. Await more input.
            return;
        }
    } catch (Exception e) {
        session.handshakeFailure(e);
        ctx.fireUserEventTriggered(new ZMTPHandshakeFailure(session));
        throw e;
    }

    // Handshake is done.
    session.handshakeSuccess(handshake);

    // Replace this handler with the framing encoder and decoder
    if (actualReadableBytes() > 0) {
        out.add(in.readBytes(actualReadableBytes()));
    }
    final ZMTPDecoder decoder = config.decoder().decoder(session);
    final ZMTPEncoder encoder = config.encoder().encoder(session);
    final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(session.negotiatedVersion());
    final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>(
            new ZMTPFramingDecoder(wireFormat, decoder), new ZMTPFramingEncoder(wireFormat, encoder));
    ctx.pipeline().replace(this, ctx.name(), handler);

    // Tell the user that the handshake is complete
    ctx.fireUserEventTriggered(new ZMTPHandshakeSuccess(session, handshake));
}

From source file:com.srotya.sidewinder.core.ingress.binary.SeriesDataPointDecoder.java

License:Apache License

public static DataPoint decodeBufToDPoint(ByteBuf buf) {
    int dbNameLength = buf.readInt();
    if (dbNameLength < 0) {
        return null;
    }//from www . j  a  v a  2 s.  co m
    byte[] dbBytes = new byte[dbNameLength];
    buf.readBytes(dbBytes);
    String dbName = new String(dbBytes);
    int measurementNameLength = buf.readInt();
    if (measurementNameLength < 0) {
        return null;
    }
    byte[] measurementNameBytes = new byte[measurementNameLength];
    buf.readBytes(measurementNameBytes);
    String measurementName = new String(measurementNameBytes);

    int valueNameLength = buf.readInt();
    if (valueNameLength < 0) {
        return null;
    }
    byte[] valueNameBytes = new byte[valueNameLength];
    buf.readBytes(valueNameBytes);
    String valueFieldName = new String(valueNameBytes);
    long timestamp = buf.readLong();
    byte flag = buf.readByte();
    DataPoint dp;
    if (flag == '0') {
        double value = buf.readDouble();
        dp = new DataPoint(dbName, measurementName, valueFieldName, null, timestamp, value);
        dp.setFp(true);
    } else {
        long value = buf.readLong();
        dp = new DataPoint(dbName, measurementName, valueFieldName, null, timestamp, value);
    }
    return dp;
}