Skip to content

LargeFileUploadTask may not fully read input stream, uploaded file is corrupted #1923

@worpet

Description

@worpet

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:

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 than length 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:
Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedStandard GitHub label

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions