Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

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

License:Apache License

/**
 * Verifies the packets written. After receiving the last packet, it will also send an EOF to
 * the channel./*from w ww  . java2s .c  o m*/
 *
 * @param checksumStart the start position to calculate the checksum
 * @param checksumEnd the end position to calculate the checksum
 * @return the checksum of the data read starting from checksumStart
 */
private Future<Long> verifyWriteRequests(final EmbeddedChannel channel, final long checksumStart,
        final long checksumEnd) {
    return EXECUTOR.submit(new Callable<Long>() {
        @Override
        public Long call() {
            try {
                long checksum = 0;
                long pos = 0;
                while (true) {
                    RPCProtoMessage request = (RPCProtoMessage) CommonUtils.waitForResult("wrtie request",
                            new Function<Void, Object>() {
                                @Override
                                public Object apply(Void v) {
                                    return channel.readOutbound();
                                }
                            }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));
                    validateWriteRequest(request.getMessage().<Protocol.WriteRequest>getMessage(), pos);

                    DataBuffer buffer = request.getPayloadDataBuffer();
                    // Last packet.
                    if (buffer == null) {
                        channel.writeInbound(RPCProtoMessage.createOkResponse(null));
                        return checksum;
                    }
                    try {
                        Assert.assertTrue(buffer instanceof DataNettyBufferV2);
                        ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                        while (buf.readableBytes() > 0) {
                            if (pos >= checksumStart && pos <= checksumEnd) {
                                checksum += BufferUtils.byteToInt(buf.readByte());
                            } else {
                                buf.readByte();
                            }
                            pos++;
                        }
                    } finally {
                        buffer.release();
                    }
                }
            } catch (Throwable throwable) {
                LOG.error("Failed to verify write requests.", throwable);
                Assert.fail();
                throw throwable;
            }
        }
    });
}

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

License:Apache License

/**
 * Verifies the packets written. After receiving the last packet, it will also send an EOF to
 * the channel./*from   w  w  w.  j a v  a 2s  .  com*/
 *
 * @return the checksum of the data read starting from checksumStart
 */
