Example usage for io.netty.buffer Unpooled wrappedBuffer

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

Introduction

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

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:io.atomix.cluster.messaging.impl.MessageDecoderTest.java

License:Apache License

@Test
public void testReadStringFromHeapBuffer() throws Exception {
    String payload = "huuhaa";
    ByteBuf byteBuf = Unpooled.wrappedBuffer(payload.getBytes(StandardCharsets.UTF_8));
    try {/*from w w  w  .  j a va  2 s. c o  m*/
        assertEquals(payload, MessageDecoder.readString(byteBuf, payload.length(), StandardCharsets.UTF_8));
    } finally {
        byteBuf.release();
    }
    byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
    byteBuf = Unpooled.buffer(4 + bytes.length);
    try {
        byteBuf.writeInt(1);
        byteBuf.writeBytes(bytes);
        byteBuf.readInt();
        assertEquals(payload, MessageDecoder.readString(byteBuf, payload.length(), StandardCharsets.UTF_8));
    } finally {
        byteBuf.release();
    }
}

From source file:io.cettia.asity.bridge.netty4.NettyServerHttpExchange.java

License:Apache License

@Override
protected void doWrite(ByteBuffer byteBuffer) {
    ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.wrappedBuffer(byteBuffer));
    if (!written) {
        written = true;/*from  w  w  w  .j av a  2 s. co  m*/
        context.write(response);
    }
    context.writeAndFlush(buf);
}

From source file:io.cettia.asity.bridge.netty4.NettyServerWebSocket.java

License:Apache License

@Override
protected void doSend(ByteBuffer byteBuffer) {
    context.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(byteBuffer)));
}

From source file:io.crate.protocols.postgres.types.BasePGTypeTest.java

License:Apache License

@SuppressWarnings("unchecked")
private void assertBytesRead(byte[] value, T expectedValue, int pos, FormatCodes.FormatCode formatCode) {
    ByteBuf buffer = Unpooled.wrappedBuffer(value);
    T readValue;/*  w ww .  j a va  2  s.  c om*/
    if (formatCode == FormatCodes.FormatCode.BINARY) {
        readValue = (T) pgType.readBinaryValue(buffer, pos);
    } else {
        readValue = (T) pgType.readTextValue(buffer, pos);
    }
    buffer.release();
    assertThat(readValue, is(expectedValue));
}

From source file:io.datty.aerospike.support.AerospikeValueUtil.java

License:Apache License

public static ByteBuf toByteBuf(Object aerospikeValue) {
    if (aerospikeValue instanceof byte[]) {
        return Unpooled.wrappedBuffer((byte[]) aerospikeValue);
    } else if (aerospikeValue instanceof String) {
        return asByteBuf((String) aerospikeValue);
    } else {//from   w w w  .j  a va 2s  . co m
        return asByteBuf(aerospikeValue.toString());
    }
}

From source file:io.datty.aerospike.support.AerospikeValueUtil.java

License:Apache License

public static ByteBuf asByteBuf(String str) {
    return Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8));
}

From source file:io.datty.api.DattyRecordIOTest.java

License:Apache License

@Test
public void testOne() {

    ByteBuf value = Unpooled.wrappedBuffer("value".getBytes());

    DattyRecord rec = new DattyRecord();
    rec.put(minorKey, new ByteBufValue(value));

    ByteBuf sink = Unpooled.buffer();/*from   w  ww  . ja v  a  2s .co  m*/

    sink = DattyRecordIO.writeRecord(rec, sink);

    //System.out.println(Arrays.toString(ByteBufUtil.getBytes(sink)));

    DattyRecord actual = DattyRecordIO.readRecord(sink);

    Assert.assertFalse(actual.isEmpty());
    Assert.assertEquals(1, actual.size());

    assertValue("value", actual, minorKey);
}

From source file:io.datty.api.DattyRecordIOTest.java

License:Apache License

@Test
public void testTwo() {

    ByteBuf value = Unpooled.wrappedBuffer("value".getBytes());
    ByteBuf value2 = Unpooled.wrappedBuffer("value2".getBytes());

    DattyRecord rec = new DattyRecord();
    rec.put(minorKey, new ByteBufValue(value));
    rec.put("minorKey2", new ByteBufValue(value2));

    ByteBuf sink = Unpooled.buffer();/*from   w w w  . jav a2 s .com*/

    sink = DattyRecordIO.writeRecord(rec, sink);

    //System.out.println(Arrays.toString(ByteBufUtil.getBytes(sink)));

    DattyRecord actual = DattyRecordIO.readRecord(sink);

    Assert.assertFalse(actual.isEmpty());
    Assert.assertEquals(2, actual.size());

    assertValue("value", actual, minorKey);
    assertValue("value2", actual, "minorKey2");

}

From source file:io.datty.msgpack.core.reader.ByteBufReader.java

License:Apache License

public ByteBuf wrap(String str) {
    return Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8));
}

From source file:io.datty.msgpack.test.CompositeByteBufTest.java

License:Apache License

@Test
public void testTwoComponents() {

    ByteBuf first = Unpooled.wrappedBuffer("a".getBytes());
    ByteBuf second = Unpooled.wrappedBuffer("b".getBytes());

    CompositeByteBuf result = first.alloc().compositeBuffer();
    result.addComponent(true, first);//  ww w .  j a v a 2  s  . co  m
    result.addComponent(true, second);

    byte[] actual = ByteBufUtil.getBytes(result);

    Assert.assertEquals(2, actual.length);
    Assert.assertEquals('a', actual[0]);
    Assert.assertEquals('b', actual[1]);

}