Skip to content

Commit

Permalink
Fixed NoSuchMethodError when parsing a JSON stream on Java 8 (#2328)
Browse files Browse the repository at this point in the history
Fixes #2326

An explicit cast is needed here due to an API change in Java 9, see #2218.
In Java 8 and earlier, the `position(I)` method was final in `Buffer`, and returned a `Buffer`.
In Java 9 and later, the method was opened, and `ByteBuffer` overrides it, returning a `ByteBuffer`.
This causes a `NoSuchMethodError` when running a class, compiled with a newer Java version, on Java 8.
  • Loading branch information
shanshin authored and woainikk committed Jun 15, 2023
1 parent 3a63b86 commit 12c0f07
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ internal class CharsetReader(
val remaining = if (position <= limit) limit - position else 0
val bytesRead = inputStream.read(byteBuffer.array(), byteBuffer.arrayOffset() + position, remaining)
if (bytesRead < 0) return bytesRead
byteBuffer.position(position + bytesRead)
// Method `position(I)LByteBuffer` does not exist in Java 8. For details, see comment for `flip` in `init` method
(byteBuffer as Buffer).position(position + bytesRead)
} finally {
(byteBuffer as Buffer).flip() // see the `init` block in this class for the reasoning behind the cast
}
Expand Down

0 comments on commit 12c0f07

Please sign in to comment.