Don't recreate attachment InputStream if we don't have to.

This commit is contained in:
Nicholas Tinsley
2024-01-31 14:02:29 -05:00
parent d1ef9d5dcf
commit 467dae8132
3 changed files with 181 additions and 17 deletions

View File

@@ -5,9 +5,9 @@
package org.signal.core.util
import java.io.EOFException
import java.io.IOException
import java.io.InputStream
import kotlin.jvm.Throws
/**
* Reads a 32-bit variable-length integer from the stream.
@@ -80,3 +80,26 @@ fun InputStream.readLength(): Long {
return count
}
/**
* Backported from AOSP API 31 source code.
*
* @param count number of bytes to skip
*/
@Throws(IOException::class)
fun InputStream.skipNBytesCompat(count: Long) {
var n = count
while (n > 0) {
val ns = skip(n)
if (ns in 1..n) {
n -= ns
} else if (ns == 0L) {
if (read() == -1) {
throw EOFException()
}
n--
} else {
throw IOException("Unable to skip exactly")
}
}
}