Java Code Examples for io.netty.channel.FileRegion#transferTo()
The following examples show how to use
io.netty.channel.FileRegion#transferTo() .
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: OioByteStreamChannel.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Override protected void doWriteFileRegion(FileRegion region) throws Exception { OutputStream os = this.os; if (os == null) { throw new NotYetConnectedException(); } if (outChannel == null) { outChannel = Channels.newChannel(os); } long written = 0; for (;;) { long localWritten = region.transferTo(outChannel, written); if (localWritten == -1) { checkEOF(region); return; } written += localWritten; if (written >= region.count()) { return; } } }
Example 2
Source File: AbstractKQueueStreamChannel.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
/** * 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 3
Source File: AbstractEpollStreamChannel.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: OioByteStreamChannel.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
@Override protected void doWriteFileRegion(FileRegion region) throws Exception { OutputStream os = this.os; if (os == null) { throw new NotYetConnectedException(); } if (outChannel == null) { outChannel = Channels.newChannel(os); } long written = 0; for (;;) { long localWritten = region.transferTo(outChannel, written); if (localWritten == -1) { checkEOF(region); return; } written += localWritten; if (written >= region.count()) { return; } } }
Example 5
Source File: FileRegionEncoder.java From DDMQ with Apache License 2.0 | 5 votes |
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder. * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }
Example 6
Source File: FileRegionEncoder.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder.将消息编码到ByteBuf中。对于可由此编码器处理的每个书面消息,将调用此方法。 * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }
Example 7
Source File: FileRegionEncoder.java From rocketmq-read with Apache License 2.0 | 5 votes |
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder. * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }
Example 8
Source File: FileRegionEncoder.java From DDMQ with Apache License 2.0 | 5 votes |
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder. * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }
Example 9
Source File: FileRegionEncoder.java From rocketmq with Apache License 2.0 | 5 votes |
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder. * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }
Example 10
Source File: NioSocketChannel.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Override protected long doWriteFileRegion(FileRegion region) throws Exception { final long position = region.transferred(); return region.transferTo(javaChannel(), position); }
Example 11
Source File: LambdaHttpHandler.java From quarkus with Apache License 2.0 | 4 votes |
@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 12
Source File: BaseFunction.java From quarkus with Apache License 2.0 | 4 votes |
@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); } }
Example 13
Source File: NioSocketChannel.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
@Override protected long doWriteFileRegion(FileRegion region) throws Exception { final long position = region.transfered(); return region.transferTo(javaChannel(), position); }