title: Using HttpClient 4.1 to decode chunked data
body: <p>I am using <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html" rel="nofollow noreferrer">HttpClient</a> to send a request a server which is supposed to return xml data. This data is returned as chunked data. I am then trying to write the received xml data to a file. The code I use is shown below:</p>
  
  <pre><code>    HttpEntity entity = response.getEntity();
      InputStream instream = entity.getContent();
  
          try {
              // do something useful
              InputStreamReader isr = new InputStreamReader(instream);
              FileWriter pw;
  
              pw = new FileWriter(filename, append);
  
              OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream(filename, append), "UTF-8");
              BufferedReader rd = new BufferedReader(isr);
  
              String line = "";
              while ((line = rd.readLine()) != null) {
                  // pw.write(line);
                  outWriter.write(line);
              }
              isr.close();
              pw.close();
          } finally {
              instream.close();
          }
  </code></pre>
  
  <p>This results in data that looks as follows to be printed to the file:
  <img src="https://i.stack.imgur.com/p9ygJ.png" alt="resulting file"></p>
  
  <p>This code works for non chunked data. How do I properly handle chunked data responses using HttpClient. Any help is greatly appreciated.</p>