mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-22 12:08:34 +00:00
Rename TruncatingInputStream -> LimitedInputStream.
This commit is contained in:
committed by
Cody Henthorne
parent
a6767e4f8a
commit
a8fb4eb21a
@@ -15,21 +15,22 @@ import kotlin.math.min
|
||||
/**
|
||||
* An [InputStream] that will read from the target [InputStream] until it reaches the end, or until it has read [maxBytes] bytes.
|
||||
*/
|
||||
class TruncatingInputStream(private val wrapped: InputStream, private val maxBytes: Long) : FilterInputStream(wrapped) {
|
||||
class LimitedInputStream(private val wrapped: InputStream, private val maxBytes: Long) : FilterInputStream(wrapped) {
|
||||
|
||||
private var bytesRead: Long = 0
|
||||
private var totalBytesRead: Long = 0
|
||||
private var lastMark = -1L
|
||||
|
||||
override fun read(): Int {
|
||||
if (bytesRead >= maxBytes) {
|
||||
if (totalBytesRead >= maxBytes) {
|
||||
return -1
|
||||
}
|
||||
|
||||
return wrapped.read().also {
|
||||
if (it >= 0) {
|
||||
bytesRead++
|
||||
}
|
||||
val read = wrapped.read()
|
||||
if (read >= 0) {
|
||||
totalBytesRead++
|
||||
}
|
||||
|
||||
return read
|
||||
}
|
||||
|
||||
override fun read(destination: ByteArray): Int {
|
||||
@@ -37,34 +38,33 @@ class TruncatingInputStream(private val wrapped: InputStream, private val maxByt
|
||||
}
|
||||
|
||||
override fun read(destination: ByteArray, offset: Int, length: Int): Int {
|
||||
if (bytesRead >= maxBytes) {
|
||||
if (totalBytesRead >= maxBytes) {
|
||||
return -1
|
||||
}
|
||||
|
||||
val bytesRemaining: Long = maxBytes - bytesRead
|
||||
val bytesToRead: Int = if (bytesRemaining > length) length else Math.toIntExact(bytesRemaining)
|
||||
val bytesRemaining: Long = maxBytes - totalBytesRead
|
||||
val bytesToRead: Int = min(length, Math.toIntExact(bytesRemaining))
|
||||
val bytesRead = wrapped.read(destination, offset, bytesToRead)
|
||||
|
||||
if (bytesRead > 0) {
|
||||
this.bytesRead += bytesRead
|
||||
totalBytesRead += bytesRead
|
||||
}
|
||||
|
||||
return bytesRead
|
||||
}
|
||||
|
||||
override fun skip(requestedSkipCount: Long): Long {
|
||||
val bytesRemaining: Long = maxBytes - bytesRead
|
||||
val bytesRemaining: Long = maxBytes - totalBytesRead
|
||||
val bytesToSkip: Long = min(bytesRemaining, requestedSkipCount)
|
||||
val skipCount = super.skip(bytesToSkip)
|
||||
|
||||
return super.skip(bytesToSkip).also { bytesSkipped ->
|
||||
if (bytesSkipped > 0) {
|
||||
this.bytesRead += bytesSkipped
|
||||
}
|
||||
}
|
||||
totalBytesRead += skipCount
|
||||
|
||||
return skipCount
|
||||
}
|
||||
|
||||
override fun available(): Int {
|
||||
val bytesRemaining = Math.toIntExact(maxBytes - bytesRead)
|
||||
val bytesRemaining = Math.toIntExact(maxBytes - totalBytesRead)
|
||||
return min(bytesRemaining, wrapped.available())
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class TruncatingInputStream(private val wrapped: InputStream, private val maxByt
|
||||
}
|
||||
|
||||
wrapped.mark(readlimit)
|
||||
lastMark = bytesRead
|
||||
lastMark = totalBytesRead
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
@@ -91,7 +91,7 @@ class TruncatingInputStream(private val wrapped: InputStream, private val maxByt
|
||||
}
|
||||
|
||||
wrapped.reset()
|
||||
bytesRead = lastMark
|
||||
totalBytesRead = lastMark
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,6 +100,10 @@ class TruncatingInputStream(private val wrapped: InputStream, private val maxByt
|
||||
* @param byteLimit The maximum number of truncated bytes to read. Defaults to no limit.
|
||||
*/
|
||||
fun readTruncatedBytes(byteLimit: Int = -1): ByteArray {
|
||||
if (totalBytesRead < maxBytes) {
|
||||
throw IllegalStateException("Stream has not been fully read")
|
||||
}
|
||||
|
||||
return if (byteLimit < 0) {
|
||||
wrapped.readFully()
|
||||
} else {
|
||||
@@ -10,11 +10,11 @@ import org.junit.Test
|
||||
import org.signal.core.util.readFully
|
||||
import org.signal.core.util.readNBytesOrThrow
|
||||
|
||||
class TruncatingInputStreamTest {
|
||||
class LimitedInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I fully read the stream via a buffer, I should only get maxBytes`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val data = inputStream.readFully()
|
||||
|
||||
assertEquals(75, data.size)
|
||||
@@ -22,7 +22,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I fully read the stream one byte at a time, I should only get maxBytes`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
|
||||
var count = 0
|
||||
var lastRead = inputStream.read()
|
||||
@@ -36,7 +36,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I skip past the maxBytes, I should get -1`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
|
||||
val skipCount = inputStream.skip(100)
|
||||
val read = inputStream.read()
|
||||
@@ -47,7 +47,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I skip, I should still truncate correctly afterwards`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
|
||||
val skipCount = inputStream.skip(50)
|
||||
val data = inputStream.readFully()
|
||||
@@ -58,7 +58,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I skip more than maxBytes, I only skip maxBytes`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
|
||||
val skipCount = inputStream.skip(100)
|
||||
|
||||
@@ -67,7 +67,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I finish reading the stream, getTruncatedBytes gives me the rest`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
inputStream.readFully()
|
||||
|
||||
val truncatedBytes = inputStream.readTruncatedBytes()
|
||||
@@ -76,16 +76,22 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I finish reading the stream, getTruncatedBytes gives me the rest, respecting the byte limit`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
inputStream.readFully()
|
||||
|
||||
val truncatedBytes = inputStream.readTruncatedBytes(byteLimit = 10)
|
||||
assertEquals(10, truncatedBytes.size)
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException::class)
|
||||
fun `if I have not finished reading the stream, getTruncatedBytes throws IllegalStateException`() {
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
inputStream.readTruncatedBytes()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when I call available, it should respect the maxBytes`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val available = inputStream.available()
|
||||
|
||||
assertEquals(75, available)
|
||||
@@ -93,7 +99,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I call available after reading some bytes, it should respect the maxBytes`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
inputStream.readNBytesOrThrow(50)
|
||||
|
||||
val available = inputStream.available()
|
||||
@@ -103,7 +109,7 @@ class TruncatingInputStreamTest {
|
||||
|
||||
@Test
|
||||
fun `when I mark and reset, it should jump back to the correct position`() {
|
||||
val inputStream = TruncatingInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
|
||||
|
||||
inputStream.mark(100)
|
||||
inputStream.readNBytesOrThrow(10)
|
||||
Reference in New Issue
Block a user