fix: loop FileChannel.transferTo() — it can transfer fewer bytes than requested without looping, silently truncating files
Build & Deploy / build-and-deploy (push) Successful in 43s
Build & Deploy / build-and-deploy (push) Successful in 43s
This commit is contained in:
@@ -171,7 +171,15 @@ public class StaticResource {
|
|||||||
} else {
|
} else {
|
||||||
// Partial content (Range request) — transfer only the requested bytes
|
// Partial content (Range request) — transfer only the requested bytes
|
||||||
try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) {
|
try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) {
|
||||||
ch.transferTo(offset, length, java.nio.channels.Channels.newChannel(output));
|
long remaining = length;
|
||||||
|
long pos = offset;
|
||||||
|
while (remaining > 0) {
|
||||||
|
long written = ch.transferTo(pos, remaining,
|
||||||
|
java.nio.channels.Channels.newChannel(output));
|
||||||
|
if (written <= 0) break;
|
||||||
|
pos += written;
|
||||||
|
remaining -= written;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user