I don’t think it strictly has to load the whole th...
# squarelibraries
j
I don’t think it strictly has to load the whole thing into memory
s
Correct, if we are able to chunk the entire file and have retry/failure mechanism for chunking upload then it shouldnt be a problem.
j
Each part’s type is a
RequestBody
, and you can create instances of these that stream data from wherever it originates
s
That would be the details of the entire request and the actual file too right?
Copy code
val requestFile = mediaFile.asRequestBody(MediaType.let { "multipart/form-data".toMediaTypeOrNull() })
If you look into it, its all loaded again as a single bytes stream
Copy code
@JvmStatic
    @JvmName("create")
    fun File.asRequestBody(contentType: MediaType? = null): RequestBody {
      return object : RequestBody() {
        override fun contentType() = contentType

        override fun contentLength() = length()

        override fun writeTo(sink: BufferedSink) {
          source().use { source -> sink.writeAll(source) }
        }
      }
    }
Copy code
/** Returns a source that reads from `file`. */
@Throws(FileNotFoundException::class)
fun File.source(): Source = InputStreamSource(inputStream(), Timeout.NONE)
Copy code
public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        // BEGIN Android-changed: Open files using IoBridge to share BlockGuard & StrictMode logic.
        // <http://b/112107427>
        // fd = new FileDescriptor();
        fd = IoBridge.open(name, O_RDONLY);
        // END Android-changed: Open files using IoBridge to share BlockGuard & StrictMode logic.

        // Android-changed: Tracking mechanism for FileDescriptor sharing.
        // fd.attach(this);
        isFdOwner = true;

        path = name;

        // Android-removed: Open files using IoBridge to share BlockGuard & StrictMode logic.
        // open(name);

        // Android-added: File descriptor ownership tracking.
        IoUtils.setFdOwner(this.fd, this);

        // Android-added: CloseGuard support.
        guard.open("close");
    }
And then finally this
j
This code isn’t loading the entire file into memory
writeAll reads the source stream about 8 KiB at a time