[Java] Avoid casting ByteBuffer to Buffer (#6785)

* Revert "avoiding even more NoSuchMethod exceptions (#6729)"

This reverts commit 6fb2c90d9e.

* Revert "avoiding more NoSuchMethod exceptions (#6671)"

This reverts commit 752c7b576d.

* Revert "avoiding NoSuchMethod exception (#6658)"

This reverts commit 813d3632ec.

* Use Java 8 for Kotlin Linux builds to verify
This commit is contained in:
Björn Harrtell
2021-08-31 00:51:06 +02:00
committed by GitHub
parent b20c4d3aad
commit 0e9d79c355
8 changed files with 34 additions and 40 deletions

View File

@@ -17,7 +17,6 @@
package com.google.flatbuffers;
import java.nio.ByteBuffer;
import java.nio.Buffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
@@ -56,7 +55,7 @@ public class Utf8Old extends Utf8 {
if (cache.lastOutput == null || cache.lastOutput.capacity() < estimated) {
cache.lastOutput = ByteBuffer.allocate(Math.max(128, estimated));
}
((Buffer) cache.lastOutput).clear();
cache.lastOutput.clear();
cache.lastInput = in;
CharBuffer wrap = (in instanceof CharBuffer) ?
(CharBuffer) in : CharBuffer.wrap(in);
@@ -68,7 +67,7 @@ public class Utf8Old extends Utf8 {
throw new IllegalArgumentException("bad character encoding", e);
}
}
((Buffer) cache.lastOutput).flip();
cache.lastOutput.flip();
return cache.lastOutput.remaining();
}
@@ -87,11 +86,11 @@ public class Utf8Old extends Utf8 {
public String decodeUtf8(ByteBuffer buffer, int offset, int length) {
CharsetDecoder decoder = CACHE.get().decoder;
decoder.reset();
Buffer b = ((Buffer) buffer).duplicate();
b.position(offset);
b.limit(offset + length);
buffer = buffer.duplicate();
buffer.position(offset);
buffer.limit(offset + length);
try {
CharBuffer result = decoder.decode((ByteBuffer) b);
CharBuffer result = decoder.decode(buffer);
return result.toString();
} catch (CharacterCodingException e) {
throw new IllegalArgumentException("Bad encoding", e);