Throw exception when trying to read after end of stream
This commit is contained in:
parent
f64ea09a1b
commit
af916f39e9
@ -7,6 +7,7 @@ import java.io.InputStream
|
|||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.function.Predicate
|
import java.util.function.Predicate
|
||||||
|
import kotlin.NoSuchElementException
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
interface IStreamCodec<V> {
|
interface IStreamCodec<V> {
|
||||||
@ -137,6 +138,10 @@ fun InputStream.readDouble() = Double.fromBits(readLong())
|
|||||||
fun InputStream.readVarIntLE(): Int {
|
fun InputStream.readVarIntLE(): Int {
|
||||||
val readFirst = read()
|
val readFirst = read()
|
||||||
|
|
||||||
|
if (readFirst < 0) {
|
||||||
|
throw NoSuchElementException("Reached end of stream")
|
||||||
|
}
|
||||||
|
|
||||||
if (readFirst and 64 == 0) {
|
if (readFirst and 64 == 0) {
|
||||||
return if (readFirst and 128 != 0) -(readFirst and 63) else readFirst and 63
|
return if (readFirst and 128 != 0) -(readFirst and 63) else readFirst and 63
|
||||||
}
|
}
|
||||||
@ -149,6 +154,11 @@ fun InputStream.readVarIntLE(): Int {
|
|||||||
while (nextBit != 0) {
|
while (nextBit != 0) {
|
||||||
result = result or (read shl i)
|
result = result or (read shl i)
|
||||||
read = read()
|
read = read()
|
||||||
|
|
||||||
|
if (read < 0) {
|
||||||
|
throw NoSuchElementException("Reached end of stream")
|
||||||
|
}
|
||||||
|
|
||||||
nextBit = read and 128
|
nextBit = read and 128
|
||||||
read = read and 127
|
read = read and 127
|
||||||
|
|
||||||
@ -175,6 +185,10 @@ fun OutputStream.writeVarIntLE(value: Int) {
|
|||||||
fun InputStream.readVarLongLE(): Long {
|
fun InputStream.readVarLongLE(): Long {
|
||||||
val readFirst = read()
|
val readFirst = read()
|
||||||
|
|
||||||
|
if (readFirst < 0) {
|
||||||
|
throw NoSuchElementException("Reached end of stream")
|
||||||
|
}
|
||||||
|
|
||||||
if (readFirst and 64 == 0) {
|
if (readFirst and 64 == 0) {
|
||||||
return if (readFirst and 128 != 0) -(readFirst and 63).toLong() else (readFirst and 63).toLong()
|
return if (readFirst and 128 != 0) -(readFirst and 63).toLong() else (readFirst and 63).toLong()
|
||||||
}
|
}
|
||||||
@ -187,6 +201,11 @@ fun InputStream.readVarLongLE(): Long {
|
|||||||
while (nextBit != 0) {
|
while (nextBit != 0) {
|
||||||
result = result or (read shl i).toLong()
|
result = result or (read shl i).toLong()
|
||||||
read = read()
|
read = read()
|
||||||
|
|
||||||
|
if (read < 0) {
|
||||||
|
throw NoSuchElementException("Reached end of stream")
|
||||||
|
}
|
||||||
|
|
||||||
nextBit = read and 128
|
nextBit = read and 128
|
||||||
read = read and 127
|
read = read and 127
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user