Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

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

License:Apache License

/**
 * Reads the chunks from the given {@link DataReader}.
 *
 * @param reader the data reader/*from   w w w  . j  a v a  2  s . c  o m*/
 * @param checksumStart the start position to calculate the checksum
 * @param bytesToRead bytes to read
 * @return the checksum of the data read starting from checksumStart
 */
private long checkChunks(DataReader reader, long checksumStart, long bytesToRead) throws Exception {
    long pos = 0;
    long checksum = 0;

    while (true) {
        DataBuffer chunk = reader.readChunk();
        if (chunk == null) {
            break;
        }
        try {
            assertTrue(chunk instanceof NioDataBuffer);
            ByteBuf buf = (ByteBuf) chunk.getNettyOutput();
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            for (int i = 0; i < bytes.length; i++) {
                if (pos >= checksumStart) {
                    checksum += BufferUtils.byteToInt(bytes[i]);
                }
                pos++;
                if (pos >= bytesToRead) {
                    return checksum;
                }
            }
        } finally {
            chunk.release();
        }
    }
    return checksum;
}

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

License:Apache License

/**
 * Reads the packets from the given {@link PacketReader}.
 *
 * @param reader the packet reader//from ww  w  .  j a va2  s  .co  m
 * @param checksumStart the start position to calculate the checksum
 * @param bytesToRead bytes to read
 * @return the checksum of the data read starting from checksumStart
 */
private long checkPackets(PacketReader reader, long checksumStart, long bytesToRead) throws Exception {
    long pos = 0;
    long checksum = 0;

    while (true) {
        DataBuffer packet = reader.readPacket();
        if (packet == null) {
            break;
        }
        try {
            Assert.assertTrue(packet instanceof DataNettyBufferV2);
            ByteBuf buf = (ByteBuf) packet.getNettyOutput();
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            for (int i = 0; i < bytes.length; i++) {
                if (pos >= checksumStart) {
                    checksum += BufferUtils.byteToInt(bytes[i]);
                }
                pos++;
                if (pos >= bytesToRead) {
                    return checksum;
                }
            }
        } finally {
            packet.release();
        }
    }
    return checksum;
}

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

License:Apache License

@Override
public void writeChunk(ByteBuf chunk) throws IOException {
    mBuffer.limit(mBuffer.position() + chunk.readableBytes());
    chunk.readBytes(mBuffer);
}

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

License:Apache License

@Override
public void writePacket(ByteBuf packet) throws IOException {
    mBuffer.limit(mBuffer.position() + packet.readableBytes());
    packet.readBytes(mBuffer);
}

From source file:alluxio.network.protocol.RPCBlockWriteRequest.java

License:Apache License

/**
 * Decodes the input {@link ByteBuf} into a {@link RPCBlockWriteRequest} object and returns it.
 *
 * @param in the input {@link ByteBuf}/* www  . ja va 2 s  .  com*/
 * @return The decoded RPCBlockWriteRequest object
 */
public static RPCBlockWriteRequest decode(ByteBuf in) {
    long sessionId = in.readLong();
    long blockId = in.readLong();
    long offset = in.readLong();
    long length = in.readLong();
    // TODO(gene): Look into accessing Netty ByteBuf directly, to avoid copying the data.
    // Length will always be greater than 0 if the request is not corrupted. If length is negative,
    // ByteBuffer.allocate will fail. If length is 0 this will become a no-op but still go through
    // the necessary calls to validate the sessionId/blockId. If length is positive, the request
    // will proceed as normal
    ByteBuffer buffer = ByteBuffer.allocate((int) length);
    in.readBytes(buffer);
    DataByteBuffer data = new DataByteBuffer(buffer, (int) length);
    return new RPCBlockWriteRequest(sessionId, blockId, offset, length, data);
}

From source file:alluxio.network.protocol.RPCFileWriteRequest.java

License:Apache License

