11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Comparator;
34 import java.util.Formatter;
35 import java.util.Locale;
36 import java.util.Objects;
37 import java.util.StringJoiner;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 import java.util.regex.PatternSyntaxException;
41
42 /**
43 * The {@code String} class represents character strings. All
44 * string literals in Java programs, such as {@code "abc"}, are
45 * implemented as instances of this class.
46 * <p>
47 * Strings are constant; their values cannot be changed after they
48 * are created. String buffers support mutable strings.
49 * Because String objects are immutable they can be shared. For example:
50 * <blockquote><pre>
343 * default charset.
344 *
345 * @param ascii
346 * The bytes to be converted to characters
347 *
348 * @param hibyte
349 * The top 8 bits of each 16-bit Unicode code unit
350 *
351 * @see #String(byte[], int, int, java.lang.String)
352 * @see #String(byte[], int, int, java.nio.charset.Charset)
353 * @see #String(byte[], int, int)
354 * @see #String(byte[], java.lang.String)
355 * @see #String(byte[], java.nio.charset.Charset)
356 * @see #String(byte[])
357 */
358 @Deprecated
359 public String(byte ascii[], int hibyte) {
360 this(ascii, hibyte, 0, ascii.length);
361 }
362
363 /* Common private utility method used to bounds check the byte array
364 * and requested offset & length values used by the String(byte[],..)
365 * constructors.
366 */
367 private static void checkBounds(byte[] bytes, int offset, int length) {
368 if (length < 0)
369 throw new StringIndexOutOfBoundsException(length);
370 if (offset < 0)
371 throw new StringIndexOutOfBoundsException(offset);
372 if (offset > bytes.length - length)
373 throw new StringIndexOutOfBoundsException(offset + length);
374 }
375
376 /**
377 * Constructs a new {@code String} by decoding the specified subarray of
378 * bytes using the specified charset. The length of the new {@code String}
379 * is a function of the charset, and hence may not be equal to the length
380 * of the subarray.
381 *
382 * <p> The behavior of this constructor when the given bytes are not valid
383 * in the given charset is unspecified. The {@link
384 * java.nio.charset.CharsetDecoder} class should be used when more control
385 * over the decoding process is required.
386 *
387 * @param bytes
388 * The bytes to be decoded into characters
389 *
390 * @param offset
391 * The index of the first byte to decode
392 *
432 * The index of the first byte to decode
433 *
434 * @param length
435 * The number of bytes to decode
436 *
437 * @param charset
438 * The {@linkplain java.nio.charset.Charset charset} to be used to
439 * decode the {@code bytes}
440 *
441 * @throws IndexOutOfBoundsException
442 * If the {@code offset} and {@code length} arguments index
443 * characters outside the bounds of the {@code bytes} array
444 *
445 * @since 1.6
446 */
447 public String(byte bytes[], int offset, int length, Charset charset) {
448 if (charset == null)
449 throw new NullPointerException("charset");
450 checkBounds(bytes, offset, length);
451 this.value = StringCoding.decode(charset, bytes, offset, length);
452 }
453
454 /**
455 * Constructs a new {@code String} by decoding the specified array of bytes
456 * using the specified {@linkplain java.nio.charset.Charset charset}. The
457 * length of the new {@code String} is a function of the charset, and hence
458 * may not be equal to the length of the byte array.
459 *
460 * <p> The behavior of this constructor when the given bytes are not valid
461 * in the given charset is unspecified. The {@link
462 * java.nio.charset.CharsetDecoder} class should be used when more control
463 * over the decoding process is required.
464 *
465 * @param bytes
466 * The bytes to be decoded into characters
467 *
468 * @param charsetName
469 * The name of a supported {@linkplain java.nio.charset.Charset
470 * charset}
471 *
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.nio.ByteBuffer;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Comparator;
35 import java.util.Formatter;
36 import java.util.Locale;
37 import java.util.Objects;
38 import java.util.StringJoiner;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import java.util.regex.PatternSyntaxException;
42
43 /**
44 * The {@code String} class represents character strings. All
45 * string literals in Java programs, such as {@code "abc"}, are
46 * implemented as instances of this class.
47 * <p>
48 * Strings are constant; their values cannot be changed after they
49 * are created. String buffers support mutable strings.
50 * Because String objects are immutable they can be shared. For example:
51 * <blockquote><pre>
344 * default charset.
345 *
346 * @param ascii
347 * The bytes to be converted to characters
348 *
349 * @param hibyte
350 * The top 8 bits of each 16-bit Unicode code unit
351 *
352 * @see #String(byte[], int, int, java.lang.String)
353 * @see #String(byte[], int, int, java.nio.charset.Charset)
354 * @see #String(byte[], int, int)
355 * @see #String(byte[], java.lang.String)
356 * @see #String(byte[], java.nio.charset.Charset)
357 * @see #String(byte[])
358 */
359 @Deprecated
360 public String(byte ascii[], int hibyte) {
361 this(ascii, hibyte, 0, ascii.length);
362 }
363
364 private static void checkBounds(byte[] bytes, int offset, int length) {
365 checkBounds(bytes.length, offset, length);
366 }
367
368 private static void checkBounds(ByteBuffer bytes, int offset, int length) {
369 checkBounds(bytes.capacity(), offset, length);
370 }
371
372 /* Common private utility method used to bounds check the byte array
373 * and requested offset & length values used by the String(byte[],..)
374 * constructors.
375 */
376 private static void checkBounds(int lengthOfBytes, int offset, int length) {
377 if (length < 0)
378 throw new StringIndexOutOfBoundsException(length);
379 if (offset < 0)
380 throw new StringIndexOutOfBoundsException(offset);
381 if (offset > lengthOfBytes - length)
382 throw new StringIndexOutOfBoundsException(offset + length);
383 }
384
385 /**
386 * Constructs a new {@code String} by decoding the specified subarray of
387 * bytes using the specified charset. The length of the new {@code String}
388 * is a function of the charset, and hence may not be equal to the length
389 * of the subarray.
390 *
391 * <p> The behavior of this constructor when the given bytes are not valid
392 * in the given charset is unspecified. The {@link
393 * java.nio.charset.CharsetDecoder} class should be used when more control
394 * over the decoding process is required.
395 *
396 * @param bytes
397 * The bytes to be decoded into characters
398 *
399 * @param offset
400 * The index of the first byte to decode
401 *
441 * The index of the first byte to decode
442 *
443 * @param length
444 * The number of bytes to decode
445 *
446 * @param charset
447 * The {@linkplain java.nio.charset.Charset charset} to be used to
448 * decode the {@code bytes}
449 *
450 * @throws IndexOutOfBoundsException
451 * If the {@code offset} and {@code length} arguments index
452 * characters outside the bounds of the {@code bytes} array
453 *
454 * @since 1.6
455 */
456 public String(byte bytes[], int offset, int length, Charset charset) {
457 if (charset == null)
458 throw new NullPointerException("charset");
459 checkBounds(bytes, offset, length);
460 this.value = StringCoding.decode(charset, bytes, offset, length);
461 }
462
463 public String(ByteBuffer bytes, int offset, int length, Charset charset) {
464 if (charset == null)
465 throw new NullPointerException("charset");
466 checkBounds(bytes, offset, length);
467 this.value = StringCoding.decode(charset, bytes, offset, length);
468 }
469
470 /**
471 * Constructs a new {@code String} by decoding the specified array of bytes
472 * using the specified {@linkplain java.nio.charset.Charset charset}. The
473 * length of the new {@code String} is a function of the charset, and hence
474 * may not be equal to the length of the byte array.
475 *
476 * <p> The behavior of this constructor when the given bytes are not valid
477 * in the given charset is unspecified. The {@link
478 * java.nio.charset.CharsetDecoder} class should be used when more control
479 * over the decoding process is required.
480 *
481 * @param bytes
482 * The bytes to be decoded into characters
483 *
484 * @param charsetName
485 * The name of a supported {@linkplain java.nio.charset.Charset
486 * charset}
487 *
|