Skip to content

Instantly share code, notes, and snippets.

@raghunandankavi2010
Last active January 24, 2021 15:13
Show Gist options
  • Save raghunandankavi2010/4cd62bc1c58751e02089dec90d7cd094 to your computer and use it in GitHub Desktop.
Save raghunandankavi2010/4cd62bc1c58751e02089dec90d7cd094 to your computer and use it in GitHub Desktop.
Request Body for input stream
   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 {
    sink.writeAll(Okio.source(contentResolver.openInputStream(uri)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment