List of usage examples for io.netty.handler.stream ChunkedFile ChunkedFile
public ChunkedFile(RandomAccessFile file) throws IOException
From source file:com.flysoloing.learning.network.netty.file.FileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { RandomAccessFile raf = null;// w ww .j a va 2 s .c o m long length = -1; try { raf = new RandomAccessFile(msg, "r"); length = raf.length(); } catch (Exception e) { ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n'); return; } finally { if (length < 0 && raf != null) { raf.close(); } } ctx.write("OK: " + raf.length() + '\n'); if (ctx.pipeline().get(SslHandler.class) == null) { // SSL not enabled - can use zero-copy file transfer. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); } else { // SSL enabled - cannot use zero-copy file transfer. ctx.write(new ChunkedFile(raf)); } ctx.writeAndFlush("\n"); }
From source file:com.SendFileHandler_org.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws IOException { ctx.writeAndFlush(new ChunkedFile(new File(this.fileName))) .addListener(new FileTransferCompleteListener(server)); }
From source file:com.sheldon.javaPrj.netty.FileClientHandler.java
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("send file"); File f = new File("/home/wangxiangnan/Dump20140717.sql"); ctx.channel().writeAndFlush(new ChunkedFile(f)); }
From source file:com.topsec.bdc.platform.api.test.file.FileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { RandomAccessFile raf = null;/*from w w w .ja v a 2s . c o m*/ long length = -1; try { raf = new RandomAccessFile(msg, "r"); length = raf.length(); } catch (Exception e) { ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n'); return; } finally { if (length < 0 && raf != null) { raf.close(); } } ctx.write("OK: " + raf.length() + '\n'); if (ctx.pipeline().get(SslHandler.class) == null) { // SSL not enabled - can use zero-copy file transfer. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); } else { // SSL enabled - cannot use zero-copy file transfer. ctx.write(new ChunkedFile(raf)); } ctx.writeAndFlush("\n"); }
From source file:io.aos.netty5.file.FileServerHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { RandomAccessFile raf = null;//from w w w .j a va 2 s . com long length = -1; try { raf = new RandomAccessFile(msg, "r"); length = raf.length(); } catch (Exception e) { ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n'); return; } finally { if (length < 0 && raf != null) { raf.close(); } } ctx.write("OK: " + raf.length() + '\n'); if (ctx.pipeline().get(SslHandler.class) == null) { // SSL not enabled - can use zero-copy file transfer. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); } else { // SSL enabled - cannot use zero-copy file transfer. ctx.write(new ChunkedFile(raf)); } ctx.writeAndFlush("\n"); }
From source file:io.gatling.http.client.body.file.FileRequestBody.java
License:Apache License
@Override public WritableContent build(boolean zeroCopy, ByteBufAllocator alloc) throws IOException { long contentLength = content.length(); Object file = zeroCopy ? new DefaultFileRegion(content, 0, contentLength) : new ChunkedFile(content); return new WritableContent(file, contentLength); }
From source file:io.netty.handler.stream.ChunkedWriteHandlerTest.java
License:Apache License
@Test public void testChunkedFile() throws IOException { check(new ChunkedFile(TMP)); check(new ChunkedFile(TMP), new ChunkedFile(TMP), new ChunkedFile(TMP)); }
From source file:netty4.fileExample.FileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { RandomAccessFile raf = null;//from ww w.j a v a 2s . c o m long length = -1; try { raf = new RandomAccessFile(msg, "r"); length = raf.length(); } catch (Exception e) { ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n'); return; } finally { if (length < 0 && raf != null) { raf.close(); } } ctx.write("OK: " + raf.length() + '\n'); if (ctx.pipeline().get(SslHandler.class) == null) { // SSL not enabled - can use zero-copy file transfer. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); System.out.println("zero-copy"); } else { // SSL enabled - cannot use zero-copy file transfer. ctx.write(new ChunkedFile(raf)); System.out.println("non zero-copy"); } //ctx.writeAndFlush("\n"); ChannelFuture writeFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); }
From source file:nettyFileServer.FileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { msg = "index.html"; RandomAccessFile raf = null;/*from ww w . ja va2s. com*/ long length = -1; try { raf = new RandomAccessFile(msg, "r"); length = raf.length(); } catch (Exception e) { ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n'); return; } finally { if (length < 0 && raf != null) { raf.close(); } } ctx.write("OK: " + raf.length() + '\n'); if (ctx.pipeline().get(SslHandler.class) == null) { // SSL not enabled - can use zero-copy file transfer. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); } else { // SSL enabled - cannot use zero-copy file transfer. ctx.write(new ChunkedFile(raf)); } ctx.writeAndFlush("\n"); }
From source file:org.glowroot.agent.plugin.netty.Http1ServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest request = (HttpRequest) msg; @SuppressWarnings("deprecation") String uri = request.getUri(); if (uri.equals("/exception")) { throw new Exception("Test"); }//from w w w. j av a 2 s .c om if (uri.equals("/chunked")) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set("transfer-encoding", "chunked"); response.headers().set("content-type", "text/plain"); ctx.write(response); final File file = File.createTempFile("glowroot-netty-plugin-it-", ".txt"); final ChunkedFile chunkedFile = new ChunkedFile(file); Files.write(CONTENT, file); ctx.write(chunkedFile).addListener(ChannelFutureListener.CLOSE) .addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> arg0) throws Exception { chunkedFile.close(); if (!file.delete()) { throw new IllegalStateException("Could not delete file: " + file.getPath()); } } }); return; } FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set("Content-Type", "text/plain"); response.headers().set("Content-Length", response.content().readableBytes()); ctx.write(response).addListener(ChannelFutureListener.CLOSE); } }