hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "radai rosenblatt (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCORE-335) add utility method to EntityUtils to allow reading entity contents into a user-provided OutputStream
Date Sat, 30 Mar 2013 13:17:15 GMT

    [ https://issues.apache.org/jira/browse/HTTPCORE-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13618071#comment-13618071
] 

radai rosenblatt commented on HTTPCORE-335:
-------------------------------------------

@Gary - youre right. dont know how i managed to miss that one. though - now this begs the
question of why doesnt EntityUtils.toByteArray() use that exact same HttpEntity.writeTo()
to achieve its purpose ...
                
> 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


Mime
View raw message