You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Oleg Kalnichevski (JIRA)" <ji...@apache.org> on 2013/04/02 18:27:17 UTC

[jira] [Resolved] (HTTPCORE-335) add utility method to EntityUtils to allow reading entity contents into a user-provided OutputStream

     [ https://issues.apache.org/jira/browse/HTTPCORE-335?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oleg Kalnichevski resolved HTTPCORE-335.
----------------------------------------

    Resolution: Not A Problem

EntityUtils#toByteArray() utilizes non synchronized ByteArrayBuffer internally, but otherwise for no really good reason.

Oleg
                
> add utility method to EntityUtils to allow reading entity contents into a user-provided OutputStream
> ----------------------------------------------------------------------------------------------------
>
>                 Key: HTTPCORE-335
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-335
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore
>    Affects Versions: 4.2.3
>            Reporter: radai rosenblatt
>
> as a user of HttpComponents the most basic way i have to download a large binary file using HttpCore would be:
> {code}
> HttpResponse response = get(url, null, null);
> HttpEntity entity = response.getEntity();
> byte[] data = EntityUtils.toByteArray(entity);
> {code}
> 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:
> {code}
> 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.
> {code}
> a trivial implementation of this method would be an almost exact copy-paste of EntityUtils.toByteArray():
> {code}
>    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();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org