mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-20 02:58:45 +00:00
Make LimitedInputStream less strict.
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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())
|
||||||
|
|||||||
Reference in New Issue
Block a user