Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "7b5a612", "specHash": "86fcc6c", "version": "5.15.0" }
{ "engineHash": "af01b67", "specHash": "3599cec", "version": "5.15.0" }
46 changes: 30 additions & 16 deletions src/main/java/com/box/sdkgen/internal/utils/UtilsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -311,41 +312,54 @@ public static String hexToBase64(String hex) {
public static Iterator<InputStream> iterateChunks(
InputStream stream, long chunkSize, long fileSize) {
return new Iterator<InputStream>() {
private boolean streamIsFinished = false;
private InputStream nextChunk;
private boolean isNextChunkPrepared = false;

@Override
public boolean hasNext() {
return !streamIsFinished;
}

@Override
public InputStream next() {
private void prepareNext() {
if (isNextChunkPrepared) {
return;
}
isNextChunkPrepared = 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) {
// End of stream
streamIsFinished = true;
break;
}
bytesRead += read;
}

if (bytesRead == 0) {
// No more data to yield
streamIsFinished = true;
return null;
nextChunk = null;
return;
}

// Return the chunk as a ByteArrayInputStream
return new ByteArrayInputStream(buffer, 0, bytesRead);
} catch (Exception e) {
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;
isNextChunkPrepared = false;
return result;
}
};
}

Expand Down