Example usage for io.netty.buffer Unpooled buffer

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

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

From source file:com.flysoloing.learning.network.netty.udt.echo.message.MsgEchoClientHandler.java

License:Apache License

public MsgEchoClientHandler() {
    super(false);
    final ByteBuf byteBuf = Unpooled.buffer(MsgEchoClient.SIZE);
    for (int i = 0; i < byteBuf.capacity(); i++) {
        byteBuf.writeByte((byte) i);
    }/*  ww  w. jav  a  2s  .c  om*/
    message = new UdtMessage(byteBuf);
}

From source file:com.flysoloing.learning.network.netty.udt.echo.rendezvous.MsgEchoPeerHandler.java

License:Apache License

public MsgEchoPeerHandler(final int messageSize) {
    super(false);
    final ByteBuf byteBuf = Unpooled.buffer(messageSize);
    for (int i = 0; i < byteBuf.capacity(); i++) {
        byteBuf.writeByte((byte) i);
    }/*  w ww.  j av  a2s . c o  m*/
    message = new UdtMessage(byteBuf);
}

From source file:com.flysoloing.learning.network.netty.udt.echo.rendezvousBytes.ByteEchoPeerHandler.java

License:Apache License

public ByteEchoPeerHandler(final int messageSize) {
    super(false);
    message = Unpooled.buffer(messageSize);
    for (int i = 0; i < message.capacity(); i++) {
        message.writeByte((byte) i);
    }/*from  w ww.  j a  v  a2 s  .co  m*/
}

From source file:com.ghrum.common.protocol.MessageLookupService.java

License:Apache License

/**
 * Encodes a {@link Message} into a stream
 *
 * @param message the message to encode to the buffer
 * @return a wrapped buffer that contains the header and the body of the message
 * @throws java.io.IOException/*from w w  w. j  a  v a  2s.c o  m*/
 */
@SuppressWarnings("unchecked")
protected <T extends Message> ByteBuf encode(T message) throws IOException {
    final MessageCodec<Message> codec = (MessageCodec<Message>) getCodec(message.getClass());
    if (codec == null) {
        throw new IOException("Unknown operation class: " + message.getClass());
    }
    final ByteBuf body = codec.encode(message);
    final ByteBuf header = Unpooled.buffer(3).writeByte(codec.getOpcode()).writeShort(body.capacity());
    return Unpooled.wrappedBuffer(header, body);
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Method to easily create a request.//from  w w w.j  a  v a  2 s  . c om
 * @param httpMethod the {@link HttpMethod} desired.
 * @param uri string representation of the desired URI.
 * @param headers any associated headers as a {@link HttpHeaders} object. Can be null.
 * @param content the content that accompanies the request. Can be null.
 * @return A {@link FullHttpRequest} object that defines the request required by the input.
 */
private FullHttpRequest buildRequest(HttpMethod httpMethod, String uri, HttpHeaders headers,
        ByteBuffer content) {
    ByteBuf contentBuf;
    if (content != null) {
        contentBuf = Unpooled.wrappedBuffer(content);
    } else {
        contentBuf = Unpooled.buffer(0);
    }
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, uri, contentBuf);
    if (headers != null) {
        httpRequest.headers().set(headers);
    }
    return httpRequest;
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

@Test
public void healtCheckRequestTest() throws ExecutionException, InterruptedException, IOException {
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
            "/healthCheck", Unpooled.buffer(0));
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
    ByteBuffer content = getContent(response, responseParts);
    assertEquals("GET content does not match original content", "GOOD", new String(content.array()));
}

From source file:com.github.gregwhitaker.requestreply.Client.java

License:Apache License

public void start() throws Exception {
    Publisher<ClientTcpDuplexConnection> publisher = ClientTcpDuplexConnection.create(remoteAddress,
            new NioEventLoopGroup(1));

    ClientTcpDuplexConnection duplexConnection = RxReactiveStreams.toObservable(publisher).toBlocking().last();
    ReactiveSocket reactiveSocket = DefaultReactiveSocket.fromClientConnection(duplexConnection,
            ConnectionSetupPayload.create("UTF-8", "UTF-8"), t -> t.printStackTrace());

    reactiveSocket.startAndWait();/*from   w  ww.ja v  a  2s .  com*/

    // Create an observable that emits messages at a specific interval.
    Publisher<Payload> requestStream = RxReactiveStreams.toPublisher(
            Observable.interval(1_000, TimeUnit.MILLISECONDS).onBackpressureDrop().map(i -> new Payload() {
                @Override
                public ByteBuffer getData() {
                    return ByteBuffer.wrap(("YO " + i).getBytes());
                }

                @Override
                public ByteBuffer getMetadata() {
                    return Frame.NULL_BYTEBUFFER;
                }
            }));

    final CountDownLatch latch = new CountDownLatch(Integer.MAX_VALUE);

    requestStream.subscribe(new Subscriber<Payload>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(Payload payload) {
            ByteBuf buffer = Unpooled.buffer(payload.getData().capacity());
            buffer.writeBytes(payload.getData());

            byte[] bytes = new byte[buffer.capacity()];
            buffer.readBytes(bytes);

            System.out.println("Client Sent: " + new String(bytes));

            reactiveSocket.requestResponse(payload).subscribe(new Subscriber<Payload>() {
                @Override
                public void onSubscribe(Subscription s) {
                    s.request(Long.MAX_VALUE);
                }

                @Override
                public void onNext(Payload payload) {
                    ByteBuf buffer = Unpooled.buffer(payload.getData().capacity());
                    buffer.writeBytes(payload.getData());

                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.readBytes(bytes);

                    System.out.println("Client Received: " + new String(bytes));

                    latch.countDown();
                }

                @Override
                public void onError(Throwable t) {
                    latch.countDown();
                }

                @Override
                public void onComplete() {
                    latch.countDown();
                }
            });
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

        }
    });

    latch.await();
    System.exit(0);
}

From source file:com.github.mrstampy.pprspray.core.receiver.AbstractChunkReceiver.java

License:Open Source License

/**
 * Rehydrate and transform./*from   w w w  . j  ava2 s .c om*/
 *
 * @param array
 *          the array
 * @return the byte[]
 * @see #setTransformer(MediaTransformer)
 */
protected byte[] rehydrateAndTransform(Set<AMC> set) {
    int size = calcSize(set);
    ByteBuf buf = Unpooled.buffer(size);

    for (AMC chunk : set) {
        buf.writeBytes(chunk.getData());
    }

    return transform(buf.array());
}

From source file:com.github.mrstampy.pprspray.core.streamer.footer.MediaFooter.java

License:Open Source License

private byte[] buildFooter() {
    ByteBuf buf = Unpooled.buffer(MediaStreamerUtils.FOOTER_LENGTH);

    buf.writeBytes(getType().eomBytes());
    buf.writeInt(getMessageHash());/* w  ww.j a va 2 s . co  m*/
    buf.writeInt(getMediaHash());

    return buf.array();
}

From source file:com.github.mrstampy.pprspray.core.streamer.MediaStreamType.java

License:Open Source License

private void setEomBytes() {
    int eomOrdinal = Integer.MAX_VALUE - ordinal();

    ByteBuf buf = Unpooled.buffer(4);
    buf.writeInt(eomOrdinal);
    eomBytes = buf.array();
}