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
- Create a file that is exactly 64MiB sized locally
- Upload to box using BoxClient.chunkedUpload.uploadBigFile(inputStream, fileName, fileSize, folderId)
- 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;
}
};
}
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
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() {
}