Java Code Examples for io.netty.channel.FileRegion#transferred()

The following examples show how to use io.netty.channel.FileRegion#transferred() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractKQueueStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Write a {@link FileRegion}
 * @param in the collection which contains objects to write.
 * @param region the {@link FileRegion} from which the bytes should be written
 * @return The value that should be decremented from the write quantum which starts at
 * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows:
 * <ul>
 *     <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content)
 *     is encountered</li>
 *     <li>1 - if a single call to write data was made to the OS</li>
 *     <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no
 *     data was accepted</li>
 * </ul>
 */
private int writeFileRegion(ChannelOutboundBuffer in, FileRegion region) throws Exception {
    if (region.transferred() >= region.count()) {
        in.remove();
        return 0;
    }

    if (byteChannel == null) {
        byteChannel = new KQueueSocketWritableByteChannel();
    }
    final long flushedAmount = region.transferTo(byteChannel, region.transferred());
    if (flushedAmount > 0) {
        in.progress(flushedAmount);
        if (region.transferred() >= region.count()) {
            in.remove();
        }
        return 1;
    }
    return WRITE_STATUS_SNDBUF_FULL;
}
 
Example 2
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Write a {@link FileRegion}
 * @param in the collection which contains objects to write.
 * @param region the {@link FileRegion} from which the bytes should be written
 * @return The value that should be decremented from the write quantum which starts at
 * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows:
 * <ul>
 *     <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content)
 *     is encountered</li>
 *     <li>1 - if a single call to write data was made to the OS</li>
 *     <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but
 *     no data was accepted</li>
 * </ul>
 */
private int writeFileRegion(ChannelOutboundBuffer in, FileRegion region) throws Exception {
    if (region.transferred() >= region.count()) {
        in.remove();
        return 0;
    }

    if (byteChannel == null) {
        byteChannel = new EpollSocketWritableByteChannel();
    }
    final long flushedAmount = region.transferTo(byteChannel, region.transferred());
    if (flushedAmount > 0) {
        in.progress(flushedAmount);
        if (region.transferred() >= region.count()) {
            in.remove();
        }
        return 1;
    }
    return WRITE_STATUS_SNDBUF_FULL;
}
 
Example 3
Source File: AbstractOioByteChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    for (;;) {
        Object msg = in.current();
        if (msg == null) {
            // nothing left to write
            break;
        }
        if (msg instanceof ByteBuf) {
            ByteBuf buf = (ByteBuf) msg;
            int readableBytes = buf.readableBytes();
            while (readableBytes > 0) {
                doWriteBytes(buf);
                int newReadableBytes = buf.readableBytes();
                in.progress(readableBytes - newReadableBytes);
                readableBytes = newReadableBytes;
            }
            in.remove();
        } else if (msg instanceof FileRegion) {
            FileRegion region = (FileRegion) msg;
            long transferred = region.transferred();
            doWriteFileRegion(region);
            in.progress(region.transferred() - transferred);
            in.remove();
        } else {
            in.remove(new UnsupportedOperationException(
                    "unsupported message type: " + StringUtil.simpleClassName(msg)));
        }
    }
}
 
Example 4
Source File: NioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected long doWriteFileRegion(FileRegion region) throws Exception {
    final long position = region.transferred();
    return region.transferTo(javaChannel(), position);
}
 
Example 5
Source File: OioByteStreamChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static void checkEOF(FileRegion region) throws IOException {
    if (region.transferred() < region.count()) {
        throw new EOFException("Expected to be able to write " + region.count() + " bytes, " +
                               "but only wrote " + region.transferred());
    }
}
 
Example 6
Source File: LambdaHttpHandler.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void handleMessage(Object msg) {
    try {
        //log.info("Got message: " + msg.getClass().getName());

        if (msg instanceof HttpResponse) {
            HttpResponse res = (HttpResponse) msg;
            responseBuilder.setStatusCode(res.status().code());

            if (request.getRequestSource() == AwsProxyRequest.RequestSource.ALB) {
                responseBuilder.setStatusDescription(res.status().reasonPhrase());
            }
            responseBuilder.setMultiValueHeaders(new Headers());
            for (String name : res.headers().names()) {
                for (String v : res.headers().getAll(name)) {
                    responseBuilder.getMultiValueHeaders().add(name, v);
                }
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;
            int readable = content.content().readableBytes();
            if (baos == null && readable > 0) {
                baos = createByteStream();
            }
            for (int i = 0; i < readable; i++) {
                baos.write(content.content().readByte());
            }
        }
        if (msg instanceof FileRegion) {
            FileRegion file = (FileRegion) msg;
            if (file.count() > 0 && file.transferred() < file.count()) {
                if (baos == null)
                    baos = createByteStream();
                if (byteChannel == null)
                    byteChannel = Channels.newChannel(baos);
                file.transferTo(byteChannel, file.transferred());
            }
        }
        if (msg instanceof LastHttpContent) {
            if (baos != null) {
                if (isBinary(responseBuilder.getMultiValueHeaders().getFirst("Content-Type"))) {
                    responseBuilder.setBase64Encoded(true);
                    responseBuilder.setBody(Base64.getMimeEncoder().encodeToString(baos.toByteArray()));
                } else {
                    responseBuilder.setBody(new String(baos.toByteArray(), "UTF-8"));
                }
            }
            future.complete(responseBuilder);
        }
    } catch (Throwable ex) {
        future.completeExceptionally(ex);
    } finally {
        if (msg != null) {
            ReferenceCountUtil.release(msg);
        }
    }
}
 
Example 7
Source File: BaseFunction.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void handleMessage(Object msg) {
    try {
        //log.info("Got message: " + msg.getClass().getName());

        if (msg instanceof HttpResponse) {
            HttpResponse res = (HttpResponse) msg;
            responseBuilder = request.createResponseBuilder(HttpStatus.valueOf(res.status().code()));
            for (Map.Entry<String, String> entry : res.headers()) {
                responseBuilder.header(entry.getKey(), entry.getValue());
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;
            if (baos == null) {
                // todo what is right size?
                baos = createByteStream();
            }
            int readable = content.content().readableBytes();
            for (int i = 0; i < readable; i++) {
                baos.write(content.content().readByte());
            }
        }
        if (msg instanceof FileRegion) {
            FileRegion file = (FileRegion) msg;
            if (file.count() > 0 && file.transferred() < file.count()) {
                if (baos == null)
                    baos = createByteStream();
                if (byteChannel == null)
                    byteChannel = Channels.newChannel(baos);
                file.transferTo(byteChannel, file.transferred());
            }
        }
        if (msg instanceof LastHttpContent) {
            responseBuilder.body(baos.toByteArray());
            future.complete(responseBuilder.build());
        }
    } catch (Throwable ex) {
        future.completeExceptionally(ex);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}