Skip to content

BUG in Chunked Upload for Big Files #1957

Description

@LeoKwongVersaFile

Description of the Issue

When uploading a file larger than 50MB, Chunked Uploads should be used, however, if the file size is exactly multiples of 8MB then a NullPointerException is thrown.

Steps to Reproduce

  1. Create a file that is exactly 64MiB sized locally
  2. Upload to box using BoxClient.chunkedUpload.uploadBigFile(inputStream, fileName, fileSize, folderId)
  3. NullPointerException will be thrown.

Expected Behavior

Expect to upload file successfully.

Issue Found in Version

box-java-sdk 10.16.0
In com.box.sdkgen.internal.utils.UtilsManager, in the method iterateChunks
if a chunk exactly divisible by 8MB then it returns a null InputStream

Proposed fix

public static Iterator iterateChunks(
InputStream stream, long chunkSize, long fileSize) {
return new Iterator() {

        private InputStream nextChunk;
        private boolean initialized = false;

        private void prepareNext() {
            if (initialized) {
                return;
            }

            initialized = true;

            try {
                byte[] buffer = new byte[(int) chunkSize];
                int bytesRead = 0;

                while (bytesRead < chunkSize) {
                    int read = stream.read(
                        buffer,
                        bytesRead,
                        (int) (chunkSize - bytesRead)
                    );

                    if (read == -1) {
                        break;
                    }

                    bytesRead += read;
                }

                if (bytesRead == 0) {
                    nextChunk = null;
                    return;
                }

                nextChunk = new ByteArrayInputStream(buffer, 0, bytesRead);

            } catch (IOException e) {
                throw new RuntimeException("Error reading from stream", e);
            }
        }

        @Override
        public boolean hasNext() {
            prepareNext();
            return nextChunk != null;
        }

        @Override
        public InputStream next() {
            prepareNext();

            if (nextChunk == null) {
                throw new NoSuchElementException();
            }

            InputStream result = nextChunk;

            nextChunk = null;
            initialized = false;

            return result;
        }
    };

}

Metadata

Metadata

Labels

bugAdded to issues that describes SDK bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions