< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
@@ -572,21 +572,21 @@
                      }
                      this.value = dst;
                      this.coder = LATIN1;
                      return;
                  }
-                 byte[] buf = new byte[length << 1];
+                 byte[] buf = StringUTF16.newBytesFor(length);
                  StringLatin1.inflate(dst, 0, buf, 0, dp);
                  dst = buf;
                  dp = decodeUTF8_UTF16(bytes, offset, sl, dst, dp, true);
                  if (dp != length) {
                      dst = Arrays.copyOf(dst, dp << 1);
                  }
                  this.value = dst;
                  this.coder = UTF16;
              } else { // !COMPACT_STRINGS
-                 byte[] dst = new byte[length << 1];
+                 byte[] dst = StringUTF16.newBytesFor(length);
                  int dp = decodeUTF8_UTF16(bytes, offset, offset + length, dst, 0, true);
                  if (dp != length) {
                      dst = Arrays.copyOf(dst, dp << 1);
                  }
                  this.value = dst;

@@ -603,11 +603,11 @@
          } else if (charset == US_ASCII.INSTANCE) {
              if (COMPACT_STRINGS && !StringCoding.hasNegatives(bytes, offset, length)) {
                  this.value = Arrays.copyOfRange(bytes, offset, offset + length);
                  this.coder = LATIN1;
              } else {
-                 byte[] dst = new byte[length << 1];
+                 byte[] dst = StringUTF16.newBytesFor(length);
                  int dp = 0;
                  while (dp < length) {
                      int b = bytes[offset++];
                      StringUTF16.putChar(dst, dp++, (b >= 0) ? (char) b : REPL);
                  }

@@ -748,19 +748,19 @@
                      dst = Arrays.copyOf(dst, dp);
                  }
                  return new String(dst, LATIN1);
              }
              if (dp == 0) {
-                 dst = new byte[length << 1];
+                 dst = StringUTF16.newBytesFor(length);
              } else {
-                 byte[] buf = new byte[length << 1];
+                 byte[] buf = StringUTF16.newBytesFor(length);
                  StringLatin1.inflate(dst, 0, buf, 0, dp);
                  dst = buf;
              }
              dp = decodeUTF8_UTF16(bytes, offset, sl, dst, dp, false);
          } else { // !COMPACT_STRINGS
-             dst = new byte[length << 1];
+             dst = StringUTF16.newBytesFor(length);
              dp = decodeUTF8_UTF16(bytes, offset, offset + length, dst, 0, false);
          }
          if (dp != length) {
              dst = Arrays.copyOf(dst, dp << 1);
          }

@@ -1302,11 +1302,11 @@
          if (!StringCoding.hasNegatives(val, 0, val.length)) {
              return val.clone();
          }
  
          int dp = 0;
-         byte[] dst = new byte[val.length << 1];
+         byte[] dst = StringUTF16.newBytesFor(val.length);
          for (byte c : val) {
              if (c < 0) {
                  dst[dp++] = (byte) (0xc0 | ((c & 0xff) >> 6));
                  dst[dp++] = (byte) (0x80 | (c & 0x3f));
              } else {
< prev index next >