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.github.nettybook.ch4.EchoClientHandler2.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    String sendMessage = "Hello netty";

    ByteBuf messageBuffer = Unpooled.buffer();
    messageBuffer.writeBytes(sendMessage.getBytes());

    StringBuilder builder = new StringBuilder();
    builder.append(" ? [");
    builder.append(sendMessage);/*ww  w  .ja v a2  s.co  m*/
    builder.append("]");

    System.out.println(builder.toString());
    ctx.write(messageBuffer);

    ctx.flush();
}

From source file:com.github.pgasync.impl.netty.ByteBufMessageEncoder.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
    Encoder<Message> encoder = (Encoder<Message>) ENCODERS.get(msg.getClass());

    buffer.clear();// w  ww .j a v a 2  s. com
    ByteBuffer msgbuf = buffer;
    try {
        while (true) {
            try {
                encoder.write(msg, msgbuf);
                break;
            } catch (BufferOverflowException overflow) {
                // large clob/blob, resize buffer aggressively
                msgbuf = ByteBuffer.allocate(msgbuf.capacity() * 4);
            }
        }

        msgbuf.flip();
        out.writeBytes(msgbuf);
    } catch (Throwable t) {
        // broad catch as otherwise the exception is silently dropped
        ctx.fireExceptionCaught(t);
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java

License:Open Source License

/**
 * Writes a string to output buffer using the specified encoding
 * @param input the input string/*from  ww  w  .  j a va 2  s.  c  o m*/
 * @param output the output buffer
 * @param charset the charset to use in order to encode the string
 */
public static void writeStringToOctetString(@Nullable String input, ByteBuf output, Charset charset) {
    if (input == null) {
        output.writeInt(-1);
        return;
    }

    byte[] bytes = input.getBytes(charset);
    output.writeInt(bytes.length);
    output.writeBytes(bytes);
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java

License:Open Source License

/**
 * Writes a byte array to output buffer/*from ww w.ja  v a 2 s. co m*/
 * @param input the input byte array
 * @param output the output buffer
 */
public static void writeBytesToOctetString(@Nullable ByteBuf input, ByteBuf output) {
    if (input == null) {
        output.writeInt(-1);
        return;
    }

    output.writeInt(input.readableBytes());
    output.writeBytes(input);
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_positive_and_utf8_encoded_reads_string_correctly()
        throws Exception {

    byte[] stringBytes = STRING_TO_ENCODE.getBytes(StandardCharsets.UTF_8);
    ByteBuf encodedString = Unpooled.buffer(stringBytes.length + 4);
    encodedString.writeInt(stringBytes.length);
    encodedString.writeBytes(stringBytes);

    String decodedString = ChannelBufferUtils.readOctetStringToString(encodedString, StandardCharsets.UTF_8);

    assertEquals("Decoded string and encoded string are not the same", STRING_TO_ENCODE, decodedString);
    encodedString.release();// ww  w  .j av a2s  . com
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_positive_and_utf16_encoded_reads_string_correctly()
        throws Exception {

    byte[] stringBytes = STRING_TO_ENCODE.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedString = Unpooled.buffer(stringBytes.length + 4);
    encodedString.writeInt(stringBytes.length);
    encodedString.writeBytes(stringBytes);

    String decodedString = ChannelBufferUtils.readOctetStringToString(encodedString, StandardCharsets.UTF_16);

    assertEquals("Decoded string and encoded string are not the same", STRING_TO_ENCODE, decodedString);
    encodedString.release();/*from  w w w  .  j  a va 2  s.  co  m*/
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = InvalidUUIDException.class)
public void testReadUUIDWhenInputIsAMalformedUUIDStringThrowsInvalidUUIDException() throws Exception {
    String UUIDString = UUID.randomUUID().toString().replace('-', '1');
    byte[] stringBytes = UUIDString.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedUUID = Unpooled.buffer(stringBytes.length + 4);
    encodedUUID.writeInt(stringBytes.length);
    encodedUUID.writeBytes(stringBytes);

    try {//from w w  w. java2 s  . c om
        ChannelBufferUtils.readUUID(encodedUUID, StandardCharsets.UTF_16);
    } finally {
        encodedUUID.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadUUIDWhenInputIsAValidUUIDItIsConvertedToTheSameUUID() throws Exception {
    UUID uuid = UUID.randomUUID();
    String UUIDString = uuid.toString();
    byte[] stringBytes = UUIDString.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedUUID = Unpooled.buffer(stringBytes.length + 4);
    encodedUUID.writeInt(stringBytes.length);
    encodedUUID.writeBytes(stringBytes);

    UUID readUUID = ChannelBufferUtils.readUUID(encodedUUID, StandardCharsets.UTF_16);

    assertEquals("Encoded and decoded UUIDs do not match", uuid, readUUID);
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToBytesWhenOctetStringSizeIsPositiveReadsByteBufferCorrectly() throws Exception {

    byte[] bytesToTransfer = { 0x14, 0x13 };
    ByteBuf octetStringBytes = Unpooled.buffer(bytesToTransfer.length + 4);
    octetStringBytes.writeInt(bytesToTransfer.length);
    octetStringBytes.writeBytes(bytesToTransfer);

    ByteBuf readBytes = ChannelBufferUtils.readOctetStringToBytes(octetStringBytes);
    octetStringBytes.readerIndex(4);//from   w w w.j  a va 2s. co  m

    assertEquals("Read bytes and given bytes are not the same", octetStringBytes, readBytes);
    octetStringBytes.release();
    readBytes.release();
}

From source file:com.github.subalakr.yasjl.AutoBenchRowPerf.java

License:Apache License

private void parseResults(String file, String path) throws Exception {
    String response = getResource(file);

    StringBuilder sb = new StringBuilder();
    sb.append("size: " + humanReadableByteCount(response.length(), true) + ",\t\t");
    sb.append("level-depth: " + path.split("/").length + ",\t\t");
    sb.append("time: ");

    JsonPointer[] jsonPointers = { new JsonPointer(path, new JsonPointerCB1() {
        public void call(ByteBuf buf) {
            buf.release();/*from   ww w . java 2 s  .  c  om*/
        }
    }) };
    ByteBuf buf = Unpooled.buffer();
    parser.initialize(buf, jsonPointers);
    buf.writeBytes(response.getBytes());
    long start = System.currentTimeMillis();
    parser.parse();
    long end = System.currentTimeMillis();
    sb.append((end - start) + "ms");
    System.out.println(sb.toString());
}