-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added two progress indicators to inform of the upload/download of byt… #328
Changes from 4 commits
d7dc44e
26d9d01
3b028d0
920e7e4
9fb5dd6
ac0b4d7
89ed1ee
3dafd56
f504669
ff18fe1
6b1568c
cea3b7f
d5cad0f
0f5b553
97df8d6
c8a7083
d31dd6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
|
@@ -107,11 +108,12 @@ public HttpResponse executeRequest(Request<?> request, Map<String, String> addit | |
// Need to keep the connection open until the stream is consumed by the caller. Wrap the | ||
// stream such that close() will disconnect the connection. | ||
keepConnectionOpen = true; | ||
int contentLength = connection.getContentLength (); | ||
return new HttpResponse( | ||
responseCode, | ||
convertHeaders(connection.getHeaderFields()), | ||
connection.getContentLength(), | ||
new UrlConnectionInputStream(connection)); | ||
createInputStream(request, connection, responseCode, contentLength)); | ||
} finally { | ||
if (!keepConnectionOpen) { | ||
connection.disconnect(); | ||
|
@@ -153,7 +155,7 @@ private static boolean hasResponseBody(int requestMethod, int responseCode) { | |
* Wrapper for a {@link HttpURLConnection}'s InputStream which disconnects the connection on | ||
* stream close. | ||
*/ | ||
static class UrlConnectionInputStream extends FilterInputStream { | ||
private static class UrlConnectionInputStream extends FilterInputStream { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just leave this unchanged. I'm not sure if anything is using it but it shouldn't impact this PR. (I was concerned about increasing this classes visibility to protected, but it's fine to leave as is). |
||
private final HttpURLConnection mConnection; | ||
|
||
UrlConnectionInputStream(HttpURLConnection connection) { | ||
|
@@ -168,10 +170,25 @@ public void close() throws IOException { | |
} | ||
} | ||
|
||
/** | ||
* Overload this method for manipulate response stream (Use 'write (byte[] b, int off, int len)' in subclasse). | ||
* | ||
* @param request current request. | ||
* @param connection current connection of request. | ||
* @param responseCode response code. | ||
* @param length size of response stream. | ||
* @return an UrlConnectionInputStream object for read response. | ||
*/ | ||
protected FilterInputStream createInputStream (Request<?> request, HttpURLConnection connection, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the return type here should be InputStream, not FilterInputStream. Volley doesn't care/depend on what kind of InputStream gets returned here - any InputStream is fine. The fact that we use FilterInputStream here and in your (presumed) subclass isn't a requirement, just an implementation detail. |
||
int responseCode, int length) { | ||
|
||
return new UrlConnectionInputStream (connection); | ||
} | ||
|
||
/** | ||
* Initializes an {@link InputStream} from the given {@link HttpURLConnection}. | ||
* | ||
* @param connection | ||
* @param connection Request connection | ||
* @return an HttpEntity populated with data from <code>connection</code>. | ||
*/ | ||
private static InputStream inputStreamFromConnection(HttpURLConnection connection) { | ||
|
@@ -223,7 +240,7 @@ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOE | |
// NOTE: Any request headers added here (via setRequestProperty or addRequestProperty) should be | ||
// checked against the existing properties in the connection and not overridden if already set. | ||
@SuppressWarnings("deprecation") | ||
/* package */ static void setConnectionParametersForRequest( | ||
/* package */ void setConnectionParametersForRequest( | ||
HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { | ||
switch (request.getMethod()) { | ||
case Method.DEPRECATED_GET_OR_POST: | ||
|
@@ -270,15 +287,15 @@ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOE | |
} | ||
} | ||
|
||
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) | ||
private void addBodyIfExists(HttpURLConnection connection, Request<?> request) | ||
throws IOException, AuthFailureError { | ||
byte[] body = request.getBody(); | ||
if (body != null) { | ||
addBody(connection, request, body); | ||
} | ||
} | ||
|
||
private static void addBody(HttpURLConnection connection, Request<?> request, byte[] body) | ||
private void addBody(HttpURLConnection connection, Request<?> request, byte[] body) | ||
throws IOException { | ||
// Prepare output. There is no need to set Content-Length explicitly, | ||
// since this is handled by HttpURLConnection using the size of the prepared | ||
|
@@ -289,8 +306,22 @@ private static void addBody(HttpURLConnection connection, Request<?> request, by | |
connection.setRequestProperty( | ||
HttpHeaderParser.HEADER_CONTENT_TYPE, request.getBodyContentType()); | ||
} | ||
DataOutputStream out = new DataOutputStream(connection.getOutputStream()); | ||
OutputStream out = this.createOutputStream ( request, connection, body.length ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be: DataOutputStream out = new DataOutputStream(createOutputStream(request, connection, body.length)); |
||
out.write(body); | ||
out.close(); | ||
out.close (); | ||
} | ||
|
||
/** | ||
* Overload this method for manipulate request stream (Use 'write (byte[] b)' in subclasse). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two comments here:
|
||
* | ||
* @param request current request. | ||
* @param connection current connection of request. | ||
* @param length size of stream to write. | ||
* @return an OutputStream object for write request body. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "@return an OutputStream to which the request body will be written" |
||
*/ | ||
protected OutputStream createOutputStream (Request<?> request, HttpURLConnection connection, | ||
int length) throws IOException { | ||
|
||
return new DataOutputStream (connection.getOutputStream ()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return connection.getOutputStream() |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just leave this unchanged. I'm not sure if anything is using it but it shouldn't impact this PR. (I was concerned about increasing this classes visibility to protected, but it's fine to leave as is).