title: Netty HTTP client download zip file
body: <p>I'm writing Netty client which downloads one specific large file from one specific server using one specific url (<code>https://myserver.com/aaa.zip</code>) over HTTPS and saves it on disk.</p>
  
  <p>I haven't found any example for HTTP client getting binary response, so digging some documentation this is what I got:</p>
  
  <p>I'm using Netty 4.0.15</p>
  
  <pre><code>ChannelPipeline pipeline = socketChannel.pipeline();
      SSLEngine engine =
              SecureSslContextFactory.getClientContext().createSSLEngine();
          engine.setUseClientMode(true);
  
      pipeline.addLast("ssl", new SslHandler(engine));
      pipeline.addLast("codec",new HttpClientCodec());
      pipeline.addLast("handler", new HttpWebClientHandler());
  </code></pre>
  
  <p>and my Handler looks like this:</p>
  
  <pre><code>public class HttpWebClientHandler extends SimpleChannelInboundHandler&lt;HttpObject&gt; {
  
      File file = new File("aaa.zip");
          int written = 0;
  
      @Override
      protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
              throws Exception {
  
          if (msg instanceof HttpContent){
              HttpContent content = (HttpContent) msg;
              int currentlyWritten = 0;
              ByteBuf byteBuf = content.content();        
               FileOutputStream outputStream = new FileOutputStream(file);
               FileChannel localfileChannel = outputStream.getChannel();
               try{
               ByteBuffer byteBuffer = byteBuf.nioBuffer();
               currentlyWritten += localfileChannel.write(byteBuffer,written);
               written+=currentlyWritten;
                  byteBuf.readerIndex(byteBuf.readerIndex() + currentlyWritten);
                  localfileChannel.force(false);
              }finally{
                  localfileChannel.close();
                  outputStream.close();
              }
          }
      }
  }
  </code></pre>
  
  <p>My file is downloaded and have the same ammount of bytes as original one, but file is corrupted and has wrong checksum. </p>
  
  <p>Can anybody tell me what is wrong?</p>