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.mesosphere.mesos.rx.java.test.simulation.MesosServerSimulationScenariosTest.java

License:Apache License

@NotNull
private static byte[] bufToBytes(@NotNull final ByteBuf buf) {
    final int i = buf.readableBytes();
    final byte[] data = new byte[i];
    buf.readBytes(data);
    return data;/*  ww  w  . j  a  v a 2  s .c o m*/
}

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testEncodedLenthTwoBytes() {
    int length = 128;
    byte[] expected = new byte[] { (byte) 0x80, 0x01 };

    ByteBuf buf = MQParser.getBuffer(length);
    assertEquals(3, buf.readableBytes());
    assertEquals(0, buf.readByte());//from   w  w w .  j a va 2 s  . co m

    byte[] actual = new byte[buf.readableBytes()];
    buf.readBytes(actual);
    assertArrayEquals(expected, actual);
}

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testEncodedLenthThreeBytes() {
    int length = 16384;
    byte[] expected = new byte[] { (byte) 0x80, (byte) 0x80, 0x01 };

    ByteBuf buf = MQParser.getBuffer(length);
    assertEquals(4, buf.readableBytes());
    assertEquals(0, buf.readByte());// w ww  .  j av a2  s . c  om

    byte[] actual = new byte[buf.readableBytes()];
    buf.readBytes(actual);
    assertArrayEquals(expected, actual);
}

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testEncodedLenthFourBytes() {
    int length = 2097152;
    byte[] expected = new byte[] { (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01 };

    ByteBuf buf = MQParser.getBuffer(length);
    assertEquals(5, buf.readableBytes());
    assertEquals(0, buf.readByte());/*from   w  ww .  j  a v  a 2 s.c  o  m*/

    byte[] actual = new byte[buf.readableBytes()];
    buf.readBytes(actual);
    assertArrayEquals(expected, actual);
}

From source file:com.mobius.software.mqtt.parser.test.TestPublish.java

License:Open Source License

@Test
public void testTime() throws UnsupportedEncodingException, MalformedMessageException {
    Publish actual = new Publish(20,
            new Topic(new Text("root/first/second/third/fourth/fifth/andsomeadditionalsymbols"),
                    QoS.EXACTLY_ONCE),/*  w  ww.j  av a 2  s  .  c o  m*/
            Unpooled.buffer().writeBytes("1234567890-=qwertyuiop[]asdfghjkl".getBytes()), false, false);
    ByteBuf buf = MQParser.encode(actual);
    buf = MQParser.encode(actual);
    ByteBuf slice = MQParser.next(buf);
    buf.readBytes(slice);
    MQParser.decode(slice);
}

From source file:com.mpush.api.protocol.Packet.java

License:Apache License

public static Packet decodePacket(Packet packet, ByteBuf in, int bodyLength) {
    packet.cc = in.readShort();//read cc
    packet.flags = in.readByte();//read flags
    packet.sessionId = in.readInt();//read sessionId
    packet.lrc = in.readByte();//read lrc

    //read body/*  ww w. j a v  a2s .c  o  m*/
    if (bodyLength > 0) {
        in.readBytes(packet.body = new byte[bodyLength]);
    }
    return packet;
}

From source file:com.mpush.common.message.ByteBufMessage.java

License:Apache License

@Override
public byte[] encode() {
    ByteBuf body = connection.getChannel().alloc().heapBuffer();
    try {/*from  www  .jav a  2 s.c  om*/
        encode(body);
        byte[] bytes = new byte[body.readableBytes()];
        body.readBytes(bytes);
        return bytes;
    } finally {
        body.release();
    }
}

From source file:com.mpush.common.message.ByteBufMessage.java

License:Apache License

public byte[] decodeBytes(ByteBuf body) {
    int fieldLength = body.readShort();
    if (fieldLength == 0)
        return null;
    if (fieldLength == Short.MAX_VALUE) {
        fieldLength += body.readInt();//from  w  w  w. ja v a 2  s.  c o  m
    }
    byte[] bytes = new byte[fieldLength];
    body.readBytes(bytes);
    return bytes;
}

From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;// ?
    }//w ww .  j  a  v a 2  s.  c om

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();// ?Header?
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");// keyUTF-8String
            attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    // Header End
    if (frame.readableBytes() > 4) {
        message.setBody(marshallingDecoder.decode(frame));// 4Data
    }
    message.setHeader(header);
    return message;
}

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

void getFrames(ByteBuf in, List<byte[]> out) {
    while (true) {
        in.markReaderIndex();/* w ww .  j av  a2s  .c  o  m*/
        final int stx = in.readerIndex();

        if (hasFrame(in) == false) {
            in.resetReaderIndex();
            break;
        }

        int length = in.readerIndex() - stx;
        byte[] readBytes = new byte[length];

        in.resetReaderIndex();
        in.readBytes(readBytes);
        out.add(readBytes);
    }
}