Example usage for io.netty.buffer Unpooled buffer

List of usage examples for io.netty.buffer Unpooled buffer

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

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

License:Apache License

public static ByteBuf serialize(final boolean enveloped, final ZMTPMessage message, int version) {
    final ByteBuf buffer = Unpooled.buffer(ZMTPUtils.messageSize(message, enveloped, version));
    ZMTPUtils.writeMessage(message, buffer, enveloped, 1);
    return buffer;
}

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

License:Apache License

@Test
public void testWriteLongBE() {
    ByteBuf cb = Unpooled.buffer(8);
    cb.order(ByteOrder.BIG_ENDIAN);
    ZMTPUtils.writeLong(cb, 1);
    cmp(cb, 0, 0, 0, 0, 0, 0, 0, 1);
}

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

License:Apache License

@Test
public void testWriteLongLE() {
    ByteBuf cb = Unpooled.buffer(8);
    cb.order(ByteOrder.LITTLE_ENDIAN);
    ZMTPUtils.writeLong(cb, 1);
    cmp(cb, 0, 0, 0, 0, 0, 0, 0, 1);
}

From source file:com.spotify.netty.zmtp.Benchmark.java

License:Apache License

@Ignore("this is a benchmark")
@Test// w w  w  .ja v  a2  s  .  co m
public void benchmarkEncoding() throws ZMTPMessageParsingException {
    final ProgressMeter meter = new ProgressMeter("messages");
    ZMTPMessage message = new ZMTPMessage(
            asList(ZMTPFrame.create("first identity frame"), ZMTPFrame.create("second identity frame")),
            asList(ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"),
                    ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"),
                    ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"),
                    ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata")));
    final ZMTPMessageParser parser = new ZMTPMessageParser(true, 1024 * 1024, 1);
    long sum = 0;
    for (long i = 0; i < 1000000; i++) {
        for (long j = 0; j < 1000; j++) {
            final ByteBuf buffer = Unpooled.buffer(ZMTPUtils.messageSize(message, true, 1));
            ZMTPUtils.writeMessage(message, buffer, true, 1);
            message = parser.parse(buffer).getMessage();

            sum += buffer.readableBytes();
            buffer.release();
        }
        meter.inc(1000, 0);
    }
    System.out.println(sum);
}

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

License:Apache License

@Theory
public void protocolErrorsCauseException(@TestedOn(ints = { 16, 17, 27, 32, 48, 53 }) final int payloadSize)
        throws Exception {
    final Bootstrap b = new Bootstrap();
    b.group(new NioEventLoopGroup());
    b.channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<NioSocketChannel>() {
        @Override/*from  w ww  .j a  v  a 2 s.  c o  m*/
        protected void initChannel(final NioSocketChannel ch) throws Exception {
            ch.pipeline().addLast(new MockHandler());
        }
    });

    final Channel channel = b.connect(serverAddress).awaitUninterruptibly().channel();

    final ByteBuf payload = Unpooled.buffer(payloadSize);
    for (int i = 0; i < payloadSize; i++) {
        payload.writeByte(0);
    }
    channel.writeAndFlush(payload);

    mockHandler.active.get(5, SECONDS);
    mockHandler.exception.get(5, SECONDS);
    mockHandler.inactive.get(5, SECONDS);
    assertFalse(mockHandler.handshaked);
    assertFalse(mockHandler.read);
}

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

/**
 * @param args/*w  w  w.j  a  v  a2s.co m*/
 */
public static void main(String[] args) {
    AuthPacket authPacket1 = new AuthPacket();
    authPacket1.decodeBody(null);

    byte[] bs = new byte[] { 82, 0, 0, 0, 10, 49, 48, 46, 48, 46, 49, 45, 77, 97, 114, 105, 97, 68, 66, 0, -98,
            1, 0, 0, 110, 104, 61, 56, 64, 122, 101, 107, 0, -1, -9, 8, 2, 0, 15, -96, 21, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 105, 78, 41, 35, 111, 43, 39, 124, 98, 82, 87, 60, 0, 109, 121, 115, 113, 108, 95, 110, 97,
            116, 105, 118, 101, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0 };
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);

    byteBuf.setBytes(0, bs);

    HandshakePacket handshakePacket = new HandshakePacket();
    try {
        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);
    } catch (DecodeException e) {
        e.printStackTrace();
    }

    BackendConf backendConf = BackendConf.getInstance();
    BackendServerConf backendServerConf = backendConf.getServers()[0];

    AuthPacket authPacket = new AuthPacket();
    authPacket.charsetIndex = (byte) (handshakePacket.charset & 0xff);
    authPacket.user = backendServerConf.getProps().get("user").getBytes();
    try {
        authPacket.password = getPass(backendServerConf.getProps().get("pwd"), handshakePacket);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    authPacket.passwordLen = (byte) authPacket.password.length;
    authPacket.database = backendServerConf.getProps().get("db").getBytes();
    ByteBuf byteBuf1 = authPacket.encode();
    System.out.println(Arrays.toString(byteBuf1.array()));

}

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

