Example usage for io.netty.buffer ByteBuf duplicate

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

Introduction

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

Prototype

public abstract ByteBuf duplicate();

Source Link

Document

Returns a buffer which shares the whole region of this buffer.

Usage

From source file:io.datty.spring.converter.embedded.SimpleEntityTest.java

License:Apache License

@Test
public void testCrossNull() {

    SimpleEntity entity = new SimpleEntity();
    entity.setId(123L);/*  w  w  w .j  a  v a 2  s  . c  o  m*/

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    Assert.assertNull(rec.get("embedded"));

    SimpleEntity actual = DattyConverterUtil.read(SimpleEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNull(actual.getEmbedded());
}

From source file:io.datty.spring.converter.embedded.SimpleEntityTest.java

License:Apache License

@Test
public void testCrossEmbeddedNull() {

    SimpleEntity entity = new SimpleEntity();
    entity.setId(123L);//from   ww w . ja  v a  2  s.c  om

    EmbeddedEntity embedded = new EmbeddedEntity();
    entity.setEmbedded(embedded);

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    Object embeddedObj = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(embeddedObj);
    Assert.assertTrue(embeddedObj instanceof Map);

    Map embeddedMap = (Map) embeddedObj;
    Assert.assertTrue(embeddedMap.isEmpty());

    SimpleEntity actual = DattyConverterUtil.read(SimpleEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertNull(actual.getEmbedded().getInnerField());

}

From source file:io.datty.spring.converter.embedded.SimpleEntityTest.java

License:Apache License

@Test
public void testCrossEmbedded() {

    SimpleEntity entity = new SimpleEntity();
    entity.setId(123L);/*ww  w. j ava 2  s. c  o  m*/

    EmbeddedEntity embedded = new EmbeddedEntity();
    embedded.setInnerField("inner");
    entity.setEmbedded(embedded);

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    Object embeddedObj = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(embeddedObj);

    Assert.assertTrue(embeddedObj instanceof Map);

    Map embeddedMap = (Map) embeddedObj;
    Assert.assertFalse(embeddedMap.isEmpty());
    Object innerField = embeddedMap.get("innerField");
    Assert.assertNotNull(innerField);
    Assert.assertTrue(innerField instanceof String);
    Assert.assertEquals("inner", innerField);

    SimpleEntity actual = DattyConverterUtil.read(SimpleEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(embedded.getInnerField(), actual.getEmbedded().getInnerField());

}

From source file:io.datty.spring.converter.embedded.StringMapEntityTest.java

License:Apache License

@Test
public void testNull() {

    StringMapEntity entity = new StringMapEntity();
    entity.setId(123L);//from w  w w  .  j av  a 2s .co  m

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    Assert.assertNull(rec.get("embedded"));

    StringMapEntity actual = DattyConverterUtil.read(StringMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNull(actual.getEmbedded());

}

From source file:io.datty.spring.converter.embedded.StringMapEntityTest.java

License:Apache License

@Test
public void testEmbeddedEmpty() {

    StringMapEntity entity = new StringMapEntity();
    entity.setId(123L);//from www.j av  a  2s .  com
    entity.setEmbedded(Collections.<String, EmbeddedEntity>emptyMap());

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<String, Map> map = (Map<String, Map>) value;
    Assert.assertEquals(0, map.size());

    StringMapEntity actual = DattyConverterUtil.read(StringMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(0, actual.getEmbedded().size());

}

From source file:io.datty.spring.converter.embedded.StringMapEntityTest.java

License:Apache License

@Test
public void testEmbeddedNull() {

    StringMapEntity entity = new StringMapEntity();
    entity.setId(123L);//from   www.j av a 2s  .co m

    EmbeddedEntity embedded = new EmbeddedEntity();
    entity.setEmbedded(Collections.singletonMap("k", embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(StringMaps.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<String, Map> map = (Map<String, Map>) value;
    Assert.assertEquals(1, map.size());
    Assert.assertNotNull(map.get("k"));
    Assert.assertNull(map.get("k").get("innerField"));

    StringMapEntity actual = DattyConverterUtil.read(StringMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertNull(actual.getEmbedded().get("k").getInnerField());

}

From source file:io.datty.spring.converter.embedded.StringMapEntityTest.java

License:Apache License

@Test
public void testEmbedded() {

    StringMapEntity entity = new StringMapEntity();
    entity.setId(123L);//from   w  w w . java2 s .  c  o m

    EmbeddedEntity embedded = new EmbeddedEntity();
    embedded.setInnerField("inner");
    entity.setEmbedded(Collections.singletonMap("k", embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(StringMaps.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<String, Map> map = (Map<String, Map>) value;
    Assert.assertEquals(1, map.size());
    Assert.assertNotNull(map.get("k"));
    Assert.assertEquals("inner", map.get("k").get("innerField"));

    StringMapEntity actual = DattyConverterUtil.read(StringMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertEquals("inner", actual.getEmbedded().get("k").getInnerField());

}

From source file:io.liveoak.stomp.client.StompClient.java

License:Open Source License

/**
 * Send a message to the server./*w w  w  . jav a  2s .  c o  m*/
 *
 * @param destination The destination.
 * @param content     The content bytes.
 */
public void send(String destination, ByteBuf content) {
    StompMessage message = new DefaultStompMessage();
    message.destination(destination);
    message.content(content.duplicate().retain());
    send(message);
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

private void testSendFile(Buffer expected, String path, long offset, long length) throws Exception {
    waitFor(2);/*from  w ww  . j  a  v a2  s.com*/
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.bodyEndHandler(v -> {
            assertEquals(resp.bytesWritten(), length);
            complete();
        });
        resp.sendFile(path, offset, length);
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2EventAdapter() {
            Buffer buffer = Buffer.buffer();
            Http2Headers responseHeaders;

            @Override
            public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                    int streamDependency, short weight, boolean exclusive, int padding, boolean endStream)
                    throws Http2Exception {
                responseHeaders = headers;
            }

            @Override
            public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
                    boolean endOfStream) throws Http2Exception {
                buffer.appendBuffer(Buffer.buffer(data.duplicate()));
                if (endOfStream) {
                    vertx.runOnContext(v -> {
                        assertEquals("" + length, responseHeaders.get("content-length").toString());
                        assertEquals(expected, buffer);
                        complete();
                    });
                }
                return data.readableBytes() + padding;
            }
        });
        int id = request.nextStreamId();
        request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:org.apache.activemq.artemis.core.message.impl.MessageImpl.java

License:Apache License

@Override
public synchronized ActiveMQBuffer getBodyBufferDuplicate() {

    // Must copy buffer before sending it

    ByteBuf byteBuf = ChannelBufferWrapper.unwrap(getBodyBuffer().byteBuf());
    byteBuf = byteBuf.duplicate();
    byteBuf.readerIndex(getBodyBuffer().readerIndex());
    byteBuf.writerIndex(getBodyBuffer().writerIndex());

    return new ResetLimitWrappedActiveMQBuffer(BODY_OFFSET, byteBuf, null);
}