List of usage examples for io.netty.handler.stream ChunkedNioStream ChunkedNioStream
public ChunkedNioStream(ReadableByteChannel in)
From source file:io.netty.handler.stream.ChunkedWriteHandlerTest.java
License:Apache License
@Test public void testChunkedNioStream() { check(new ChunkedNioStream(Channels.newChannel(new ByteArrayInputStream(BYTES)))); check(new ChunkedNioStream(Channels.newChannel(new ByteArrayInputStream(BYTES))), new ChunkedNioStream(Channels.newChannel(new ByteArrayInputStream(BYTES))), new ChunkedNioStream(Channels.newChannel(new ByteArrayInputStream(BYTES)))); }
From source file:ratpack.file.internal.DefaultFileHttpTransmitter.java
License:Apache License
@Override public void transmit(ExecControl execContext, final BasicFileAttributes basicFileAttributes, final Path file) throws Exception { final boolean compressThis = compress && basicFileAttributes.size() > compressionMinSize && isContentTypeCompressible(); if (compress && !compressThis) { // Signal to the compressor not to compress this httpHeaders.set(HttpHeaderConstants.CONTENT_ENCODING, HttpHeaders.Values.IDENTITY); }//from w ww . jav a2s.com if (file.getFileSystem().equals(FileSystems.getDefault()) && !compressThis) { execContext.blocking(new Callable<FileChannel>() { public FileChannel call() throws Exception { return new FileInputStream(file.toFile()).getChannel(); } }).then(new Action<FileChannel>() { public void execute(FileChannel fileChannel) throws Exception { FileRegion defaultFileRegion = new DefaultFileRegion(fileChannel, 0, basicFileAttributes.size()); transmit(basicFileAttributes, defaultFileRegion); } }); } else { execContext.blocking(new Callable<ReadableByteChannel>() { public ReadableByteChannel call() throws Exception { return Files.newByteChannel(file); } }).then(new Action<ReadableByteChannel>() { public void execute(ReadableByteChannel fileChannel) throws Exception { transmit(basicFileAttributes, new ChunkedInputAdapter(new ChunkedNioStream(fileChannel))); } }); } }
From source file:ratpack.server.internal.DefaultResponseTransmitter.java
License:Apache License
@Override public void transmit(final Context context, final HttpResponseStatus responseStatus, final BasicFileAttributes basicFileAttributes, final Path file) { String contentType = responseHeaders.get(HttpHeaderConstants.CONTENT_TYPE); final long size = basicFileAttributes.size(); Pair<Long, String> fileDetails = Pair.of(size, contentType); CompressionConfig compressionConfig = context.get(CompressionConfig.class); final boolean compressionEnabled = compressionConfig.isCompressResponses(); final Predicate<? super Pair<Long, String>> shouldCompress; if (compressionEnabled) { ImmutableSet<String> blacklist = compressionConfig.getMimeTypeBlackList(); shouldCompress = new ShouldCompressPredicate(compressionConfig.getMinSize(), compressionConfig.getMimeTypeWhiteList(), blacklist.isEmpty() ? ActivationBackedMimeTypes.getDefaultExcludedMimeTypes() : blacklist); } else {/*from www . ja va 2 s .c om*/ shouldCompress = Predicates.alwaysFalse(); } final boolean compressThis = compressionEnabled && (contentType != null && shouldCompress.apply(fileDetails)); if (!compressThis) { // Signal to the compressor not to compress this responseHeaders.set(HttpHeaderConstants.CONTENT_ENCODING, HttpHeaderConstants.IDENTITY); } responseHeaders.set(HttpHeaderConstants.CONTENT_LENGTH, size); if (!isSsl && !compressThis && file.getFileSystem().equals(FileSystems.getDefault())) { execControl.blocking(() -> new FileInputStream(file.toFile()).getChannel()).then(fileChannel -> { FileRegion defaultFileRegion = new DefaultFileRegion(fileChannel, 0, size); transmit(context, responseStatus, defaultFileRegion); }); } else { execControl.blocking(() -> Files.newByteChannel(file)).then(fileChannel -> transmit(context, responseStatus, new HttpChunkedInput(new ChunkedNioStream(fileChannel)))); } }