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:MultiThreadClient.java

License:Apache License

public void run() throws Exception {
    try {/*from w w w  . jav  a2s.  c  om*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelDuplexHandler() {
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        System.out.println(
                                index + "-??:" + msg + "Read:" + Thread.currentThread());
                    }

                    @Override
                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
                            throws Exception {
                        ChannelFuture future = ctx.writeAndFlush(msg, promise);
                        System.out.println(index + "-Write:" + Thread.currentThread());
                        Thread.yield();
                        Thread.yield();
                        future.addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                                System.out.println(index + "-Listener" + "Lintener:"
                                        + Thread.currentThread());
                            }
                        });
                    }
                });

        // Make the connection attempt.
        ChannelFuture f = b.connect(host, port).sync();
        for (int i = 0; i < 10; i++) {
            f.channel().writeAndFlush(Unpooled.wrappedBuffer(new byte[10]));
        }
        // Wait until the connection is closed.

        f.channel().closeFuture();
    } finally {
        //group.shutdownGracefully();
    }
}

From source file:alluxio.client.block.stream.GrpcDataWriterTest.java

License:Apache License

/**
 * Writes chunks of data via the given data writer and returns a checksum for a region of the data
 * written.// ww w  . j a  v a  2  s  . c  o  m
 *
 * @param length the length
 * @param start the start position to calculate the checksum
 * @param end the end position to calculate the checksum
 * @return the checksum
 */
private Future<Long> writeFile(final DataWriter writer, final long length, final long start, final long end)
        throws Exception {
    return EXECUTOR.submit(new Callable<Long>() {
        @Override
        public Long call() throws IOException {
            try {
                long checksum = 0;
                long pos = 0;

                long remaining = length;
                while (remaining > 0) {
                    int bytesToWrite = (int) Math.min(remaining, CHUNK_SIZE);
                    byte[] data = new byte[bytesToWrite];
                    RANDOM.nextBytes(data);
                    ByteBuf buf = Unpooled.wrappedBuffer(data);
                    try {
                        writer.writeChunk(buf);
                    } catch (Exception e) {
                        fail(e.getMessage());
                        throw e;
                    }
                    remaining -= bytesToWrite;

                    for (int i = 0; i < data.length; i++) {
                        if (pos >= start && pos <= end) {
                            checksum += BufferUtils.byteToInt(data[i]);
                        }
                        pos++;
                    }
                }
                return checksum;
            } catch (Throwable throwable) {
                LOG.error("Failed to write file.", throwable);
                fail();
                throw throwable;
            }
        }
    });
}

From source file:alluxio.client.block.stream.NettyPacketReaderTest.java

License:Apache License

/**
 * Sends read responses to the channel./*  www.  j ava 2  s  .  co m*/
 *
 * @param channel the channel
 * @param length the length
 * @param start the start position to calculate the checksum
 * @param end the end position to calculate the checksum
 * @return the checksum
 */
private Future<Long> sendReadResponses(final EmbeddedChannel channel, final long length, final long start,
        final long end) {
    return EXECUTOR.submit(new Callable<Long>() {
        @Override
        public Long call() {
            long checksum = 0;
            long pos = 0;

            long remaining = length;
            while (remaining > 0) {
                int bytesToSend = (int) Math.min(remaining, PACKET_SIZE);
                byte[] data = new byte[bytesToSend];
                RANDOM.nextBytes(data);
                ByteBuf buf = Unpooled.wrappedBuffer(data);
                RPCProtoMessage message = RPCProtoMessage.createOkResponse(new DataNettyBufferV2(buf));
                channel.writeInbound(message);
                remaining -= bytesToSend;

                for (int i = 0; i < data.length; i++) {
                    if (pos >= start && pos <= end) {
                        checksum += BufferUtils.byteToInt(data[i]);
                    }
                    pos++;
                }
            }

            // send EOF.
            channel.writeInbound(RPCProtoMessage.createOkResponse(null));
            return checksum;
        }
    });
}

From source file:alluxio.client.block.stream.NettyPacketWriterTest.java

License:Apache License

/**
 * Writes packets via the given packet writer and returns a checksum for a region of the data
 * written./*from w ww. ja  v a 2s . c om*/
 *
 * @param length the length
 * @param start the start position to calculate the checksum
 * @param end the end position to calculate the checksum
 * @return the checksum
 */
private Future<Long> writeFile(final PacketWriter writer, final long length, final long start, final long end)
        throws Exception {
    return EXECUTOR.submit(new Callable<Long>() {
        @Override
        public Long call() throws IOException {
            try {
                long checksum = 0;
                long pos = 0;

                long remaining = length;
                while (remaining > 0) {
                    int bytesToWrite = (int) Math.min(remaining, PACKET_SIZE);
                    byte[] data = new byte[bytesToWrite];
                    RANDOM.nextBytes(data);
                    ByteBuf buf = Unpooled.wrappedBuffer(data);
                    try {
                        writer.writePacket(buf);
                    } catch (IOException e) {
                        Assert.fail(e.getMessage());
                        throw e;
                    }
                    remaining -= bytesToWrite;

                    for (int i = 0; i < data.length; i++) {
                        if (pos >= start && pos <= end) {
                            checksum += BufferUtils.byteToInt(data[i]);
                        }
                        pos++;
                    }
                }
                return checksum;
            } catch (Throwable throwable) {
                LOG.error("Failed to write file.", throwable);
                Assert.fail();
                throw throwable;
            }
        }
    });
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFileDataWriterTest.java

License:Apache License

@Test(timeout = 1000 * 60)
@Ignore("Flaky test")
public void pos() throws Exception {
    long blockSize = CHUNK_SIZE * 2 + CHUNK_SIZE / 3;
    try (DataWriter writer = create(blockSize, CHUNK_SIZE)) {
        byte[] data = new byte[1];
        Future<WriteSummary> actualUfs = getUfsWrite(mClient);
        for (long pos = 0; pos < blockSize; pos++) {
            assertEquals(pos, writer.pos());
            ByteBuf buf = Unpooled.wrappedBuffer(data);
            writer.writeChunk(buf);//  w  w w. j a  v a 2s  .com
        }
        actualUfs.get();
    }
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFileDataWriterTest.java

License:Apache License

/**
 * Writes chunks via the given data writer and returns a checksum for a region of the data
 * written./*from  w  w  w  .j  a v  a 2s.c  o m*/
 *
 * @param length the length
 * @return the checksum
 */
private Future<WriteSummary> writeData(final DataWriter writer, final long length) throws Exception {
    return EXECUTOR.submit(new Callable<WriteSummary>() {
        @Override
        public WriteSummary call() throws IOException {
            try {
                long checksum = 0;
                long remaining = length;
                while (remaining > 0) {
                    int bytesToWrite = (int) Math.min(remaining, CHUNK_SIZE);
                    byte[] data = new byte[bytesToWrite];
                    RANDOM.nextBytes(data);
                    ByteBuf buf = Unpooled.wrappedBuffer(data);
                    try {
                        writer.writeChunk(buf);
                    } catch (Exception e) {
                        fail(e.getMessage());
                        throw e;
                    }
                    remaining -= bytesToWrite;
                    // TODO(binfan): create a util method to calculate checksum from buffer
                    for (int i = 0; i < data.length; i++) {
                        checksum += BufferUtils.byteToInt(data[i]);
                    }
                }
                return new WriteSummary(length, checksum);
            } catch (Throwable throwable) {
                LOG.error("Failed to write file.", throwable);
                fail("Failed to write file." + throwable.getMessage());
                throw throwable;
            }
        }
    });
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFilePacketWriterTest.java

License:Apache License

@Test(timeout = 1000 * 60)
public void pos() throws Exception {
    long blockSize = PACKET_SIZE * 2 + PACKET_SIZE / 3;
    try (PacketWriter writer = create(blockSize, PACKET_SIZE)) {
        byte[] data = new byte[1];
        Future<WriteSummary> actualUfs = getUfsWrite(mChannel);
        for (long pos = 0; pos < blockSize; pos++) {
            assertEquals(pos, writer.pos());
            ByteBuf buf = Unpooled.wrappedBuffer(data);
            writer.writePacket(buf);/*www. j  a v  a  2  s.  c o  m*/
        }
        actualUfs.get();
    }
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFilePacketWriterTest.java

License:Apache License

/**
 * Writes packets via the given packet writer and returns a checksum for a region of the data
 * written./*from  www  .  ja v a  2  s . co  m*/
 *
 * @param length the length
 * @return the checksum
 */
private Future<WriteSummary> writeData(final PacketWriter writer, final long length) throws Exception {
    return EXECUTOR.submit(new Callable<WriteSummary>() {
        @Override
        public WriteSummary call() throws IOException {
            try {
                long checksum = 0;
                long remaining = length;
                while (remaining > 0) {
                    int bytesToWrite = (int) Math.min(remaining, PACKET_SIZE);
                    byte[] data = new byte[bytesToWrite];
                    RANDOM.nextBytes(data);
                    ByteBuf buf = Unpooled.wrappedBuffer(data);
                    try {
                        writer.writePacket(buf);
                    } catch (Exception e) {
                        fail(e.getMessage());
                        throw e;
                    }
                    remaining -= bytesToWrite;
                    // TODO(binfan): create a util method to calculate checksum from buffer
                    for (int i = 0; i < data.length; i++) {
                        checksum += BufferUtils.byteToInt(data[i]);
                    }
                }
                return new WriteSummary(length, checksum);
            } catch (Throwable throwable) {
                LOG.error("Failed to write file.", throwable);
                fail("Failed to write file." + throwable.getMessage());
                throw throwable;
            }
        }
    });
}

From source file:alluxio.grpc.ReadResponseMarshaller.java

License:Apache License

@Override
protected ByteBuf[] serialize(ReadResponse message) throws IOException {
    DataBuffer chunkBuffer = pollBuffer(message);
    if (chunkBuffer == null) {
        if (!message.hasChunk() || !message.getChunk().hasData()) {
            // nothing to serialize
            return new ByteBuf[0];
        }/*from www . ja  v  a2s .  com*/
        // attempts to fallback to read chunk from message
        chunkBuffer = new NettyDataBuffer(
                Unpooled.wrappedBuffer(message.getChunk().getData().asReadOnlyByteBuffer()));
    }
    int size = message.getSerializedSize() - chunkBuffer.readableBytes();
    byte[] header = new byte[size];
    CodedOutputStream stream = CodedOutputStream.newInstance(header);
    stream.writeTag(ReadResponse.CHUNK_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED);
    stream.writeUInt32NoTag(message.getChunk().getSerializedSize());
    stream.writeTag(Chunk.DATA_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED);
    stream.writeUInt32NoTag(chunkBuffer.readableBytes());
    return new ByteBuf[] { Unpooled.wrappedBuffer(header), (ByteBuf) chunkBuffer.getNettyOutput() };
}

From source file:alluxio.grpc.ReadResponseMarshallerTest.java

License:Apache License

private void validateStream(ReadResponse message) throws IOException {
    ReadResponseMarshaller marshaller = new ReadResponseMarshaller();
    byte[] expected = message.toByteArray();
    if (message.hasChunk() && message.getChunk().hasData()) {
        marshaller.offerBuffer(new NettyDataBuffer(
                Unpooled.wrappedBuffer(message.getChunk().getData().asReadOnlyByteBuffer())), message);
    }//  w  ww . j  a  v a2s.  c  o m
    InputStream stream = marshaller.stream(message);
    assertTrue(stream instanceof Drainable);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ((Drainable) stream).drainTo(outputStream);
    assertArrayEquals(expected, outputStream.toByteArray());
}