From 92ac1ed6a5baf58651e70759021910baa614332a Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 11 May 2018 14:27:31 +0900 Subject: [PATCH] Remove unsafe no-copy String allocation since it's not useful in recent Java versions. --- .../java/com/google/protobuf/UnsafeUtil.java | 27 ------------------- .../main/java/com/google/protobuf/Utf8.java | 10 ++----- 2 files changed, 2 insertions(+), 35 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java index c9e2904b4c..d84ef3c5d9 100644 --- a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java +++ b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java @@ -33,7 +33,6 @@ package com.google.protobuf; import java.lang.reflect.Field; import java.nio.Buffer; import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.logging.Level; @@ -72,8 +71,6 @@ final class UnsafeUtil { private static final long BUFFER_ADDRESS_OFFSET = fieldOffset(bufferAddressField()); - private static final long STRING_VALUE_OFFSET = fieldOffset(stringValueField()); - private UnsafeUtil() {} static boolean hasUnsafeArrayOperations() { @@ -149,10 +146,6 @@ final class UnsafeUtil { return MEMORY_ACCESSOR.getObject(target, offset); } - static void putObject(Object target, long offset, Object value) { - MEMORY_ACCESSOR.putObject(target, offset, value); - } - static byte getByte(byte[] target, long index) { return MEMORY_ACCESSOR.getByte(target, BYTE_ARRAY_BASE_OFFSET + index); } @@ -262,26 +255,6 @@ final class UnsafeUtil { return MEMORY_ACCESSOR.getLong(buffer, BUFFER_ADDRESS_OFFSET); } - /** - * Returns a new {@link String} backed by the given {@code chars}. The char array should not - * be mutated any more after calling this function. - */ - static String moveToString(char[] chars) { - if (STRING_VALUE_OFFSET == -1) { - // In the off-chance that this JDK does not implement String as we'd expect, just do a copy. - return new String(chars); - } - final String str; - try { - str = (String) UNSAFE.allocateInstance(String.class); - } catch (InstantiationException e) { - // This should never happen, but return a copy as a fallback just in case. - return new String(chars); - } - putObject(str, STRING_VALUE_OFFSET, chars); - return str; - } - static Object getStaticObject(Field field) { return MEMORY_ACCESSOR.getStaticObject(field); } diff --git a/java/core/src/main/java/com/google/protobuf/Utf8.java b/java/core/src/main/java/com/google/protobuf/Utf8.java index 6968abb3c1..de75fe6b19 100644 --- a/java/core/src/main/java/com/google/protobuf/Utf8.java +++ b/java/core/src/main/java/com/google/protobuf/Utf8.java @@ -1474,10 +1474,7 @@ final class Utf8 { } } - if (resultPos < resultArr.length) { - resultArr = Arrays.copyOf(resultArr, resultPos); - } - return UnsafeUtil.moveToString(resultArr); + return new String(resultArr, 0, resultPos); } @Override @@ -1553,10 +1550,7 @@ final class Utf8 { } } - if (resultPos < resultArr.length) { - resultArr = Arrays.copyOf(resultArr, resultPos); - } - return UnsafeUtil.moveToString(resultArr); + return new String(resultArr, 0, resultPos); } @Override