// Copyright 2018 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.unsafe; import com.google.common.base.Preconditions; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.Arrays; import javax.annotation.Nullable; import sun.misc.Unsafe; /** * Provides direct access to the string implementation used by JDK9. * *
Under JDK9, a string is two fields: byte coder
, and byte[] value
.
* The coder
field has value 0 if the encoding is LATIN-1, and 2 if the encoding is
* UTF-16 (the classic JDK8 encoding).
*
*
The Use of this is unsafe. The representation may change from one JDK version to the next.
* Ensure you do not mutate this byte array in any way.
*/
public byte[] getByteArray(String obj) {
return (byte[]) unsafe.getObject(obj, valueOffset);
}
/** Constructs a new string from a byte array and coder. */
public String newInstance(byte[] bytes, byte coder) throws ReflectiveOperationException {
return constructor.newInstance(bytes, coder);
}
}
value
field contains the actual bytes.
*/
public class StringUnsafe {
private static final boolean CAN_USE = getVersion() > 1.8;
// Fields corresponding to the coder
public static final byte LATIN1 = 0;
public static final byte UTF16 = 1;
private static final StringUnsafe INSTANCE = initInstance();
private final Unsafe unsafe;
private final Constructor