Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support use of outputstream as destination of download

  • Loading branch information...
commit 24554a3281017b20ff3f12e8a1e71a4a51f57a76 1 parent 4ce9d66
@thiagomoretto authored
View
42 src/br/eng/moretto/me/MEHTTPRequest.java
@@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
@@ -27,6 +28,7 @@
// request
URL url;
String destinationPath = null;
+ OutputStream destinationOutputStream = null;
MEHTTPRequest.Listener didRequestFinishedListener = null;
MEHTTPRequest.Listener didRequestFailedListener = null;
MEHTTPRequest.Listener didRequestRedirectedListener = null;
@@ -64,10 +66,17 @@ public MEHTTPRequest setDidRequestFailedListener(MEHTTPRequest.Listener listener
}
public MEHTTPRequest setDownloadDestinationPath(String path) {
+ this.destinationOutputStream = null;
this.destinationPath = path;
return this;
}
+ public MEHTTPRequest setDestinationOutputStream(OutputStream destinationOutputStream) {
+ this.destinationPath = null;
+ this.destinationOutputStream = destinationOutputStream;
+ return this;
+ }
+
public MEHTTPRequest setShouldAllowResumeDownloads(boolean shouldAllowResumeDownloads) {
this.shouldAllowResumeDownloads = shouldAllowResumeDownloads;
return this;
@@ -153,12 +162,18 @@ else if (shouldFollowRedirects &&
callListenerIfPresent(didRequestRedirectedListener);
}
else {
- if (destinationPath != null)
+ if (destinationOutputStream != null)
+ {
+ saveToOutputStream(urlConnection);
+ callListenerIfPresent(didRequestFinishedListener);
+ }
+ else if (destinationPath != null)
{
File fd = new File(destinationPath);
if (fd.isDirectory())
throw new Exception("Destination path is a directory. Please, append a file name.");
saveToDestination(fd, urlConnection, shouldAllowResumeDownloads);
+ callListenerIfPresent(didRequestFinishedListener);
}
else
{
@@ -187,6 +202,31 @@ else if (shouldFollowRedirects &&
}
}
+ private void saveToOutputStream(HttpURLConnection urlConnection) throws IOException {
+ totalContentReaded = 0;
+ BufferedInputStream bufin = null;
+ try
+ {
+ bufin = new BufferedInputStream(urlConnection.getInputStream());
+ urlConnection.getContentLength();
+ int count;
+ byte[] buf = new byte[1024];
+ while ((count = bufin.read(buf)) != -1)
+ {
+ destinationOutputStream.write(buf, 0, count);
+ totalContentReaded += count;
+ }
+ destinationOutputStream.flush();
+ }
+ finally
+ {
+ if (destinationOutputStream != null)
+ destinationOutputStream.close();
+ if (bufin != null)
+ bufin.close();
+ }
+ }
+
private void saveToDestination(File fd, HttpURLConnection urlConnection, boolean append) throws Exception
{
totalContentReaded = 0;
View
22 test/br/eng/moretto/me/tests/MEHTTPRequestTests.java
@@ -1,6 +1,8 @@
package br.eng.moretto.me.tests;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
@@ -154,6 +156,26 @@ public void shouldMakeGetRequestAndSaveContentOfFileAllowingResume() throws Malf
Assert.assertEquals(fi.length(), fd.length());
Assert.assertEquals(fi.length() - 1024, request.getTotalContentReaded());
}
+
+ @Test
+ public void shouldMakeGetRequestAndWriteContentToFileOutputStream() throws MalformedURLException, FileNotFoundException {
+ httpTestServer.setMockResponseCode(200);
+
+ FileOutputStream fous = new FileOutputStream(new File("/tmp/temp_file_os"));
+
+ URL testURL = new URL(urlString);
+ MEHTTPRequest request = new MEHTTPRequest(testURL);
+ request .setShouldFollowRedirects(true)
+ .setDidRequestRedirectedListener(stdOutRequestDidRedirectedListener)
+ .setDidRequestFinishedListener(stdOutRequestDidFinishedListener)
+ .setDidRequestFailedListener(stdErrRequestDidFailedListener)
+ .setDestinationOutputStream(fous)
+ .startSynchronous();
+
+ Assert.assertEquals(200, request.getResponseCode());
+ Assert.assertTrue(new File("/tmp/temp_file_os").exists());
+ Assert.assertTrue(stdOutRequestDidFinishedListenerWasCalled);
+ }
private void printHeaders(Map<String, List<String>> headers)
{

0 comments on commit 24554a3

Please sign in to comment.
Something went wrong with that request. Please try again.