@@ -23,6 +23,7 @@ import com.amazon.ion.impl.bin.utf8.Utf8StringDecoderPool
2323import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
2424import java.lang.IllegalStateException
2525import java.math.BigInteger
26+ import java.nio.Buffer
2627import java.nio.ByteBuffer
2728import java.util.Arrays
2829import kotlin.math.min
@@ -43,6 +44,7 @@ internal class ByteArrayBytecodeGenerator10(
4344) : BytecodeGenerator {
4445
4546 private val decoder = Utf8StringDecoderPool .getInstance().getOrCreate()
47+ private val scratchBuffer = ByteBuffer .wrap(source)
4648 private var scratchArray = ByteArray (32 )
4749 private val symbolTableHelper = SymbolTableHelper
4850
@@ -79,9 +81,13 @@ internal class ByteArrayBytecodeGenerator10(
7981 override fun readTimestampReference (position : Int , length : Int ) = readTimestampReference(source, position, length)
8082
8183 override fun readTextReference (position : Int , length : Int ): String {
82- // TODO(perf): See if there's a way to do this without allocating new ByteBuffers, that is compatible with JDK 8.
83- val buffer = ByteBuffer .wrap(source, position, length)
84- return decoder.decode(buffer, length)
84+ val b = scratchBuffer
85+ // We have to cast to `Buffer` here because JDK 17 added an override that returns `ByteBuffer`.
86+ // The compiler seems to prefer that version, rather than the base method (which returns `Buffer`), and so
87+ // running the tests with JDK 8 fails without this cast.
88+ (b as Buffer ).limit(position + length)
89+ (b as Buffer ).position(position)
90+ return decoder.decode(b, length)
8591 }
8692
8793 override fun readBytesReference (position : Int , length : Int ): ByteSlice = ByteSlice (source, position, position + length)
0 commit comments