Skip to content

Commit

Permalink
Add casts to make protobuf compatible with Java 1.8 runtime.
Browse files Browse the repository at this point in the history
Fix for: #11393

PiperOrigin-RevId: 511807920
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Feb 23, 2023
1 parent 5419141 commit d40aadf
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 46 deletions.
1 change: 1 addition & 0 deletions java/core/BUILD.bazel
Expand Up @@ -49,6 +49,7 @@ LITE_SRCS = [
"src/main/java/com/google/protobuf/Internal.java",
"src/main/java/com/google/protobuf/InvalidProtocolBufferException.java",
"src/main/java/com/google/protobuf/IterableByteBufferInputStream.java",
"src/main/java/com/google/protobuf/Java8Compatibility.java",
"src/main/java/com/google/protobuf/JavaType.java",
"src/main/java/com/google/protobuf/LazyField.java",
"src/main/java/com/google/protobuf/LazyFieldLite.java",
Expand Down
Expand Up @@ -189,7 +189,7 @@ public int position() {

@Override
public AllocatedBuffer position(int position) {
buffer.position(position);
Java8Compatibility.position(buffer, position);
return this;
}

Expand Down
28 changes: 14 additions & 14 deletions java/core/src/main/java/com/google/protobuf/BinaryWriter.java
Expand Up @@ -2019,8 +2019,8 @@ private void nextBuffer(AllocatedBuffer allocatedBuffer) {
buffers.addFirst(allocatedBuffer);

buffer = nioBuffer;
buffer.limit(buffer.capacity());
buffer.position(0);
Java8Compatibility.limit(buffer, buffer.capacity());
Java8Compatibility.position(buffer, 0);
// Set byte order to little endian for fast writing of fixed 32/64.
buffer.order(ByteOrder.LITTLE_ENDIAN);

Expand All @@ -2046,7 +2046,7 @@ void finishCurrentBuffer() {
if (buffer != null) {
totalDoneBytes += bytesWrittenToCurrentBuffer();
// Update the indices on the netty buffer.
buffer.position(pos + 1);
Java8Compatibility.position(buffer, pos + 1);
buffer = null;
pos = 0;
limitMinusOne = 0;
Expand Down Expand Up @@ -2475,7 +2475,7 @@ public void write(byte[] value, int offset, int length) {
}

pos -= length;
buffer.position(pos + 1);
Java8Compatibility.position(buffer, pos + 1);
buffer.put(value, offset, length);
}

Expand All @@ -2494,7 +2494,7 @@ public void writeLazy(byte[] value, int offset, int length) {
}

pos -= length;
buffer.position(pos + 1);
Java8Compatibility.position(buffer, pos + 1);
buffer.put(value, offset, length);
}

Expand All @@ -2506,7 +2506,7 @@ public void write(ByteBuffer value) {
}

pos -= length;
buffer.position(pos + 1);
Java8Compatibility.position(buffer, pos + 1);
buffer.put(value);
}

Expand All @@ -2526,7 +2526,7 @@ public void writeLazy(ByteBuffer value) {
}

pos -= length;
buffer.position(pos + 1);
Java8Compatibility.position(buffer, pos + 1);
buffer.put(value);
}

Expand Down Expand Up @@ -2576,8 +2576,8 @@ private void nextBuffer(AllocatedBuffer allocatedBuffer) {
buffers.addFirst(allocatedBuffer);

buffer = nioBuffer;
buffer.limit(buffer.capacity());
buffer.position(0);
Java8Compatibility.limit(buffer, buffer.capacity());
Java8Compatibility.position(buffer, 0);

bufferOffset = UnsafeUtil.addressOffset(buffer);
limitMinusOne = bufferOffset + (buffer.limit() - 1);
Expand All @@ -2602,7 +2602,7 @@ void finishCurrentBuffer() {
if (buffer != null) {
totalDoneBytes += bytesWrittenToCurrentBuffer();
// Update the indices on the netty buffer.
buffer.position(bufferPos() + 1);
Java8Compatibility.position(buffer, bufferPos() + 1);
buffer = null;
pos = 0;
limitMinusOne = 0;
Expand Down Expand Up @@ -3016,7 +3016,7 @@ public void write(byte[] value, int offset, int length) {
}

pos -= length;
buffer.position(bufferPos() + 1);
Java8Compatibility.position(buffer, bufferPos() + 1);
buffer.put(value, offset, length);
}

Expand All @@ -3035,7 +3035,7 @@ public void writeLazy(byte[] value, int offset, int length) {
}

pos -= length;
buffer.position(bufferPos() + 1);
Java8Compatibility.position(buffer, bufferPos() + 1);
buffer.put(value, offset, length);
}

Expand All @@ -3047,7 +3047,7 @@ public void write(ByteBuffer value) {
}

pos -= length;
buffer.position(bufferPos() + 1);
Java8Compatibility.position(buffer, bufferPos() + 1);
buffer.put(value);
}

Expand All @@ -3067,7 +3067,7 @@ public void writeLazy(ByteBuffer value) {
}

pos -= length;
buffer.position(bufferPos() + 1);
Java8Compatibility.position(buffer, bufferPos() + 1);
buffer.put(value);
}

Expand Down
Expand Up @@ -107,7 +107,7 @@ static void write(ByteBuffer buffer, OutputStream output) throws IOException {
}
} finally {
// Restore the initial position.
buffer.position(initialPos);
Java8Compatibility.position(buffer, initialPos);
}
}

Expand Down
28 changes: 14 additions & 14 deletions java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
Expand Up @@ -1271,7 +1271,7 @@ public final void writeRawBytes(final ByteBuffer value) throws IOException {
write(value.array(), value.arrayOffset(), value.capacity());
} else {
ByteBuffer duplicated = value.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
write(duplicated);
}
}
Expand Down Expand Up @@ -1522,7 +1522,7 @@ private static final class HeapNioEncoder extends ArrayEncoder {
@Override
public void flush() {
// Update the position on the buffer.
byteBuffer.position(initialPosition + getTotalBytesWritten());
Java8Compatibility.position(byteBuffer, initialPosition + getTotalBytesWritten());
}
}

Expand Down Expand Up @@ -1684,7 +1684,7 @@ public void writeRawBytes(final ByteBuffer value) throws IOException {
write(value.array(), value.arrayOffset(), value.capacity());
} else {
ByteBuffer duplicated = value.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
write(duplicated);
}
}
Expand Down Expand Up @@ -1794,26 +1794,26 @@ public void writeStringNoTag(String value) throws IOException {
// Save the current position and increment past the length field. We'll come back
// and write the length field after the encoding is complete.
final int startOfBytes = buffer.position() + minLengthVarIntSize;
buffer.position(startOfBytes);
Java8Compatibility.position(buffer, startOfBytes);

// Encode the string.
encode(value);

// Now go back to the beginning and write the length.
int endOfBytes = buffer.position();
buffer.position(startPos);
Java8Compatibility.position(buffer, startPos);
writeUInt32NoTag(endOfBytes - startOfBytes);

// Reposition the buffer past the written data.
buffer.position(endOfBytes);
Java8Compatibility.position(buffer, endOfBytes);
} else {
final int length = Utf8.encodedLength(value);
writeUInt32NoTag(length);
encode(value);
}
} catch (UnpairedSurrogateException e) {
// Roll back the change and convert to an IOException.
buffer.position(startPos);
Java8Compatibility.position(buffer, startPos);

// TODO(nathanmittler): We should throw an IOException here instead.
inefficientWriteStringNoTag(value, e);
Expand All @@ -1826,7 +1826,7 @@ public void writeStringNoTag(String value) throws IOException {
@Override
public void flush() {
// Update the position of the original buffer.
originalBuffer.position(buffer.position());
Java8Compatibility.position(originalBuffer, buffer.position());
}

@Override
Expand Down Expand Up @@ -2014,7 +2014,7 @@ public void writeRawBytes(ByteBuffer value) throws IOException {
write(value.array(), value.arrayOffset(), value.capacity());
} else {
ByteBuffer duplicated = value.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
write(duplicated);
}
}
Expand Down Expand Up @@ -2150,7 +2150,7 @@ public void writeStringNoTag(String value) throws IOException {
// Save the current position and increment past the length field. We'll come back
// and write the length field after the encoding is complete.
int stringStart = bufferPos(position) + minLengthVarIntSize;
buffer.position(stringStart);
Java8Compatibility.position(buffer, stringStart);

// Encode the string.
Utf8.encodeUtf8(value, buffer);
Expand Down Expand Up @@ -2187,7 +2187,7 @@ public void writeStringNoTag(String value) throws IOException {
@Override
public void flush() {
// Update the position of the original buffer.
originalBuffer.position(bufferPos(position));
Java8Compatibility.position(originalBuffer, bufferPos(position));
}

@Override
Expand All @@ -2201,7 +2201,7 @@ public int getTotalBytesWritten() {
}

private void repositionBuffer(long pos) {
buffer.position(bufferPos(pos));
Java8Compatibility.position(buffer, bufferPos(pos));
}

private int bufferPos(long pos) {
Expand Down Expand Up @@ -2478,7 +2478,7 @@ public void writeRawBytes(final ByteBuffer value) throws IOException {
write(value.array(), value.arrayOffset(), value.capacity());
} else {
ByteBuffer duplicated = value.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
write(duplicated);
}
}
Expand Down Expand Up @@ -2792,7 +2792,7 @@ public void writeRawBytes(final ByteBuffer value) throws IOException {
write(value.array(), value.arrayOffset(), value.capacity());
} else {
ByteBuffer duplicated = value.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
write(duplicated);
}
}
Expand Down
8 changes: 6 additions & 2 deletions java/core/src/main/java/com/google/protobuf/Internal.java
Expand Up @@ -313,7 +313,11 @@ public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) {
}
// ByteBuffer.equals() will only compare the remaining bytes, but we want to
// compare all the content.
return a.duplicate().clear().equals(b.duplicate().clear());
ByteBuffer aDuplicate = a.duplicate();
Java8Compatibility.clear(aDuplicate);
ByteBuffer bDuplicate = b.duplicate();
Java8Compatibility.clear(bDuplicate);
return aDuplicate.equals(bDuplicate);
}

