Example usage for io.netty.buffer ByteBuf writableBytes

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

Introduction

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

Prototype

public abstract int writableBytes();

Source Link

Document

Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex) .

Usage

From source file:DescriptorTester.java

License:Open Source License

public void sender(ByteBuf buf) {

    DatagramSocket socket = null;
    try {/*  w w  w. ja v  a2 s. co m*/
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }

    {
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        System.out.printf("packetsize %d", packet_size);
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message  ");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

From source file:TestDescriptor.java

License:Open Source License

public void sender(ByteBuf buf) {

    DatagramSocket socket = null;
    try {//w  w w  .j a va 2s .c  om
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }

    {
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        System.out.printf("packetsize %d", packet_size);
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message  ");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:CapwapMsgSender.java

License:Open Source License

public static void main(String args[]) {

    DatagramSocket socket = null;
    try {//from   w w w . j  a  va  2 s.  c o  m
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //ByteBuf nett_buf = Unpooled.buffer();
    ByteBuf buf = null;

    Scanner sc = new Scanner(System.in);
    while (true) {
        System.out.println("\nEnter Capwap Message ID");
        int capwapMsg = sc.nextInt();
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message %s ", ODLCapwapConsts.msgTypetoString(capwapMsg));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:alluxio.worker.block.io.LocalFileBlockReader.java

License:Apache License

@Override
public int transferTo(ByteBuf buf) throws IOException {
    return buf.writeBytes(mLocalFileChannel, buf.writableBytes());
}

From source file:alluxio.worker.block.RemoteBlockReader.java

License:Apache License

@Override
public int transferTo(ByteBuf buf) throws IOException {
    Preconditions.checkState(!mClosed);/*from   ww  w .  j  a va2  s  .  c o m*/
    init();
    if (mInputStream == null || mInputStream.remaining() <= 0) {
        return -1;
    }
    int bytesToRead = (int) Math.min(buf.writableBytes(), mInputStream.remaining());
    return buf.writeBytes(mInputStream, bytesToRead);
}

From source file:alluxio.worker.block.UnderFileSystemBlockReader.java

License:Apache License

/**
 * This interface is supposed to be used for sequence block reads.
 *
 * @param buf the byte buffer//from w w w . j a  v a  2s  .  c  om
 * @return the number of bytes read, -1 if it reaches EOF and none was read
 * @throws IOException if any I/O errors occur when reading the block
 */
@Override
public int transferTo(ByteBuf buf) throws IOException {
    Preconditions.checkState(!mClosed);
    if (mUnderFileSystemInputStream == null) {
        return -1;
    }
    if (mBlockMeta.getBlockSize() <= mInStreamPos) {
        return -1;
    }
    // Make a copy of the state to keep track of what we have read in this transferTo call.
    ByteBuf bufCopy = null;
    if (mBlockWriter != null) {
        bufCopy = buf.duplicate();
        bufCopy.readerIndex(bufCopy.writerIndex());
    }
    int bytesToRead = (int) Math.min((long) buf.writableBytes(), mBlockMeta.getBlockSize() - mInStreamPos);
    int bytesRead = buf.writeBytes(mUnderFileSystemInputStream, bytesToRead);

    if (bytesRead <= 0) {
        return bytesRead;
    }

    mInStreamPos += bytesRead;

    if (mBlockWriter != null) {
        bufCopy.writerIndex(buf.writerIndex());
        while (bufCopy.readableBytes() > 0) {
            mBlockWriter.transferFrom(bufCopy);
        }
    }

    return bytesRead;
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferFullBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE * 2, (int) TEST_BLOCK_SIZE * 2);
    try {//  ww w  .j a  v  a2  s  .  com
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE, (int) TEST_BLOCK_SIZE,
                buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferPartialBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE / 2, (int) TEST_BLOCK_SIZE / 2);
    try {//from   w  ww .j  av a2 s .  c  om
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE,
                (int) TEST_BLOCK_SIZE / 2, buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

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

License:Apache License

@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
    BlockReader blockReader = ((BlockReadRequestInternal) mRequest).mBlockReader;
    Preconditions.checkArgument(blockReader.getChannel() instanceof FileChannel,
            "Only FileChannel is supported!");
    switch (mTransferType) {
    case MAPPED://w w w  . j a  va  2s.  co m
        ByteBuf buf = channel.alloc().buffer(len, len);
        try {
            FileChannel fileChannel = (FileChannel) blockReader.getChannel();
            Preconditions.checkState(fileChannel.position() == offset);
            while (buf.writableBytes() > 0 && buf.writeBytes(fileChannel, buf.writableBytes()) != -1) {
            }
            return new DataNettyBufferV2(buf);
        } catch (Throwable e) {
            buf.release();
            throw e;
        }
    case TRANSFER: // intend to fall through as TRANSFER is the default type.
    default:
        return new DataFileChannel((FileChannel) blockReader.getChannel(), offset, len);
    }
}

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

License:Apache License

@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
    BlockReader blockReader = ((UfsBlockReadRequestInternal) mRequest).mBlockReader;
    // This buf is released by netty.
    ByteBuf buf = channel.alloc().buffer(len, len);
    try {//w  w  w  .  j  av  a2s .  c  om
        while (buf.writableBytes() > 0 && blockReader.transferTo(buf) != -1) {
        }
        return new DataNettyBufferV2(buf);
    } catch (Throwable e) {
        buf.release();
        throw e;
    }
}