[
https://issues.apache.org/jira/browse/HTTPCORE-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13615659#comment-13615659
]
Gary Gregory commented on HTTPCORE-335:
---------------------------------------
How about using org.apache.http.HttpEntity.writeTo(OutputStream)
> 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: [email protected]
For additional commands, e-mail: [email protected]