Improve memory usage of new APNG renderer by making it streaming.

This commit is contained in:
Greyson Parrelli
2026-03-20 21:50:12 -04:00
committed by Cody Henthorne
parent 48374e6950
commit 25b01a30be
13 changed files with 485 additions and 341 deletions

View File

@@ -139,6 +139,27 @@ fun InputStream.copyTo(outputStream: OutputStream, closeInputStream: Boolean = t
return StreamUtil.copy(this, outputStream, closeInputStream, closeOutputStream)
}
/**
* Skips exactly [n] bytes from this stream. Unlike [InputStream.skip], this method
* guarantees all bytes are skipped by looping and falling back to [read] if needed.
*
* @throws IOException if the stream ends before [n] bytes have been skipped.
*/
@Throws(IOException::class)
fun InputStream.skipNBytesOrThrow(n: Long) {
var remaining = n
while (remaining > 0) {
val skipped = skip(remaining)
if (skipped > 0) {
remaining -= skipped
} else if (read() == -1) {
throw IOException("Stream ended before $n bytes could be skipped (${n - remaining} skipped)")
} else {
remaining--
}
}
}
/**
* Returns true if every byte in this stream matches the predicate, otherwise false.
*/