Skip to content

Commit

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

Fixes Kotlin#2326

An explicit cast is needed here due to an API change in Java 9, see Kotlin#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 JesusMcCloud committed Jul 5, 2023
1 parent 963f338 commit 940022f
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 940022f

Please sign in to comment.