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:com.spotify.netty4.handler.codec.zmtp.ZMTPWriterTest.java

License:Apache License

@Test
public void testTwoFrames() throws Exception {
    final ZMTPWriter writer = ZMTPWriter.create(ZMTP10);
    final ByteBuf buf = Unpooled.buffer();
    writer.reset(buf);/*from  w  ww  .j av  a 2  s. c  o m*/

    final ByteBuf f0 = copiedBuffer("hello ", UTF_8);
    final ByteBuf f1 = copiedBuffer("hello ", UTF_8);

    writer.frame(f0.readableBytes(), true).writeBytes(f0.duplicate());
    writer.frame(f1.readableBytes(), false).writeBytes(f1.duplicate());

    final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder());
    decoder.decode(null, buf, out);

    assertThat(out, hasSize(1));
    assertThat(out, contains((Object) asList(f0, f1)));
}

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

License:Apache License

@Test
public void testReframe() throws Exception {
    final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder());
    final ZMTPWriter writer = ZMTPWriter.create(ZMTP10);
    final ByteBuf buf = Unpooled.buffer();

    writer.reset(buf);//from   w  ww . j a v a  2s  .co  m

    // Request a frame with margin in anticipation of a larger payload...
    // ... but write a smaller payload
    final ByteBuf content = copiedBuffer("hello world", UTF_8);
    writer.frame(content.readableBytes() * 2, true).writeBytes(content.duplicate());

    // And rewrite the frame accordingly
    writer.reframe(content.readableBytes(), false);

    // Verify that the message can be parsed
    decoder.decode(null, buf, out);
    assertThat(out, hasSize(1));
    assertThat(out, contains((Object) singletonList(content)));

    // Write and verify another message
    final ByteBuf next = copiedBuffer("next", UTF_8);
    writer.frame(next.readableBytes(), false).writeBytes(next.duplicate());

    out.clear();
    decoder.decode(null, buf, out);
    assertThat(out, hasSize(1));
    assertThat(out, contains((Object) singletonList(next)));
}

From source file:com.tcy.app.netty4.Ne4ClientHandler.java

License:Apache License

public void sendMessage(Object object) {
    ByteBuf buf = ctx.alloc().buffer();
    buf.setByte(0, 123456);//from   w  ww.ja  va 2  s  .c  o  m
    ctx.writeAndFlush(buf.duplicate().retain()).addListener(trafficGenerator);

}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testHttpDataFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf data = Unpooled.buffer(1024);
    for (int i = 0; i < 256; i++) {
        data.writeInt(RANDOM.nextInt());
    }//w ww.  j  a  v  a  2 s  .c  o m
    ByteBuf frame = releaseLater(ENCODER.encodeDataFrame(streamId, false, data.duplicate()));
    assertDataFrame(frame, streamId, false, data);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testHttpHeadersFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    boolean exclusive = RANDOM.nextBoolean();
    int dependency = RANDOM.nextInt() & 0x7FFFFFFF;
    int weight = (RANDOM.nextInt() & 0xFF) + 1;
    ByteBuf headerBlock = Unpooled.buffer(1024);
    for (int i = 0; i < 256; i++) {
        headerBlock.writeInt(RANDOM.nextInt());
    }//from  w ww .j a va 2  s  .  c  om
    ByteBuf frame = releaseLater(ENCODER.encodeHeadersFrame(streamId, false, exclusive, dependency, weight,
            headerBlock.duplicate()));
    assertHeadersFrame(frame, streamId, exclusive, dependency, weight, false, headerBlock);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testContinuedHttpHeadersFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    boolean exclusive = RANDOM.nextBoolean();
    int dependency = RANDOM.nextInt() & 0x7FFFFFFF;
    int weight = (RANDOM.nextInt() & 0xFF) + 1;
    ByteBuf headerBlock = Unpooled.buffer(2 * HTTP_MAX_LENGTH);
    for (int i = 0; i < 2 * HTTP_MAX_LENGTH; i++) {
        headerBlock.writeByte(RANDOM.nextInt());
    }/*from w w w  .  j a  v a2s.c  om*/
    ByteBuf frame = releaseLater(ENCODER.encodeHeadersFrame(streamId, false, exclusive, dependency, weight,
            headerBlock.duplicate()));
    assertHeadersFrame(frame, streamId, exclusive, dependency, weight, false, headerBlock);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf headerBlock = Unpooled.buffer(1024);
    for (int i = 0; i < 256; i++) {
        headerBlock.writeInt(RANDOM.nextInt());
    }//from ww w.ja  v  a 2 s. c  o m
    ByteBuf frame = releaseLater(
            ENCODER.encodePushPromiseFrame(streamId, promisedStreamId, headerBlock.duplicate()));
    assertPushPromiseFrame(frame, streamId, promisedStreamId, headerBlock);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testContinuedHttpPushPromiseFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf headerBlock = Unpooled.buffer(2 * HTTP_MAX_LENGTH);
    for (int i = 0; i < 2 * HTTP_MAX_LENGTH; i++) {
        headerBlock.writeByte(RANDOM.nextInt());
    }/*ww  w  . jav  a  2s  .  c  o m*/
    ByteBuf frame = releaseLater(
            ENCODER.encodePushPromiseFrame(streamId, promisedStreamId, headerBlock.duplicate()));
    assertPushPromiseFrame(frame, streamId, promisedStreamId, headerBlock);
}

From source file:de.unipassau.isl.evs.ssh.core.network.handler.CrypterTest.java

License:Open Source License

public void testRoundtrip() {
    ByteBuf buf = channel.alloc().buffer();
    for (int c : new int[] { 1, 1, 5, 10, 50, 100, 500, 1000, 5000, 10000 }) {
        buf.capacity(c);/*from   w  ww.  ja v  a2s .  c  o m*/
        while (buf.writableBytes() > 0) {
            buf.writeByte(c);
        }

        channel.writeOutbound(buf.duplicate().retain());
        for (ByteBuf msg; (msg = channel.readOutbound()) != null;) {
            assertNotSame(buf, msg);
            channel.writeInbound(msg);
        }
        assertEquals(buf, channel.readInbound());
    }
    ReferenceCountUtil.release(buf);
}

From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignerTest.java

License:Open Source License

public void testRoundtrip() {
    ByteBuf buf = channel.alloc().buffer();
    for (int c : new int[] { 1, 1, 5, 10, 50, 100, 500, 1000, 5000, 10000 }) {
        buf.capacity(c);/*  w  w  w . ja v  a2  s  . c  o m*/
        while (buf.writableBytes() > 0) {
            buf.writeByte(c);
        }

        channel.writeOutbound(buf.duplicate().retain());
        channel.runPendingTasks();
        for (Object msg; (msg = channel.readOutbound()) != null;) {
            channel.writeInbound(msg);
        }
        assertEquals(buf, channel.readInbound());
    }
    ReferenceCountUtil.release(buf);
}