Commit 92ac1ed6 authored by Anuraag Agrawal's avatar Anuraag Agrawal

Remove unsafe no-copy String allocation since it's not useful in recent Java versions.

parent 7377d81c
...@@ -33,7 +33,6 @@ package com.google.protobuf; ...@@ -33,7 +33,6 @@ package com.google.protobuf;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.Buffer; import java.nio.Buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.util.logging.Level; import java.util.logging.Level;
...@@ -72,8 +71,6 @@ final class UnsafeUtil { ...@@ -72,8 +71,6 @@ final class UnsafeUtil {
private static final long BUFFER_ADDRESS_OFFSET = fieldOffset(bufferAddressField()); private static final long BUFFER_ADDRESS_OFFSET = fieldOffset(bufferAddressField());
private static final long STRING_VALUE_OFFSET = fieldOffset(stringValueField());
private UnsafeUtil() {} private UnsafeUtil() {}
static boolean hasUnsafeArrayOperations() { static boolean hasUnsafeArrayOperations() {
...@@ -149,10 +146,6 @@ final class UnsafeUtil { ...@@ -149,10 +146,6 @@ final class UnsafeUtil {
return MEMORY_ACCESSOR.getObject(target, offset); 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) { static byte getByte(byte[] target, long index) {
return MEMORY_ACCESSOR.getByte(target, BYTE_ARRAY_BASE_OFFSET + index); return MEMORY_ACCESSOR.getByte(target, BYTE_ARRAY_BASE_OFFSET + index);
} }
...@@ -262,26 +255,6 @@ final class UnsafeUtil { ...@@ -262,26 +255,6 @@ final class UnsafeUtil {
return MEMORY_ACCESSOR.getLong(buffer, BUFFER_ADDRESS_OFFSET); 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) { static Object getStaticObject(Field field) {
return MEMORY_ACCESSOR.getStaticObject(field); return MEMORY_ACCESSOR.getStaticObject(field);
} }
......
...@@ -1474,10 +1474,7 @@ final class Utf8 { ...@@ -1474,10 +1474,7 @@ final class Utf8 {
} }
} }
if (resultPos < resultArr.length) { return new String(resultArr, 0, resultPos);
resultArr = Arrays.copyOf(resultArr, resultPos);
}
return UnsafeUtil.moveToString(resultArr);
} }
@Override @Override
...@@ -1553,10 +1550,7 @@ final class Utf8 { ...@@ -1553,10 +1550,7 @@ final class Utf8 {
} }
} }
if (resultPos < resultArr.length) { return new String(resultArr, 0, resultPos);
resultArr = Arrays.copyOf(resultArr, resultPos);
}
return UnsafeUtil.moveToString(resultArr);
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment