Example usage for io.netty.buffer ByteBuf isWritable

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

Introduction

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

Prototype

public abstract boolean isWritable();

Source Link

Document

Returns true if and only if (this.capacity - this.writerIndex) is greater than 0 .

Usage

From source file:org.asynchttpclient.request.body.multipart.MultipartBody.java

License:Open Source License

public BodyState transferTo(ByteBuf target) throws IOException {

    if (done)/*from  w  w  w. j  a v a  2  s.co m*/
        return BodyState.STOP;

    while (target.isWritable() && !done) {
        MultipartPart<? extends Part> currentPart = parts.get(currentPartIndex);
        currentPart.transferTo(target);

        if (currentPart.getState() == MultipartState.DONE) {
            currentPartIndex++;
            if (currentPartIndex == parts.size()) {
                done = true;
            }
        }
    }

    return BodyState.CONTINUE;
}

From source file:org.fusesource.hawtdispatch.netty.HawtSocketChannel.java

License:Apache License

private void onReadReady() {
    final ChannelPipeline pipeline = pipeline();
    final ByteBuf byteBuf = pipeline.inboundByteBuffer();
    boolean closed = false;
    boolean read = false;
    boolean firedInboundBufferSuspended = false;
    try {/*w  w w .  jav a2s.  c  om*/
        expandReadBuffer(byteBuf);
        loop: for (;;) {

            int localReadAmount = byteBuf.writeBytes(javaChannel(), byteBuf.writableBytes());
            if (localReadAmount > 0) {
                read = true;
            } else if (localReadAmount < 0) {
                closed = true;
                break;
            }

            switch (expandReadBuffer(byteBuf)) {
            case 0:
                // Read all - stop reading.
                break loop;
            case 1:
                // Keep reading until everything is read.
                break;
            case 2:
                // Let the inbound handler drain the buffer and continue reading.
                if (read) {
                    read = false;
                    pipeline.fireInboundBufferUpdated();
                    if (!byteBuf.isWritable()) {
                        throw new IllegalStateException(
                                "an inbound handler whose buffer is full must consume at " + "least one byte.");
                    }
                }
            }
        }
    } catch (Throwable t) {
        if (read) {
            read = false;
            pipeline.fireInboundBufferUpdated();
        }

        if (t instanceof IOException) {
            closed = true;
        } else if (!closed) {
            firedInboundBufferSuspended = true;
            pipeline.fireChannelReadSuspended();
        }
        pipeline().fireExceptionCaught(t);
    } finally {
        if (read) {
            pipeline.fireInboundBufferUpdated();
        }

        if (closed) {
            setInputShutdown();
            if (isOpen()) {
                if (Boolean.TRUE.equals(config().getOption(ChannelOption.ALLOW_HALF_CLOSURE))) {
                    pipeline.fireUserEventTriggered(ChannelInputShutdownEvent.INSTANCE);
                } else {
                    close(newPromise());
                }
            }
        } else if (!firedInboundBufferSuspended) {
            pipeline.fireChannelReadSuspended();
        }

        if (!config().isAutoRead()) {
            readSource.suspend();
        }
    }
}

From source file:org.jfxvnc.net.rfb.codec.decoder.rect.RawRectDecoder.java

License:Apache License

protected void sendRect(List<Object> out) {
    int i = 0;/* w w  w . ja  va 2  s.  co m*/
    // ByteBuf pixels = aloc.buffer(capacity);
    // while (pixels.isWritable()) {
    // pixels.writeInt(framebuffer.getUnsignedByte(i + redPos) << pixelFormat.getRedShift()
    // | framebuffer.getUnsignedByte(i + 1) << pixelFormat.getGreenShift()
    // | framebuffer.getUnsignedByte(i + bluePos) << pixelFormat.getBlueShift() | 0xff000000);
    // i+=4;
    // }

    if (pixelFormat.getBytePerPixel() == 1) {
        out.add(new RawImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
                framebuffer.copy(), rect.getWidth()));
        return;
    }

    // reduce 4 byte to 3 byte
    int size = capacity - (capacity / 4);
    ByteBuf pixels = aloc.buffer(size, size);
    while (pixels.isWritable()) {
        pixels.writeByte(framebuffer.getUnsignedByte(i + redPos));
        pixels.writeByte(framebuffer.getUnsignedByte(i + 1));
        pixels.writeByte(framebuffer.getUnsignedByte(i + bluePos));
        i += 4;
    }
    out.add(new RawImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), pixels,
            rect.getWidth() * 3));

    // if (pixels.length > 5000) {
    // Arrays.parallelSetAll(pixels,
    // (i) -> framebuffer.getUnsignedByte(i * 4 + redPos) << pixelFormat.getRedShift()
    // | framebuffer.getUnsignedByte(i * 4 + 1) << pixelFormat.getGreenShift()
    // | framebuffer.getUnsignedByte(i * 4 + bluePos) << pixelFormat.getBlueShift() | 0xff000000);
    // } else {
    // Arrays.setAll(pixels,
    // (i) -> framebuffer.getUnsignedByte(i * 4 + redPos) << pixelFormat.getRedShift()
    // | framebuffer.getUnsignedByte(i * 4 + 1) << pixelFormat.getGreenShift()
    // | framebuffer.getUnsignedByte(i * 4 + bluePos) << pixelFormat.getBlueShift() | 0xff000000);
    // }

}

From source file:org.jfxvnc.net.rfb.codec.decoder.rect.ZlibRectDecoder.java

License:Apache License

@Override
protected void sendRect(List<Object> out) {
    initialized = false;/*from ww w.j  a  v  a 2s .  c  o m*/
    if (framebuffer.hasArray()) {
        inflater.setInput(framebuffer.array(), 0, framebuffer.capacity());
    } else {
        byte[] array = new byte[framebuffer.readableBytes()];
        framebuffer.getBytes(framebuffer.readerIndex(), array);
        inflater.setInput(array);
    }
    byte[] result = new byte[rect.getWidth() * rect.getHeight() * pixelFormat.getBytePerPixel()];
    try {
        int resultLength = inflater.inflate(result);
        if (resultLength != result.length) {
            logger.error("incorrect zlib ({}/{})", resultLength, result.length);
            return;
        }

        if (pixelFormat.getBytePerPixel() == 1) {
            out.add(new ZlibImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
                    framebuffer.copy(), rect.getWidth()));
            return;
        }

        int i = 0;
        ByteBuf pixels = aloc.buffer(result.length - (result.length / 4));
        while (pixels.isWritable()) {
            pixels.writeByte(result[i + redPos] & 0xFF);
            pixels.writeByte(result[i + 1] & 0xFF);
            pixels.writeByte(result[i + bluePos] & 0xFF);
            i += 4;
        }
        out.add(new ZlibImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), pixels,
                rect.getWidth() * 3));

    } catch (DataFormatException e) {
        logger.error(e.getMessage(), e);
        return;
    }
}

From source file:org.legacy.network.protocol.ondemand.XorEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
    while (out.isWritable()) {
        out.writeByte(in.readUnsignedByte() ^ key);
    }/*from   w  ww  . j  a v a 2 s.  com*/
}