write to disk
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user