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] [Updated] (HTTPCORE-335) add utility method to EntityUtils to allow reading entity contents into a user-provided OutputStream
Date Fri, 22 Mar 2013 20:21:17 GMT

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

radai rosenblatt updated HTTPCORE-335:
--------------------------------------

    Description: 
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}


  was:
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 {
            if (entity.getContentLength() > Integer.MAX_VALUE) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in
memory");
            }
            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}


    
> 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