diff options
author | Sergey Shanshin <sergey.shanshin@jetbrains.com> | 2023-06-13 16:37:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 15:37:25 +0200 |
commit | a269f97ea00d71a1310e1a0bc237010f72f54cb7 (patch) | |
tree | 0bc26b896c425d5353a9fbac62a787abd3fa06b4 | |
parent | 881e7a91e722fdaad180db298df807c597bb065c (diff) | |
download | kotlinx.serialization-a269f97ea00d71a1310e1a0bc237010f72f54cb7.tar.gz |
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.
-rw-r--r-- | formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt b/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt index 61fc7910..d7c606bd 100644 --- a/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt +++ b/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt @@ -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 } |