private Future<WriteSummary> getUfsWrite(final EmbeddedChannel channel) {
    return EXECUTOR.submit(new Callable<WriteSummary>() {
        @Override
        public WriteSummary call() throws TimeoutException, InterruptedException {
            try {
                long checksum = 0;
                long pos = 0;
                long len = 0;
                while (true) {
                    RPCProtoMessage request = (RPCProtoMessage) CommonUtils.waitForResult("write request",
                            () -> channel.readOutbound(),
                            WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
                    Protocol.WriteRequest writeRequest = request.getMessage().asWriteRequest();
                    validateWriteRequest(writeRequest, pos);
                    DataBuffer buffer = request.getPayloadDataBuffer();
                    // Last packet.
                    if (writeRequest.hasEof() && writeRequest.getEof()) {
                        assertTrue(buffer == null);
                        channel.writeInbound(RPCProtoMessage.createOkResponse(null));
                        return new WriteSummary(len, checksum);
                    }
                    // UFS block init
                    if (writeRequest.getCreateUfsBlockOptions().hasBytesInBlockStore()) {
                        assertTrue(buffer == null);
                        pos += writeRequest.getCreateUfsBlockOptions().getBytesInBlockStore();
                        continue;
                    }
                    try {
                        Assert.assertTrue(buffer instanceof DataNettyBufferV2);
                        ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                        while (buf.readableBytes() > 0) {
                            checksum += BufferUtils.byteToInt(buf.readByte());
                            pos++;
                            len++;
                        }
                    } finally {
                        buffer.release();
                    }
                }
            } catch (Throwable throwable) {
                fail("Failed to verify write requests." + throwable.getMessage());
                throw throwable;
            }
        }
    });
}

From source file:alluxio.worker.netty.DataServerReadHandlerTest.java

License:Apache License

/**
 * Checks all the read responses./* w w  w  .ja v a  2  s  . c om*/
 */
protected void checkAllReadResponses(EmbeddedChannel channel, long checksumExpected) {
    boolean eof = false;
    long checksumActual = 0;
    while (!eof) {
        Object readResponse = waitForOneResponse(channel);
        if (readResponse == null) {
            Assert.fail();
            break;
        }
        DataBuffer buffer = checkReadResponse(readResponse, Protocol.Status.Code.OK);
        eof = buffer == null;
        if (buffer != null) {
            if (buffer instanceof DataNettyBufferV2) {
                ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                while (buf.readableBytes() > 0) {
                    checksumActual += BufferUtils.byteToInt(buf.readByte());
                }
                buf.release();
            } else {
                Assert.assertTrue(buffer instanceof DataFileChannel);
                ByteBuffer buf = buffer.getReadOnlyByteBuffer();
                byte[] array = new byte[buf.remaining()];
                buf.get(array);
                for (int i = 0; i < array.length; i++) {
                    checksumActual += BufferUtils.byteToInt(array[i]);
                }
            }
        }
    }
    Assert.assertEquals(checksumExpected, checksumActual);
    Assert.assertTrue(eof);
}

From source file:alluxio.worker.netty.ReadHandlerTest.java

License:Apache License

/**
 * Checks all the read responses./*w  w  w. j ava 2s  .  co  m*/
 */
protected void checkAllReadResponses(EmbeddedChannel channel, long checksumExpected) {
    boolean eof = false;
    long checksumActual = 0;
    while (!eof) {
        Object readResponse = waitForOneResponse(channel);
        if (readResponse == null) {
            Assert.fail();
            break;
        }
        DataBuffer buffer = checkReadResponse(readResponse, PStatus.OK);
        eof = buffer == null;
        if (buffer != null) {
            if (buffer instanceof DataNettyBufferV2) {
                ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                while (buf.readableBytes() > 0) {
                    checksumActual += BufferUtils.byteToInt(buf.readByte());
                }
                buf.release();
            } else {
                Assert.assertTrue(buffer instanceof DataFileChannel);
                final ByteBuffer byteBuffer = ByteBuffer.allocate((int) buffer.getLength());
                WritableByteChannel writableByteChannel = new WritableByteChannel() {
                    @Override
                    public boolean isOpen() {
                        return true;
                    }

                    @Override
                    public void close() throws IOException {
                    }

                    @Override
                    public int write(ByteBuffer src) throws IOException {
                        int sz = src.remaining();
                        byteBuffer.put(src);
                        return sz;
                    }
                };
                try {
                    ((FileRegion) buffer.getNettyOutput()).transferTo(writableByteChannel, 0);
                } catch (IOException e) {
                    Assert.fail();
                }
                byteBuffer.flip();
                while (byteBuffer.remaining() > 0) {
                    checksumActual += BufferUtils.byteToInt(byteBuffer.get());
                }
            }
        }
    }
    Assert.assertEquals(checksumExpected, checksumActual);
    Assert.assertTrue(eof);
}

From source file:appeng.core.sync.packets.PacketAssemblerAnimation.java

License:Open Source License

public PacketAssemblerAnimation(final ByteBuf stream) throws IOException {
    this.x = stream.readInt();
    this.y = stream.readInt();
    this.z = stream.readInt();
    this.rate = stream.readByte();
    this.is = AEItemStack.loadItemStackFromPacket(stream);
}

From source file:appeng.core.sync.packets.PacketClick.java

License:Open Source License

public PacketClick(final ByteBuf stream) {
    this.x = stream.readInt();
    this.y = stream.readInt();
    this.z = stream.readInt();
    byte side = stream.readByte();
    if (side != -1) {
        this.side = EnumFacing.values()[side];
    } else {/*from   w  w w .  ja  v  a  2 s  .c o m*/
        this.side = null;
    }
    this.hitX = stream.readFloat();
    this.hitY = stream.readFloat();
    this.hitZ = stream.readFloat();
    this.hand = EnumHand.values()[stream.readByte()];
}

From source file:appeng.core.sync.packets.PacketCompressedNBT.java

License:Open Source License

public PacketCompressedNBT(final ByteBuf stream) throws IOException {
    this.data = null;
    this.compressFrame = null;

    final GZIPInputStream gzReader = new GZIPInputStream(new InputStream() {

        @Override//from   w  w w .j av a2  s. c  om
        public int read() throws IOException {
            if (stream.readableBytes() <= 0) {
                return -1;
            }

            return stream.readByte() & 0xff;
        }
    });

    final DataInputStream inStream = new DataInputStream(gzReader);
    this.in = CompressedStreamTools.read(inStream);
    inStream.close();
}

From source file:appeng.core.sync.packets.PacketMatterCannon.java

License:Open Source License

public PacketMatterCannon(final ByteBuf stream) {
    this.x = stream.readFloat();
    this.y = stream.readFloat();
    this.z = stream.readFloat();
    this.dx = stream.readFloat();
    this.dy = stream.readFloat();
    this.dz = stream.readFloat();
    this.len = stream.readByte();
}

From source file:appeng.core.sync.packets.PacketMEInventoryUpdate.java

License:Open Source License

public PacketMEInventoryUpdate(final ByteBuf stream) throws IOException {
    this.data = null;
    this.compressFrame = null;
    this.list = new LinkedList<IAEItemStack>();
    this.ref = stream.readByte();

    // int originalBytes = stream.readableBytes();

    final GZIPInputStream gzReader = new GZIPInputStream(new InputStream() {
        @Override/*from   w  ww .  j ava 2 s  .c  om*/
        public int read() throws IOException {
            if (stream.readableBytes() <= 0) {
                return -1;
            }

            return stream.readByte() & STREAM_MASK;
        }
    });

    final ByteBuf uncompressed = Unpooled.buffer(stream.readableBytes());
    final byte[] tmp = new byte[TEMP_BUFFER_SIZE];
    while (gzReader.available() != 0) {
        final int bytes = gzReader.read(tmp);
        if (bytes > 0) {
            uncompressed.writeBytes(tmp, 0, bytes);
        }
    }
    gzReader.close();

    // int uncompressedBytes = uncompressed.readableBytes();
    // AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes );

    while (uncompressed.readableBytes() > 0) {
        this.list.add(AEItemStack.loadItemStackFromPacket(uncompressed));
    }

    this.empty = this.list.isEmpty();
}

From source file:appeng.core.sync.packets.PacketPaintedEntity.java

License:Open Source License

public PacketPaintedEntity(final ByteBuf stream) {
    this.entityId = stream.readInt();
    this.myColor = AEColor.values()[stream.readByte()];
    this.ticks = stream.readInt();
}