jessewilson
12/17/2024, 2:19 PMSuhaib Kazi
12/17/2024, 2:23 PMjessewilson
12/17/2024, 2:49 PMRequestBody
, and you can create instances of these that stream data from wherever it originatesSuhaib Kazi
12/18/2024, 7:42 AMval requestFile = mediaFile.asRequestBody(MediaType.let { "multipart/form-data".toMediaTypeOrNull() })
If you look into it, its all loaded again as a single bytes streamSuhaib Kazi
12/18/2024, 7:47 AM@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) }
}
}
}
/** Returns a source that reads from `file`. */
@Throws(FileNotFoundException::class)
fun File.source(): Source = InputStreamSource(inputStream(), Timeout.NONE)
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 thisjessewilson
12/18/2024, 12:13 PMjessewilson
12/18/2024, 12:13 PM