Make LimitedInputStream less strict.

This commit is contained in:
Greyson Parrelli
2024-09-18 07:05:29 -04:00
parent 1f638db959
commit fcb2e3cc74
2 changed files with 4 additions and 7 deletions

View File

@@ -5,6 +5,7 @@
package org.signal.core.util.stream package org.signal.core.util.stream
import org.signal.core.util.logging.Log
import java.io.FilterInputStream import java.io.FilterInputStream
import java.io.InputStream import java.io.InputStream
import java.lang.UnsupportedOperationException import java.lang.UnsupportedOperationException
@@ -24,6 +25,8 @@ class LimitedInputStream(private val wrapped: InputStream, private val maxBytes:
private const val UNLIMITED = -1L private const val UNLIMITED = -1L
private val TAG = Log.tag(LimitedInputStream::class)
/** /**
* Returns a [LimitedInputStream] that doesn't limit the stream at all -- it'll allow reading the full thing. * Returns a [LimitedInputStream] that doesn't limit the stream at all -- it'll allow reading the full thing.
*/ */
@@ -143,7 +146,7 @@ class LimitedInputStream(private val wrapped: InputStream, private val maxBytes:
} }
if (totalBytesRead < maxBytes) { if (totalBytesRead < maxBytes) {
throw IllegalStateException("Stream has not been fully read") Log.w(TAG, "Reading leftover stream when the stream has not been fully read! maxBytes is $maxBytes, but we've only read $totalBytesRead")
} }
return wrapped return wrapped

View File

@@ -96,12 +96,6 @@ class LimitedInputStreamTest {
assertEquals(25, truncatedBytes.size) assertEquals(25, truncatedBytes.size)
} }
@Test(expected = IllegalStateException::class)
fun `if I have not finished reading the stream, leftoverStream throws IllegalStateException`() {
val inputStream = LimitedInputStream(ByteArray(100).inputStream(), maxBytes = 75)
inputStream.leftoverStream()
}
@Test @Test
fun `when call leftoverStream on a stream with no limit, it returns an empty array`() { fun `when call leftoverStream on a stream with no limit, it returns an empty array`() {
val inputStream = LimitedInputStream.withoutLimits(ByteArray(100).inputStream()) val inputStream = LimitedInputStream.withoutLimits(ByteArray(100).inputStream())