code: public final void writeStream(InputStream stream, OnStreamWriteListener listener) throws IOException {
          if (listener == null)
              throw new IllegalArgumentException("listener MUST not be null!");
  
          byte[] buffer = new byte[UploadService.BUFFER_SIZE];
          int bytesRead;
  
          try {
              while (listener.shouldContinueWriting() && (bytesRead = stream.read(buffer, 0, buffer.length)) > 0) {
                  write(buffer, bytesRead);
                  flush();
                  listener.onBytesWritten(bytesRead);
              }
          } finally {
              stream.close();
          }
      }
docs: Writes an input stream to the request body.
  The stream will be automatically closed after successful write or if an exception is thrown.
  @param stream input stream from which to read
  @param listener listener which gets notified when bytes are written and which controls if
  the transfer should continue
  @throws IOException if an I/O error occurs