jira-importer opened a new issue, #922:
URL: https://github.com/apache/httpcomponents-core/issues/922

   **[Radai 
Rosenblatt](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=radai)**
 opened 
**[HTTPCORE-335](https://issues.apache.org/jira/browse/HTTPCORE-335?redirect=false)**
 and commented
   
   as a user of HttpComponents the most basic way i have to download a large 
binary file using HttpCore would be:
   
   ```
   HttpResponse response = get(url, null, null);
   HttpEntity entity = response.getEntity();
   byte[] data = EntityUtils.toByteArray(entity);
   ```
   
   while this works, it has the downside of storing what might be a pretty big 
file as a byte[] in memory.
   adding an extra method to EntityUtils would allow the same functionality 
with a much smaller memory footprint:
   
   ```
   HttpResponse response = get(url, null, null);
   HttpEntity entity = response.getEntity();
   //user's responsibility to provide an output stream. commonly a temporary 
file?
   File tmpFile = getMeATmpFile();
   FileOutputStream os = new FileOutputStream(tmpFile);
   long bytesWritten = EntityUtils.writeOut(entity,os);
   //user can now work with entity contents that he has "downloaded" to the 
temp file.
   ```
   
   a trivial implementation of this method would be an almost exact copy-paste 
of EntityUtils.toByteArray():
   
   ```
   public static long writeOut(final HttpEntity entity, OutputStream output) 
throws IOException {
        if (entity == null) {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }
        InputStream instream = entity.getContent();
        if (instream == null) {
            return 0;
        }
        try {           
            byte[] tmp = new byte[4096];
            int l;
            long count = 0;
            while((l = instream.read(tmp)) != -1) {
                output.write(tmp,0,l);
                count += l;
            }
            output.flush();
            return count;
        } finally {
            instream.close();
        }
    }
   ```
   
   
   
   ---
   
   **Affects:** 4.2.3
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to