From d64a2d9941c36a7bc2a7959ea10ab8363192ac14 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Wed, 29 Jun 2016 15:23:27 -0700 Subject: Integrated internal changes from Google This includes all internal changes from around May 20 to now. --- .../com/google/protobuf/CodedOutputStream.java | 113 +++++---------------- 1 file changed, 23 insertions(+), 90 deletions(-) (limited to 'java/core/src/main/java/com/google/protobuf/CodedOutputStream.java') diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index ad174d0f..576a350f 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -36,12 +36,9 @@ import com.google.protobuf.Utf8.UnpairedSurrogateException; import java.io.IOException; import java.io.OutputStream; -import java.lang.reflect.Field; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.security.AccessController; -import java.security.PrivilegedExceptionAction; import java.util.logging.Level; import java.util.logging.Logger; @@ -59,9 +56,8 @@ import java.util.logging.Logger; */ public abstract class CodedOutputStream extends ByteOutput { private static final Logger logger = Logger.getLogger(CodedOutputStream.class.getName()); - private static final sun.misc.Unsafe UNSAFE = getUnsafe(); - private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = supportsUnsafeArrayOperations(); - private static final long ARRAY_BASE_OFFSET = byteArrayBaseOffset(); + private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = UnsafeUtil.hasUnsafeArrayOperations(); + private static final long ARRAY_BASE_OFFSET = UnsafeUtil.getArrayBaseOffset(); private static final int FIXED_32_SIZE = 4; private static final int FIXED_64_SIZE = 8; @@ -869,7 +865,7 @@ public abstract class CodedOutputStream extends ByteOutput { return computeLengthDelimitedFieldSize(value.getSerializedSize()); } - private static int computeLengthDelimitedFieldSize(int fieldLength) { + static int computeLengthDelimitedFieldSize(int fieldLength) { return computeUInt32SizeNoTag(fieldLength) + fieldLength; } @@ -948,6 +944,10 @@ public abstract class CodedOutputStream extends ByteOutput { OutOfSpaceException(Throwable cause) { super(MESSAGE, cause); } + + OutOfSpaceException(String explanationMessage, Throwable cause) { + super(MESSAGE + ": " + explanationMessage, cause); + } } /** @@ -1250,8 +1250,8 @@ public abstract class CodedOutputStream extends ByteOutput { try { buffer[position++] = value; } catch (IndexOutOfBoundsException e) { - throw new OutOfSpaceException(new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1))); + throw new OutOfSpaceException( + String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); } } @@ -1271,11 +1271,11 @@ public abstract class CodedOutputStream extends ByteOutput { long pos = ARRAY_BASE_OFFSET + position; while (true) { if ((value & ~0x7F) == 0) { - UNSAFE.putByte(buffer, pos++, (byte) value); + UnsafeUtil.putByte(buffer, pos++, (byte) value); position++; return; } else { - UNSAFE.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80)); position++; value >>>= 7; } @@ -1293,8 +1293,7 @@ public abstract class CodedOutputStream extends ByteOutput { } } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); } } } @@ -1308,8 +1307,7 @@ public abstract class CodedOutputStream extends ByteOutput { buffer[position++] = (byte) ((value >> 24) & 0xFF); } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); } } @@ -1319,11 +1317,11 @@ public abstract class CodedOutputStream extends ByteOutput { long pos = ARRAY_BASE_OFFSET + position; while (true) { if ((value & ~0x7FL) == 0) { - UNSAFE.putByte(buffer, pos++, (byte) value); + UnsafeUtil.putByte(buffer, pos++, (byte) value); position++; return; } else { - UNSAFE.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80)); position++; value >>>= 7; } @@ -1341,8 +1339,7 @@ public abstract class CodedOutputStream extends ByteOutput { } } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); } } } @@ -1360,8 +1357,7 @@ public abstract class CodedOutputStream extends ByteOutput { buffer[position++] = (byte) ((int) (value >> 56) & 0xFF); } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); } } @@ -1372,8 +1368,7 @@ public abstract class CodedOutputStream extends ByteOutput { position += length; } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, length))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e); } } @@ -1390,8 +1385,7 @@ public abstract class CodedOutputStream extends ByteOutput { position += length; } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - new IndexOutOfBoundsException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, length))); + String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e); } } @@ -1855,10 +1849,10 @@ public abstract class CodedOutputStream extends ByteOutput { long pos = originalPos; while (true) { if ((value & ~0x7F) == 0) { - UNSAFE.putByte(buffer, pos++, (byte) value); + UnsafeUtil.putByte(buffer, pos++, (byte) value); break; } else { - UNSAFE.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80)); value >>>= 7; } } @@ -1890,10 +1884,10 @@ public abstract class CodedOutputStream extends ByteOutput { long pos = originalPos; while (true) { if ((value & ~0x7FL) == 0) { - UNSAFE.putByte(buffer, pos++, (byte) value); + UnsafeUtil.putByte(buffer, pos++, (byte) value); break; } else { - UNSAFE.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80)); value >>>= 7; } } @@ -2600,65 +2594,4 @@ public abstract class CodedOutputStream extends ByteOutput { position = 0; } } - - /** - * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this - * platform. - */ - private static sun.misc.Unsafe getUnsafe() { - sun.misc.Unsafe unsafe = null; - try { - unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public sun.misc.Unsafe run() throws Exception { - Class k = sun.misc.Unsafe.class; - - for (Field f : k.getDeclaredFields()) { - f.setAccessible(true); - Object x = f.get(null); - if (k.isInstance(x)) { - return k.cast(x); - } - } - // The sun.misc.Unsafe field does not exist. - return null; - } - }); - } catch (Throwable e) { - // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError - // for Unsafe. - } - - logger.log(Level.FINEST, "sun.misc.Unsafe: {}", - unsafe != null ? "available" : "unavailable"); - return unsafe; - } - - /** - * Indicates whether or not unsafe array operations are supported on this platform. - */ - // TODO(nathanmittler): Add support for Android's MemoryBlock. - private static boolean supportsUnsafeArrayOperations() { - boolean supported = false; - if (UNSAFE != null) { - try { - UNSAFE.getClass().getMethod("arrayBaseOffset", Class.class); - UNSAFE.getClass().getMethod("putByte", Object.class, long.class, byte.class); - supported = true; - } catch (Throwable e) { - // Do nothing. - } - } - logger.log(Level.FINEST, "Unsafe array operations: {}", - supported ? "available" : "unavailable"); - return supported; - } - - /** - * Get the base offset for byte arrays, or {@code -1} if {@code sun.misc.Unsafe} is not - * available. - */ - private static int byteArrayBaseOffset() { - return HAS_UNSAFE_ARRAY_OPERATIONS ? UNSAFE.arrayBaseOffset(byte[].class) : -1; - } } -- cgit v1.2.3