Skip to content
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

Add ability to use InputStream as source of RequestBody #1038

Closed
wants to merge 2 commits into from

Conversation

mpost
Copy link

@mpost mpost commented Aug 28, 2014

Currently you can only use static resources when creating a RequestBody. In practice it can be beneficial to provide an InputStream as the RequestBody data source. For example an input stream can be monitored during the upload so that a use can be provided with feedback about the upload progress.

@@ -98,4 +99,31 @@ public static RequestBody create(final MediaType contentType, final File file) {
}
};
}

/** Returns a new request body that transmits the content of an {@code InputStream}. */
public static RequestBody create(final MediaType contentType, final InputStream is,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename is to inputStream

(that way the parameter name in your exception lines up with the parameter name in the declaration)

@swankjesse
Copy link
Collaborator

Neat.

I need a CLA https://github.com/square/okhttp/blob/master/CONTRIBUTING.md
And a simple test. (that reads from ByteArrayInputStream or similar)

@mpost
Copy link
Author

mpost commented Aug 28, 2014

Agreement is signed. I will make the changes asap. Please stay tuned.

Makes sure the InputStream is reset after use so that the stream
can be read again if supported.
@mpost
Copy link
Author

mpost commented Aug 29, 2014

The build became stale on some unrelated test. Is there anything i can do about that?

@smaspe
Copy link

smaspe commented Sep 17, 2014

It looks like the retry mechanism gets in the way of using an InputStream. writeTo gets called twice. You probably need a mark/reset mechanism

Edit: I just saw that there is indeed a reset in the writeTo finally clause. However, android contentproviders will for some reason give you non-resetable inputstreams, in which case I guess the next call to writeTo will crash

@swankjesse
Copy link
Collaborator

Sorry for the delay looking at this.

Because we can call the method multiple times, I don't think we can accept a raw input stream. Instead the implementation should look up a new input stream each time. Mark/reset doesn't really fix the problem because those methods cause the entire stream body to be buffered, which is quite inefficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants