From 0e9d79c3559f14a4ec3b6fc571110ea0c12b7118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Tue, 31 Aug 2021 00:51:06 +0200 Subject: [PATCH] [Java] Avoid casting ByteBuffer to Buffer (#6785) * Revert "avoiding even more NoSuchMethod exceptions (#6729)" This reverts commit 6fb2c90d9ef1469312840f80612504f27f03cf00. * Revert "avoiding more NoSuchMethod exceptions (#6671)" This reverts commit 752c7b576d395ffab646aa6878ccf4e17a0a7a2e. * Revert "avoiding NoSuchMethod exception (#6658)" This reverts commit 813d3632ecefaf6d4068d87abdef04d94663dca3. * Use Java 8 for Kotlin Linux builds to verify --- .github/workflows/build.yml | 2 +- .../flatbuffers/ByteBufferReadWriteBuf.java | 7 ++-- .../google/flatbuffers/ByteBufferUtil.java | 5 ++- .../google/flatbuffers/FlatBufferBuilder.java | 32 +++++++++---------- java/com/google/flatbuffers/FlexBuffers.java | 3 +- java/com/google/flatbuffers/Table.java | 5 ++- java/com/google/flatbuffers/Utf8Old.java | 13 ++++---- java/com/google/flatbuffers/Utf8Safe.java | 7 ++-- 8 files changed, 34 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 36ac4b483..e4e348366 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -143,7 +143,7 @@ jobs: - uses: actions/setup-java@v2 with: distribution: 'adopt-hotspot' - java-version: '11' + java-version: '8' - name: Build working-directory: kotlin run: ./gradlew jvmMainClasses jvmTest diff --git a/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java b/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java index 55acdd05a..aaf72fe81 100644 --- a/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java +++ b/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java @@ -2,7 +2,6 @@ package com.google.flatbuffers; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.nio.Buffer; public class ByteBufferReadWriteBuf implements ReadWriteBuf { @@ -15,7 +14,7 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf { @Override public void clear() { - ((Buffer) buffer).clear(); + buffer.clear(); } @Override @@ -118,9 +117,9 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf { public void set(int index, byte[] value, int start, int length) { requestCapacity(index + (length - start)); int curPos = buffer.position(); - ((Buffer) buffer).position(index); + buffer.position(index); buffer.put(value, start, length); - ((Buffer) buffer).position(curPos); + buffer.position(curPos); } @Override diff --git a/java/com/google/flatbuffers/ByteBufferUtil.java b/java/com/google/flatbuffers/ByteBufferUtil.java index 717d6e1b7..624dc4e2f 100644 --- a/java/com/google/flatbuffers/ByteBufferUtil.java +++ b/java/com/google/flatbuffers/ByteBufferUtil.java @@ -19,7 +19,6 @@ package com.google.flatbuffers; import static com.google.flatbuffers.Constants.*; import java.nio.ByteBuffer; -import java.nio.Buffer; /// @file /// @addtogroup flatbuffers_java_api @@ -49,9 +48,9 @@ public class ByteBufferUtil { * size prefix */ public static ByteBuffer removeSizePrefix(ByteBuffer bb) { - Buffer s = ((Buffer) bb).duplicate(); + ByteBuffer s = bb.duplicate(); s.position(s.position() + SIZE_PREFIX_LENGTH); - return (ByteBuffer) s; + return s; } } diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index b8c1aa203..a954d9fbb 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -92,7 +92,7 @@ public class FlatBufferBuilder { this.bb_factory = bb_factory; if (existing_bb != null) { bb = existing_bb; - ((Buffer) bb).clear(); + bb.clear(); bb.order(ByteOrder.LITTLE_ENDIAN); } else { bb = bb_factory.newByteBuffer(initial_size); @@ -154,7 +154,7 @@ public class FlatBufferBuilder { public FlatBufferBuilder init(ByteBuffer existing_bb, ByteBufferFactory bb_factory){ this.bb_factory = bb_factory; bb = existing_bb; - ((Buffer) bb).clear(); + bb.clear(); bb.order(ByteOrder.LITTLE_ENDIAN); minalign = 1; space = bb.capacity(); @@ -235,7 +235,7 @@ public class FlatBufferBuilder { */ public void clear(){ space = bb.capacity(); - ((Buffer) bb).clear(); + bb.clear(); minalign = 1; while(vtable_in_use > 0) vtable[--vtable_in_use] = 0; vtable_in_use = 0; @@ -273,10 +273,10 @@ public class FlatBufferBuilder { new_buf_size = (old_buf_size & 0xC0000000) != 0 ? MAX_BUFFER_SIZE : old_buf_size << 1; } - ((Buffer) bb).position(0); + bb.position(0); ByteBuffer nbb = bb_factory.newByteBuffer(new_buf_size); - new_buf_size = ((Buffer) nbb).clear().capacity(); // Ensure the returned buffer is treated as empty - ((Buffer) nbb).position(new_buf_size - old_buf_size); + new_buf_size = nbb.clear().capacity(); // Ensure the returned buffer is treated as empty + nbb.position(new_buf_size - old_buf_size); nbb.put(bb); return nbb; } @@ -527,7 +527,7 @@ public class FlatBufferBuilder { int length = elem_size * num_elems; startVector(elem_size, num_elems, alignment); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); // Slice and limit the copy vector to point to the 'array' ByteBuffer copy = bb.slice().order(ByteOrder.LITTLE_ENDIAN); @@ -602,7 +602,7 @@ public class FlatBufferBuilder { int length = utf8.encodedLength(s); addByte((byte)0); startVector(1, length, 1); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); utf8.encodeUtf8(s, bb); return endVector(); } @@ -617,7 +617,7 @@ public class FlatBufferBuilder { int length = s.remaining(); addByte((byte)0); startVector(1, length, 1); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); bb.put(s); return endVector(); } @@ -631,7 +631,7 @@ public class FlatBufferBuilder { public int createByteVector(byte[] arr) { int length = arr.length; startVector(1, length, 1); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); bb.put(arr); return endVector(); } @@ -646,7 +646,7 @@ public class FlatBufferBuilder { */ public int createByteVector(byte[] arr, int offset, int length) { startVector(1, length, 1); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); bb.put(arr, offset, length); return endVector(); } @@ -663,7 +663,7 @@ public class FlatBufferBuilder { public int createByteVector(ByteBuffer byteBuffer) { int length = byteBuffer.remaining(); startVector(1, length, 1); - ((Buffer) bb).position(space -= length); + bb.position(space -= length); bb.put(byteBuffer); return endVector(); } @@ -953,7 +953,7 @@ public class FlatBufferBuilder { if (size_prefix) { addInt(bb.capacity() - space); } - ((Buffer) bb).position(space); + bb.position(space); finished = true; } @@ -1067,7 +1067,7 @@ public class FlatBufferBuilder { public byte[] sizedByteArray(int start, int length){ finished(); byte[] array = new byte[length]; - ((Buffer) bb).position(start); + bb.position(start); bb.get(array); return array; } @@ -1089,10 +1089,10 @@ public class FlatBufferBuilder { */ public InputStream sizedInputStream() { finished(); - Buffer duplicate = ((Buffer) bb).duplicate(); + ByteBuffer duplicate = bb.duplicate(); duplicate.position(space); duplicate.limit(bb.capacity()); - return new ByteBufferBackedInputStream((ByteBuffer) duplicate); + return new ByteBufferBackedInputStream(duplicate); } /** diff --git a/java/com/google/flatbuffers/FlexBuffers.java b/java/com/google/flatbuffers/FlexBuffers.java index c88624678..b8f879b6e 100644 --- a/java/com/google/flatbuffers/FlexBuffers.java +++ b/java/com/google/flatbuffers/FlexBuffers.java @@ -23,7 +23,6 @@ import static com.google.flatbuffers.FlexBuffers.Unsigned.shortToUnsignedInt; import java.math.BigInteger; import java.nio.ByteBuffer; -import java.nio.Buffer; import java.nio.charset.StandardCharsets; /// @file @@ -689,7 +688,7 @@ public class FlexBuffers { */ public ByteBuffer data() { ByteBuffer dup = ByteBuffer.wrap(bb.data()); - ((Buffer) dup).position(end); + dup.position(end); dup.limit(end + size()); return dup.asReadOnlyBuffer().slice(); } diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java index 72fdb0a74..7f416396e 100644 --- a/java/com/google/flatbuffers/Table.java +++ b/java/com/google/flatbuffers/Table.java @@ -18,7 +18,6 @@ package com.google.flatbuffers; import static com.google.flatbuffers.Constants.*; import java.nio.ByteBuffer; -import java.nio.Buffer; import java.nio.ByteOrder; /// @cond FLATBUFFERS_INTERNAL @@ -151,7 +150,7 @@ public class Table { protected ByteBuffer __vector_as_bytebuffer(int vector_offset, int elem_size) { int o = __offset(vector_offset); if (o == 0) return null; - ByteBuffer bb = ((ByteBuffer) (((Buffer) this.bb).duplicate())).order(ByteOrder.LITTLE_ENDIAN); + ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN); int vectorstart = __vector(o); bb.position(vectorstart); bb.limit(vectorstart + __vector_len(o) * elem_size); @@ -175,7 +174,7 @@ public class Table { int vectorstart = __vector(o); bb.rewind(); bb.limit(vectorstart + __vector_len(o) * elem_size); - ((Buffer) bb).position(vectorstart); + bb.position(vectorstart); return bb; } diff --git a/java/com/google/flatbuffers/Utf8Old.java b/java/com/google/flatbuffers/Utf8Old.java index e6d02f8ee..3dac714bb 100644 --- a/java/com/google/flatbuffers/Utf8Old.java +++ b/java/com/google/flatbuffers/Utf8Old.java @@ -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); diff --git a/java/com/google/flatbuffers/Utf8Safe.java b/java/com/google/flatbuffers/Utf8Safe.java index a5900044e..523e3f1b4 100644 --- a/java/com/google/flatbuffers/Utf8Safe.java +++ b/java/com/google/flatbuffers/Utf8Safe.java @@ -31,7 +31,6 @@ package com.google.flatbuffers; import java.nio.ByteBuffer; -import java.nio.Buffer; import static java.lang.Character.MAX_SURROGATE; import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT; import static java.lang.Character.MIN_SURROGATE; @@ -311,7 +310,7 @@ final public class Utf8Safe extends Utf8 { } if (inIx == inLength) { // Successfully encoded the entire string. - ((Buffer) out).position(outIx + inIx); + out.position(outIx + inIx); return; } @@ -354,7 +353,7 @@ final public class Utf8Safe extends Utf8 { } // Successfully encoded the entire string. - ((Buffer) out).position(outIx); + out.position(outIx); } catch (IndexOutOfBoundsException e) { // TODO(nathanmittler): Consider making the API throw IndexOutOfBoundsException instead. @@ -435,7 +434,7 @@ final public class Utf8Safe extends Utf8 { int start = out.arrayOffset(); int end = encodeUtf8Array(in, out.array(), start + out.position(), out.remaining()); - ((Buffer) out).position(end - start); + out.position(end - start); } else { encodeUtf8Buffer(in, out); }