Skip to content

Commit a269f97

Browse files
authored
Fixed NoSuchMethodError when parsing a JSON stream on Java 8 (#2328)
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.
1 parent 881e7a9 commit a269f97

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ internal class CharsetReader(
103103
val remaining = if (position <= limit) limit - position else 0
104104
val bytesRead = inputStream.read(byteBuffer.array(), byteBuffer.arrayOffset() + position, remaining)
105105
if (bytesRead < 0) return bytesRead
106-
byteBuffer.position(position + bytesRead)
106+
// Method `position(I)LByteBuffer` does not exist in Java 8. For details, see comment for `flip` in `init` method
107+
(byteBuffer as Buffer).position(position + bytesRead)
107108
} finally {
108109
(byteBuffer as Buffer).flip() // see the `init` block in this class for the reasoning behind the cast
109110
}

0 commit comments

Comments
 (0)