Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/java.base/share/classes/java/lang/StringUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@

import static java.lang.String.UTF16;

/// UTF16 String operations.
///
/// UTF16 byte arrays have the identical layout as char arrays. They share the
/// same base offset and scale, and for each two-byte unit interpreted as a char,
/// it has the same endianness as a char, which is the platform endianness.
/// This is ensured in the static initializer of StringUTF16.
///
/// All indices and sizes for byte arrays carrying UTF16 data are in number of
/// chars instead of number of bytes.
final class StringUTF16 {

// Return a new byte array for a UTF16-coded string for len chars
Expand Down Expand Up @@ -1635,6 +1644,9 @@ public static int lastIndexOfLatin1(byte[] src, int srcCount,
private static final int HI_BYTE_SHIFT;
private static final int LO_BYTE_SHIFT;
static {
// Assumptions for StringUTF16 operations. Present in `LibraryCallKit::inline_string_char_access` too.
assert Unsafe.ARRAY_CHAR_BASE_OFFSET == Unsafe.ARRAY_BYTE_BASE_OFFSET : "sanity: byte[] and char[] bases agree";
assert Unsafe.ARRAY_CHAR_INDEX_SCALE == Unsafe.ARRAY_BYTE_INDEX_SCALE * 2 : "sanity: byte[] and char[] scales agree";
if (Unsafe.getUnsafe().isBigEndian()) {
HI_BYTE_SHIFT = 8;
LO_BYTE_SHIFT = 0;
Expand Down