Example usage for io.netty.buffer ByteBuf ensureWritable

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

Introduction

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

Prototype

public abstract int ensureWritable(int minWritableBytes, boolean force);

Source Link

Document

Expands the buffer #capacity() to make sure the number of #writableBytes() writable bytes is equal to or greater than the specified value.

Usage

From source file:impl.underdark.transport.bluetooth.BtLink.java

License:Open Source License

private void inputLoop() {
    // Input I/O thread.

    sendHelloFrame();/*  w  w w.j  a  v  a  2  s  .  co  m*/

    int bufferSize = 4096;
    ByteBuf inputData = Unpooled.buffer(bufferSize);
    inputData.order(ByteOrder.BIG_ENDIAN);

    try {
        int len;
        while (true) {
            inputData.ensureWritable(bufferSize, true);
            len = inputStream.read(inputData.array(), inputData.writerIndex(), bufferSize);
            if (len <= 0)
                break;

            inputData.writerIndex(inputData.writerIndex() + len);

            if (!formFrames(inputData))
                break;

            inputData.discardReadBytes();
            inputData.capacity(inputData.writerIndex() + bufferSize);
        } // while
    } catch (InterruptedIOException ex) {
        Logger.warn("bt input timeout: {}", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    } catch (Exception ex) {
        Logger.warn("bt input read failed.", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    }

    Logger.debug("bt input read end.");
    notifyDisconnect();

}

From source file:impl.underdark.transport.nsd.NsdLink.java

License:Open Source License

private void inputLoop() {
    // Input thread.
    final int bufferSize = 4096;
    ByteBuf inputData = Unpooled.buffer(bufferSize);
    inputData.order(ByteOrder.BIG_ENDIAN);

    try {/*from  w  w  w  .  j  a va 2s  . c o m*/
        int len;
        while (true) {
            inputData.ensureWritable(bufferSize, true);
            len = inputStream.read(inputData.array(), inputData.writerIndex(), bufferSize);
            if (len <= 0)
                break;

            inputData.writerIndex(inputData.writerIndex() + len);

            if (!formFrames(inputData))
                break;

            inputData.discardReadBytes();
            inputData.capacity(inputData.writerIndex() + bufferSize);
        } // while
    } catch (InterruptedIOException ex) {
        Logger.warn("nsd input timeout: {}", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    } catch (Exception ex) {
        Logger.warn("nsd input read failed: {}", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    }

    Logger.debug("nsd input read end");
    notifyDisconnect();
}