Skip to content

Instantly share code, notes, and snippets.

@raghunandankavi2010
Created August 13, 2020 05:32
Show Gist options
  • Save raghunandankavi2010/29da86022839a625842d4de0d5a8b306 to your computer and use it in GitHub Desktop.
Save raghunandankavi2010/29da86022839a625842d4de0d5a8b306 to your computer and use it in GitHub Desktop.
public class InputStreamRequestBody extends RequestBody {
private final MediaType contentType;
private final ContentResolver contentResolver;
private final Uri uri;
public InputStreamRequestBody(MediaType contentType, ContentResolver contentResolver, Uri uri) {
if (uri == null) throw new NullPointerException("uri == null");
this.contentType = contentType;
this.contentResolver = contentResolver;
this.uri = uri;
}
@Nullable
@Override
public MediaType contentType() {
return contentType;
}
@Override
public long contentLength() throws IOException {
return -1;
}
@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
try (Source source = Okio.source(contentResolver.openInputStream(uri))) {
sink.writeAll(source);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment