From 39fe976a4cd7e4915d8cda44fc6ba51cfbc711ab Mon Sep 17 00:00:00 2001 From: "Sergey.Shanshin" Date: Mon, 12 Jun 2023 11:21:46 +0200 Subject: [PATCH 1/2] Fixed NoSuchMethodError when parsing a JSON stream on Java 8 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 `ByteFuffer` overrides it, returning a `ByteBuffer`. This causes a `NoSuchMethodError` when running a class, compiled with a newer Java version, on Java 8. --- .../src/kotlinx/serialization/json/internal/CharsetReader.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 61fc791098..28a64704d6 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,7 @@ 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) + (byteBuffer as Buffer).position(position + bytesRead) } finally { (byteBuffer as Buffer).flip() // see the `init` block in this class for the reasoning behind the cast } From a4ecdfff5f68069ebed4c4e8db9479c6463b038b Mon Sep 17 00:00:00 2001 From: "Sergey.Shanshin" Date: Tue, 13 Jun 2023 14:58:48 +0200 Subject: [PATCH 2/2] ~review fixes --- .../src/kotlinx/serialization/json/internal/CharsetReader.kt | 1 + 1 file changed, 1 insertion(+) 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 28a64704d6..d7c606bd96 100644 --- a/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt +++ b/formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt @@ -103,6 +103,7 @@ 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 + // 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