Skip to content

Commit

Permalink
Fix BufferedInputStream.read() for values bigger than 0x7f (scala-nat…
Browse files Browse the repository at this point in the history
…ive#1922)

`read()` should return a value between `-1` and `255` and `-1` means `EOF`.
  • Loading branch information
catap authored and ekrich committed May 21, 2021
1 parent 479d717 commit 06651cd
Show file tree
Hide file tree
Showing 2 changed files with 12 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
11 changes: 11 additions & 0 deletions unit-tests/src/test/scala/java/io/BufferedInputStreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ 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 06651cd

Please sign in to comment.