artiya4u / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

HttpResponse.download(OutputStream) #81

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

http://stackoverflow.com/questions/9941378/how-to-download-a-large-file-using-go
ogle-api-java-client-on-android

http://javadoc.google-http-java-client.googlecode.com/hg/1.8.3-beta/com/google/a
pi/client/http/HttpResponse.html

Java environments (e.g. Java 6, Android 2.3, App Engine, or All)?

All

Please describe the feature requested.

Here's an idea: suppose you want to just download the content of a response 
into a file, e.g. if you are downloading an image.  A good way to do it is to 
use AbstractInputStreamContent.copy as in this example:

    HttpTransport transport = new NetHttpTransport();
    HttpRequestFactory requestFactory = transport.createRequestFactory();
    HttpRequest request =
        requestFactory.buildGetRequest(new GenericUrl(
            "https://www.google.com/images/srpr/logo3w.png"));
    OutputStream outputStream =
        new FileOutputStream(new File("/tmp/logo3w.png"));
    HttpResponse response = request.execute();
    InputStream inputStream = response.getContent();
    AbstractInputStreamContent.copy(inputStream, outputStream, true);

Unfortunately, AbstractInputStreamContent.copy is hard to discover.  Perhaps we 
can make it easier to find by adding an HttpResponse.download(OutputStream) 
method, so now the last 2 lines would now be:

    response.download(outputStream);

We could also potentially add something similar in JsonHttpRequest and 
JsonHttpClient.

Original issue reported on code.google.com by yan...@google.com on 7 Apr 2012 at 12:34

GoogleCodeExporter commented 9 years ago
This idea sounds good to me.
Sent out http://codereview.appspot.com/5985069/

Original comment by rmis...@google.com on 9 Apr 2012 at 12:56

GoogleCodeExporter commented 9 years ago

Original comment by rmis...@google.com on 11 Apr 2012 at 2:57