445
446 /**
447 * Constructs a new {@code String} by decoding the specified subarray of
448 * bytes using the specified charset. The length of the new {@code String}
449 * is a function of the charset, and hence may not be equal to the length
450 * of the subarray.
451 *
452 * <p> The behavior of this constructor when the given bytes are not valid
453 * in the given charset is unspecified. The {@link
454 * java.nio.charset.CharsetDecoder} class should be used when more control
455 * over the decoding process is required.
456 *
457 * @param bytes
458 * The bytes to be decoded into characters
459 *
460 * @param offset
461 * The index of the first byte to decode
462 *
463 * @param length
464 * The number of bytes to decode
465
466 * @param charsetName
467 * The name of a supported {@linkplain java.nio.charset.Charset
468 * charset}
469 *
470 * @throws UnsupportedEncodingException
471 * If the named charset is not supported
472 *
473 * @throws IndexOutOfBoundsException
474 * If {@code offset} is negative, {@code length} is negative, or
475 * {@code offset} is greater than {@code bytes.length - length}
476 *
477 * @since 1.1
478 */
479 public String(byte bytes[], int offset, int length, String charsetName)
480 throws UnsupportedEncodingException {
481 if (charsetName == null)
482 throw new NullPointerException("charsetName");
483 checkBoundsOffCount(offset, length, bytes.length);
484 StringCoding.Result ret =
485 StringCoding.decode(charsetName, bytes, offset, length);
2283 *
2284 * @param regex
2285 * the delimiting regular expression
2286 *
2287 * @param limit
2288 * the result threshold, as described above
2289 *
2290 * @return the array of strings computed by splitting this string
2291 * around matches of the given regular expression
2292 *
2293 * @throws PatternSyntaxException
2294 * if the regular expression's syntax is invalid
2295 *
2296 * @see java.util.regex.Pattern
2297 *
2298 * @since 1.4
2299 * @spec JSR-51
2300 */
2301 public String[] split(String regex, int limit) {
2302 /* fastpath if the regex is a
2303 (1)one-char String and this character is not one of the
2304 RegEx's meta characters ".$|()[{^?*+\\", or
2305 (2)two-char String and the first char is the backslash and
2306 the second is not the ascii digit or ascii letter.
2307 */
2308 char ch = 0;
2309 if (((regex.length() == 1 &&
2310 ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
2311 (regex.length() == 2 &&
2312 regex.charAt(0) == '\\' &&
2313 (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
2314 ((ch-'a')|('z'-ch)) < 0 &&
2315 ((ch-'A')|('Z'-ch)) < 0)) &&
2316 (ch < Character.MIN_HIGH_SURROGATE ||
2317 ch > Character.MAX_LOW_SURROGATE))
2318 {
2319 int off = 0;
2320 int next = 0;
2321 boolean limited = limit > 0;
2322 ArrayList<String> list = new ArrayList<>();
2323 while ((next = indexOf(ch, off)) != -1) {
2324 if (!limited || list.size() < limit - 1) {
2325 list.add(substring(off, next));
2326 off = next + 1;
|
445
446 /**
447 * Constructs a new {@code String} by decoding the specified subarray of
448 * bytes using the specified charset. The length of the new {@code String}
449 * is a function of the charset, and hence may not be equal to the length
450 * of the subarray.
451 *
452 * <p> The behavior of this constructor when the given bytes are not valid
453 * in the given charset is unspecified. The {@link
454 * java.nio.charset.CharsetDecoder} class should be used when more control
455 * over the decoding process is required.
456 *
457 * @param bytes
458 * The bytes to be decoded into characters
459 *
460 * @param offset
461 * The index of the first byte to decode
462 *
463 * @param length
464 * The number of bytes to decode
465 *
466 * @param charsetName
467 * The name of a supported {@linkplain java.nio.charset.Charset
468 * charset}
469 *
470 * @throws UnsupportedEncodingException
471 * If the named charset is not supported
472 *
473 * @throws IndexOutOfBoundsException
474 * If {@code offset} is negative, {@code length} is negative, or
475 * {@code offset} is greater than {@code bytes.length - length}
476 *
477 * @since 1.1
478 */
479 public String(byte bytes[], int offset, int length, String charsetName)
480 throws UnsupportedEncodingException {
481 if (charsetName == null)
482 throw new NullPointerException("charsetName");
483 checkBoundsOffCount(offset, length, bytes.length);
484 StringCoding.Result ret =
485 StringCoding.decode(charsetName, bytes, offset, length);
2283 *
2284 * @param regex
2285 * the delimiting regular expression
2286 *
2287 * @param limit
2288 * the result threshold, as described above
2289 *
2290 * @return the array of strings computed by splitting this string
2291 * around matches of the given regular expression
2292 *
2293 * @throws PatternSyntaxException
2294 * if the regular expression's syntax is invalid
2295 *
2296 * @see java.util.regex.Pattern
2297 *
2298 * @since 1.4
2299 * @spec JSR-51
2300 */
2301 public String[] split(String regex, int limit) {
2302 /* fastpath if the regex is a
2303 * (1) one-char String and this character is not one of the
2304 * RegEx's meta characters ".$|()[{^?*+\\", or
2305 * (2) two-char String and the first char is the backslash and
2306 * the second is not the ascii digit or ascii letter.
2307 */
2308 char ch = 0;
2309 if (((regex.length() == 1 &&
2310 ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
2311 (regex.length() == 2 &&
2312 regex.charAt(0) == '\\' &&
2313 (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
2314 ((ch-'a')|('z'-ch)) < 0 &&
2315 ((ch-'A')|('Z'-ch)) < 0)) &&
2316 (ch < Character.MIN_HIGH_SURROGATE ||
2317 ch > Character.MAX_LOW_SURROGATE))
2318 {
2319 int off = 0;
2320 int next = 0;
2321 boolean limited = limit > 0;
2322 ArrayList<String> list = new ArrayList<>();
2323 while ((next = indexOf(ch, off)) != -1) {
2324 if (!limited || list.size() < limit - 1) {
2325 list.add(substring(off, next));
2326 off = next + 1;
|