/**
 * Decodes the input {@link ByteBuf} into a {@link RPCFileWriteRequest} object and returns it.
 *
 * @param in the input {@link ByteBuf}/* w w  w .  j a va2s.co m*/
 * @return The decoded RPCFileWriteRequest object
 */
public static RPCFileWriteRequest decode(ByteBuf in) {
    long tempUfsFileId = in.readLong();
    long offset = in.readLong();
    long length = in.readLong();
    // TODO(gene): Look into accessing Netty ByteBuf directly, to avoid copying the data.
    // Length will always be greater than 0 if the request is not corrupted. If length is negative,
    // ByteBuffer.allocate will fail. If length is 0 this will become a no-op but still go through
    // the necessary calls to validate the tempUfsFileId. If length is positive, the request will
    // proceed as normal
    ByteBuffer buffer = ByteBuffer.allocate((int) length);
    in.readBytes(buffer);
    DataByteBuffer data = new DataByteBuffer(buffer, (int) length);
    return new RPCFileWriteRequest(tempUfsFileId, offset, length, data);
}

From source file:alluxio.network.protocol.RPCProtoMessage.java

License:Apache License

/**
 * Decodes the message from a buffer. This method increments the refcount of the bytebuf passed
 * by 1./*from ww w  .  ja  v  a  2s  .c om*/
 *
 * @param in the buffer
 * @param prototype a message prototype used to infer the type of the message
 * @return the message decoded
 */
public static RPCProtoMessage decode(ByteBuf in, ProtoMessage.Type prototype) {
    int length = in.readInt();
    byte[] serialized = new byte[length];
    in.readBytes(serialized);
    in.retain();
    return new RPCProtoMessage(serialized, prototype, new DataNettyBufferV2(in));
}

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

License:Open Source License

public PacketPartialItem(final ByteBuf stream) {
    this.pageNum = stream.readShort();
    stream.readBytes(this.data = new byte[stream.readableBytes()]);
}

From source file:appeng.util.item.AEItemStack.java

License:Open Source License

public static IAEItemStack loadItemStackFromPacket(final ByteBuf data) throws IOException {
    final byte mask = data.readByte();
    // byte PriorityType = (byte) (mask & 0x03);
    final byte stackType = (byte) ((mask & 0x0C) >> 2);
    final byte countReqType = (byte) ((mask & 0x30) >> 4);
    final boolean isCraftable = (mask & 0x40) > 0;
    final boolean hasTagCompound = (mask & 0x80) > 0;

    // don't send this...
    final NBTTagCompound d = new NBTTagCompound();

    // For some insane reason, Vanilla can only parse numeric item ids if they are strings
    short itemNumericId = data.readShort();
    d.setString("id", String.valueOf(itemNumericId));
    d.setShort("Damage", data.readShort());
    d.setByte("Count", (byte) 0);

    if (hasTagCompound) {
        final int len = data.readInt();

        final byte[] bd = new byte[len];
        data.readBytes(bd);

        final ByteArrayInputStream di = new ByteArrayInputStream(bd);
        d.setTag("tag", CompressedStreamTools.read(new DataInputStream(di)));
    }// w  w w  .ja va  2s  . c  o m

    // long priority = getPacketValue( PriorityType, data );
    final long stackSize = getPacketValue(stackType, data);
    final long countRequestable = getPacketValue(countReqType, data);

    final ItemStack itemstack = ItemStack.loadItemStackFromNBT(d);
    if (itemstack == null) {
        return null;
    }

    final AEItemStack item = AEItemStack.create(itemstack);
    // item.priority = (int) priority;
    item.setStackSize(stackSize);
    item.setCountRequestable(countRequestable);
    item.setCraftable(isCraftable);
    return item;
}

From source file:at.yawk.accordion.codec.unsafe.NamedObjectCodec.java

License:Mozilla Public License

private static String readClassName(ByteBuf buf) {
    int len = buf.bytesBefore((byte) 0);
    byte[] data = new byte[len];
    buf.readBytes(data);
    buf.skipBytes(1); // \0
    return new String(data, StandardCharsets.UTF_8);
}