Skip to content

Commit

Permalink
Fixed read value bigger than 0x7f via BufferedInputStream
Browse files Browse the repository at this point in the history
`read()` should return a value between `-1` and `255` and `-1` means `EOF`.

When `buf(pos)` contains value bigger than `0x75` for example `171.toByte` and
makes `.toInt`, it converts this value to `-85` that can be regarded by enduser
as `EOF` if he uses `< 0` condition.

Unfortunately this bug hasn't got workaround on user side because when stream
contains `0xff`, the `read()` returns `-1` and it is impossibly to distinguish
the `EOF` and `0xff`.
  • Loading branch information
catap committed Oct 7, 2020
1 parent af4eeb7 commit dd6b533
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion javalib/src/main/scala/java/io/BufferedInputStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BufferedInputStream(_in: InputStream, size: Int)
ensureOpen()

if (prepareRead()) {
val res = buf(pos).toInt
val res = buf(pos).toInt & 0xff
pos += 1
res
} else -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ object BufferedInputStreamSuite extends tests.Suite {

}

test("read value bigger than 0x7f") {
val inputArray = Array[Byte](0x85.toByte)
val arrayIn = new ByteArrayInputStream(inputArray)
val in = new BufferedInputStream(arrayIn)

assert(in.read() == 0x85)
assert(in.read() == -1)
}

test("read to closed buffer throws IOException") {

val inputArray =
Expand Down

0 comments on commit dd6b533

Please sign in to comment.