write to disk

This commit is contained in:
pratik
2025-10-21 16:19:06 -05:00
parent b05bee8f28
commit 6e3684b580
2 changed files with 21 additions and 9 deletions

View File

@@ -61,16 +61,26 @@ public class ChunkedUploadService {
fileType, fileState.getTotalChunks(), totalChunks));
}
// Write chunk to file
// Write chunk to file using streaming to avoid loading entire chunk into memory
Path targetPath = fileState.getTargetPath();
long offset = (long) chunkIndex * getChunkSize();
try (RandomAccessFile raf = new RandomAccessFile(targetPath.toFile(), "rw")) {
try (RandomAccessFile raf = new RandomAccessFile(targetPath.toFile(), "rw");
var inputStream = chunk.getInputStream()) {
raf.seek(offset);
byte[] data = chunk.getBytes();
raf.write(data);
// Stream chunk data in smaller buffers to reduce memory pressure
byte[] buffer = new byte[8192]; // 8KB buffer
int bytesRead;
long totalWritten = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
raf.write(buffer, 0, bytesRead);
totalWritten += bytesRead;
}
log.debug("Wrote chunk {} ({} bytes) to {} at offset {}",
chunkIndex, data.length, targetPath.getFileName(), offset);
chunkIndex, totalWritten, targetPath.getFileName(), offset);
}
fileState.markChunkReceived(chunkIndex);