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

Print this page

        

*** 27,36 **** --- 27,37 ---- import java.io.ObjectStreamField; import java.io.UnsupportedEncodingException; import java.lang.annotation.Native; import java.nio.charset.Charset; + import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Formatter; import java.util.Locale;
*** 460,471 **** public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); checkBoundsOffCount(offset, length, bytes.length); ! StringCoding.Result ret = ! StringCoding.decode(charsetName, bytes, offset, length); this.value = ret.value; this.coder = ret.coder; } /** --- 461,472 ---- public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); checkBoundsOffCount(offset, length, bytes.length); ! StringCoder.Result ret = ! StringCoder.decode(charsetName, bytes, offset, length); this.value = ret.value; this.coder = ret.coder; } /**
*** 500,511 **** */ public String(byte bytes[], int offset, int length, Charset charset) { if (charset == null) throw new NullPointerException("charset"); checkBoundsOffCount(offset, length, bytes.length); ! StringCoding.Result ret = ! StringCoding.decode(charset, bytes, offset, length); this.value = ret.value; this.coder = ret.coder; } /** --- 501,548 ---- */ public String(byte bytes[], int offset, int length, Charset charset) { if (charset == null) throw new NullPointerException("charset"); checkBoundsOffCount(offset, length, bytes.length); ! StringCoder.Result ret = ! StringCoder.decode(charset, bytes, offset, length); ! this.value = ret.value; ! this.coder = ret.coder; ! } ! ! /** ! * Constructs a new {@code String} by decoding the remaining bytes in the specified ! * {@linkplain java.nio.ByteBuffer byte buffer} using the specified ! * {@linkplain java.nio.charset.Charset charset}. ! * The length of the new {@code String} is a function of the charset, and ! * hence may not be equal to the remaining number of bytes in the ! * {@linkplain java.nio.ByteBuffer byte buffer}. ! * ! * <p> This method always replaces malformed-input and unmappable-character ! * sequences with this charset's default replacement string. The {@link ! * java.nio.charset.CharsetDecoder} class should be used when more control ! * over the decoding process is required. ! * ! * <p> ! * Bytes are read between <code>position()</code> and <code>limit()</code> ! * of the {@link java.nio.ByteBuffer}. ! * After the bytes have been read the <code>position()</code> will be ! * updated. ! * ! * @param src ! * The source of bytes to be decoded into characters ! * ! * @param charset ! * The {@linkplain java.nio.charset.Charset charset} to be used to ! * decode the {@code src} ! * ! * @since 11 ! */ ! public String(ByteBuffer src, Charset charset) { ! Objects.requireNonNull(src); ! Objects.requireNonNull(charset); ! StringCoder.Result ret = StringCoder.decode(charset, src); this.value = ret.value; this.coder = ret.coder; } /**
*** 586,596 **** * * @since 1.1 */ public String(byte bytes[], int offset, int length) { checkBoundsOffCount(offset, length, bytes.length); ! StringCoding.Result ret = StringCoding.decode(bytes, offset, length); this.value = ret.value; this.coder = ret.coder; } /** --- 623,633 ---- * * @since 1.1 */ public String(byte bytes[], int offset, int length) { checkBoundsOffCount(offset, length, bytes.length); ! StringCoder.Result ret = StringCoder.decode(bytes, offset, length); this.value = ret.value; this.coder = ret.coder; } /**
*** 935,945 **** * @since 1.1 */ public byte[] getBytes(String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException(); ! return StringCoding.encode(charsetName, coder(), value); } /** * Encodes this {@code String} into a sequence of bytes using the given * {@linkplain java.nio.charset.Charset charset}, storing the result into a --- 972,982 ---- * @since 1.1 */ public byte[] getBytes(String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException(); ! return StringCoder.encode(charsetName, coder(), value); } /** * Encodes this {@code String} into a sequence of bytes using the given * {@linkplain java.nio.charset.Charset charset}, storing the result into a
*** 958,968 **** * * @since 1.6 */ public byte[] getBytes(Charset charset) { if (charset == null) throw new NullPointerException(); ! return StringCoding.encode(charset, coder(), value); } /** * Encodes this {@code String} into a sequence of bytes using the * platform's default charset, storing the result into a new byte array. --- 995,1042 ---- * * @since 1.6 */ public byte[] getBytes(Charset charset) { if (charset == null) throw new NullPointerException(); ! return StringCoder.encode(charset, coder(), value); ! } ! ! /** ! * Encodes characters from this string into a sequence of bytes using the given ! * {@linkplain java.nio.charset.Charset charset}, storing the result into the ! * given destination {@linkplain java.nio.ByteBuffer byte buffer}. ! * ! * <p> The first character to be encoded is at index {@code srcBegin}; ! * the last character to be encoded is at index {@code srcEnd-1}. At most ! * {@code srcEnd-srcBegin}) of characters will be encoded, and at most ! * {@link java.nio.Buffer#remaining dstBuffer.remaining()} bytes will be written. ! * The position of the {@code dstBuffer} will be advanced to reflect the bytes ! * written, but its mark and limit will not be modified. ! * ! * <p> This method always replaces malformed-input and unmappable-character ! * sequences with this charset's default replacement string. The {@link ! * java.nio.charset.CharsetDecoder} class should be used when more control ! * over the decoding process is required. ! * ! * @param srcBegin ! * index of the first character in the string to encode ! * @param srcEnd ! * index after the last character in the string to encode ! * @param dstBuffer ! * The destination {@linkplain java.nio.ByteBuffer} ! * @param charset ! * The {@linkplain java.nio.charset.Charset} to be used to encode ! * the {@code String} ! * @return the number of characters encoded into the {code dstBuffer}; ! * ! * @since 11 ! */ ! public int getBytes(int srcBegin, int srcEnd, ByteBuffer dstBuffer, Charset charset) { ! Objects.requireNonNull(dstBuffer); ! Objects.requireNonNull(charset); ! checkBoundsBeginEnd(srcBegin, srcEnd, length()); ! return StringCoder.encode(charset, coder(), value, srcBegin, srcEnd, dstBuffer); } /** * Encodes this {@code String} into a sequence of bytes using the * platform's default charset, storing the result into a new byte array.
*** 975,985 **** * @return The resultant byte array * * @since 1.1 */ public byte[] getBytes() { ! return StringCoding.encode(coder(), value); } /** * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code --- 1049,1059 ---- * @return The resultant byte array * * @since 1.1 */ public byte[] getBytes() { ! return StringCoder.encode(coder(), value); } /** * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code