/** Helper method for implementing {@link Message#equals(Object)} for bytes field. */
Expand Down Expand Up @@ -353,7 +357,7 @@ public static int hashCodeByteBuffer(ByteBuffer bytes) {
bytes.capacity() > DEFAULT_BUFFER_SIZE ? DEFAULT_BUFFER_SIZE : bytes.capacity();
final byte[] buffer = new byte[bufferSize];
final ByteBuffer duplicated = bytes.duplicate();
duplicated.clear();
Java8Compatibility.clear(duplicated);
int h = bytes.capacity();
while (duplicated.remaining() > 0) {
final int length =
Expand Down
Expand Up @@ -140,9 +140,9 @@ public int read(byte[] output, int offset, int length) throws IOException {
updateCurrentByteBufferPos(length);
} else {
int prevPos = currentByteBuffer.position();
currentByteBuffer.position(currentByteBufferPos);
Java8Compatibility.position(currentByteBuffer, currentByteBufferPos);
currentByteBuffer.get(output, offset, length);
currentByteBuffer.position(prevPos);
Java8Compatibility.position(currentByteBuffer, prevPos);
updateCurrentByteBufferPos(length);
}
return length;
Expand Down
@@ -0,0 +1,67 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package com.google.protobuf;

import java.nio.Buffer;

/**
* Wrappers around {@link Buffer} methods that are covariantly overridden in Java 9+. See
* https://github.com/protocolbuffers/protobuf/issues/11393
*
* <p>TODO(b/270454719) remove when Java 8 support is no longer needed.
*/
final class Java8Compatibility {
static void clear(Buffer b) {
b.clear();
}

static void flip(Buffer b) {
b.flip();
}

static void limit(Buffer b, int limit) {
b.limit(limit);
}

static void mark(Buffer b) {
b.mark();
}

static void position(Buffer b, int position) {
b.position(position);
}

static void reset(Buffer b) {
b.reset();
}

private Java8Compatibility() {}
}

0 comments on commit d40aadf

Please sign in to comment.