public void decodeBody(ByteBuf _byteBuf) {
    byte[] bs = new byte[] { -117, -93, 2, 0, 0, 0, 0, 64, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 114, 111, 111, 116, 0, 20, -19, -111, -3, 39, -46, -116, -128, -44, -112, -26,
            -48, 42, 70, -85, 8, 83, 83, 100, 103, 68, 116, 97, 108, 101, 110, 116, 95, 98, 97, 115, 101, 119,
            101, 98, 50, 48, 49, 0 };//from ww w . j  a va  2 s . c o  m
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    byteBuf.setBytes(0, bs, 0, bs.length);

    int _index = byteBuf.readerIndex();
    int index = _index;

    clientFlags = byteBuf.getInt(index); //172939
    index += 4;

    maxPacketSize = byteBuf.getInt(index); //1073741824
    index += 4;

    charsetIndex = byteBuf.getByte(index); //33
    index += 1;

    index += extra.length;

    int len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    user = new byte[len];
    byteBuf.getBytes(index, user, 0, len);
    index += len;
    index++;

    passwordLen = byteBuf.getByte(index);
    index += 1;

    password = new byte[passwordLen];
    byteBuf.getBytes(index, password, 0, passwordLen);

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    database = new byte[len];
    byteBuf.getBytes(index, database, 0, len);
    index += len;
    index++;

}

From source file:com.talent.mysql.packet.response.HandshakePacket.java

License:Open Source License

/**
 * @param args//from   w w  w  .  j av a  2s .co  m
 * @throws DecodeException
 */
public static void main(String[] args) {
    byte[] bs = new byte[] { 82, 0, 0, 0, 10, 49, 48, 46, 48, 46, 49, 45, 77, 97, 114, 105, 97, 68, 66, 0, -98,
            1, 0, 0, 110, 104, 61, 56, 64, 122, 101, 107, 0, -1, -9, 8, 2, 0, 15, -96, 21, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 105, 78, 41, 35, 111, 43, 39, 124, 98, 82, 87, 60, 0, 109, 121, 115, 113, 108, 95, 110, 97,
            116, 105, 118, 101, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0 };
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);

    byteBuf.setBytes(0, bs);

    HandshakePacket handshakePacket = new HandshakePacket();
    try {
        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);
    } catch (DecodeException e) {
        e.printStackTrace();
    }

}

From source file:com.tesora.dve.db.mysql.DBTypeBasedUtilsTest.java

License:Open Source License

@Test
public void mysqlReadWriteTest() throws Exception {
    ByteBuf cb = Unpooled.buffer(100).order(ByteOrder.LITTLE_ENDIAN);

    int len;//  w  w  w .ja  v  a2s .c  o m
    for (Pair<MyFieldType, Object> expValue : expValuesMysql) {
        cb.clear();
        len = 0;
        if (expValue.getSecond() instanceof Byte)
            len = 1;
        DataTypeValueFunc dtvf = DBTypeBasedUtils.getMysqlTypeFunc(expValue.getFirst(), len, 0);
        dtvf.writeObject(cb, expValue.getSecond());
        assertEqualData(expValue.getSecond(), dtvf.readObject(cb));
    }
}

From source file:com.tesora.dve.db.mysql.DBTypeBasedUtilsTest.java

License:Open Source License

@Test
public void mysqlConvertTest() throws Exception {
    ByteBuf cb;/* w ww  .j  av a2 s. c  o  m*/

    ListOfPairs<MyFieldType, Object> expValuesConvMysql = new ListOfPairs<MyFieldType, Object>();
    expValuesConvMysql.add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_LONG, new Integer(8765432)));
    expValuesConvMysql.add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_LONGLONG, new Long(8765432)));
    expValuesConvMysql
            .add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_SHORT, new Short((short) 5678)));
    expValuesConvMysql.add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_TINY, new Integer(100)));
    expValuesConvMysql.add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_TINY, new Byte((byte) 1)));
    //      expValuesConvMysql.add(new Pair<MyFieldType, Object>(MyFieldType.FIELD_TYPE_BIT, new Boolean(true)));

    int len;
    for (Pair<MyFieldType, Object> expValue : expValuesConvMysql) {
        len = 0;
        if (expValue.getSecond() instanceof Byte)
            len = 1;

        cb = Unpooled.buffer(100).order(ByteOrder.LITTLE_ENDIAN);
        DataTypeValueFunc dtvf = DBTypeBasedUtils.getMysqlTypeFunc(expValue.getFirst(), len, 0);
        dtvf.writeObject(cb, expValue.getSecond());
        assertEquals(expValue.getSecond(), dtvf.readObject(cb));
    }

}