Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

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

Usage

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addByte() throws Exception {

    CommandArgs<String, String> args = new CommandArgs<>(codec).add("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();/*from  w  ww. j  av  a 2 s.c o  m*/
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addByteUsingDirectByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(CommandArgs.ExperimentalByteArrayCodec.INSTANCE)
            .add("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();/*from  w  w w  .  j ava2 s .  c  o m*/
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addValueUsingDirectByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(CommandArgs.ExperimentalByteArrayCodec.INSTANCE)
            .addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();/* w ww.j a v  a 2 s  .  c  o  m*/
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addKeyUsingDirectByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(CommandArgs.ExperimentalByteArrayCodec.INSTANCE)
            .addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();//from   ww  w.ja v  a2  s .c  o  m
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addByteUsingByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).add("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();//from   ww  w  . j  a  v  a  2  s.com
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addValueUsingByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();/*from  www . j  a v  a  2  s  .  co  m*/
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addKeyUsingByteCodec() throws Exception {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();/*w  ww  .java  2  s .  c om*/
    args.encode(buffer);

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:com.lambdaworks.redis.server.RandomServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    byte initial[] = new byte[1];
    random.nextBytes(initial);// www. ja v  a  2  s .co m

    byte[] response = new byte[Math.abs((int) initial[0])];

    Arrays.fill(response, "A".getBytes()[0]);

    ByteBuf buf = ctx.alloc().heapBuffer(response.length);

    ByteBuf encoded = buf.writeBytes(response);
    ctx.write(encoded);
}

From source file:com.ldp.nettydemo.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;//ww  w  .  jav a  2s.  c  om
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        sendBuf.writeBytes(ByteObjConverter.ObjectToByte(value));
        //         marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        sendBuf.writeBytes(ByteObjConverter.ObjectToByte(msg.getBody()));
        //         marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

private static DnsRecord newCompatibleAddressRecord(String name, String ipV4Addr) {
    final ByteBuf content = Unpooled.buffer();
    content.writeZero(12);//  w w  w .  j  a  va  2 s . com
    content.writeBytes(NetUtil.createByteArrayFromIpAddressString(ipV4Addr));
    return new DefaultDnsRawRecord(name, AAAA, 60, content);
}