-
Notifications
You must be signed in to change notification settings - Fork 30
Description
Describe the bug
I am seeing a problem with file contents not fully uploading. OneDrive reports the file size as correct, but the file is corrupted. On closer inspection, after a point, the data in the file is all empty bytes.
The problem seems to be in LargeFileUploadTask
, in this method:
msgraph-sdk-java-core/src/main/java/com/microsoft/graph/core/tasks/LargeFileUploadTask.java
Lines 275 to 280 in 9018958
private byte[] chunkInputStream(InputStream stream, int length) throws IOException { | |
byte[] buffer = new byte[length]; | |
int lengthAssert = stream.read(buffer); | |
assert lengthAssert == length; | |
return buffer; | |
} |
Your method assumes that a single call to InputStream.read(byte[])
will fill the entire buffer. But InputStream.read(byte[])
does not guarantee it will read the requested number of bytes in one call, which leads to an incompletely filled buffer and file data lost.
stream.read(buffer)
may return fewer thanlength
bytes, even if more bytes are available later.- This can occur with slow, buffered, or network-based streams.
- The use of
assert lengthAssert == length
fails only in debug mode and does not affect production code. - If the assertion fails, the method returns an incompletely filled buffer, leading to lost data and a corrupted file uploaded to OneDrive.
Expected behavior
File contents should always fully upload. File contents should not be missing and replaced with 0 bytes.
How to reproduce
Use chunkInputStream
with:
- A stream source that throttles data (a socket or piped stream).
- A buffer size larger than what is immediately available.
SDK Version
6.x
Latest version known to work for scenario above?
5.x
Known Workarounds
No response
Debug output
Viewing affected file in OneDrive:
Configuration
No response
Other information
To fix it you should replace the single read call with a loop that ensures the full buffer is read.