src/java.base/share/classes/java/lang/String.java
Print this page
*** 26,35 ****
--- 26,36 ----
package java.lang;
import java.io.ObjectStreamField;
import java.io.UnsupportedEncodingException;
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;
*** 450,459 ****
--- 451,491 ----
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charset, bytes, offset, length);
}
/**
+ * Constructs a new {@code String} by decoding 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 length of the subarray.
+ *
+ * <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 bytes
+ * The bytes to be decoded into characters
+ *
+ * @param charset
+ * The {@linkplain java.nio.charset.Charset charset} to be used to
+ * decode the {@code bytes}
+ *
+ * @throws IndexOutOfBoundsException
+ * If the {@code offset} and {@code length} arguments index
+ * characters outside the bounds of the {@code bytes} array
+ *
+ * @since 1.9
+ */
+ public String(ByteBuffer bytes, Charset charset) {
+ if (charset == null)
+ throw new NullPointerException("charset");
+ this.value = StringCoding.decode(charset, bytes);
+ }
+
+ /**
* Constructs a new {@code String} by decoding the specified array of bytes
* 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 length of the byte array.
*
*** 923,937 ****
* @return The resultant byte array
*
* @since 1.6
*/
public byte[] getBytes(Charset charset) {
! if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
/**
* Encodes this {@code String} into a sequence of bytes using the
* platform's default charset, storing the result into a new byte array.
*
* <p> The behavior of this method when this string cannot be encoded in
* the default charset is unspecified. The {@link
--- 955,1016 ----
* @return The resultant byte array
*
* @since 1.6
*/
public byte[] getBytes(Charset charset) {
! Objects.requireNonNull(charset);
return StringCoding.encode(charset, value, 0, value.length);
}
/**
+ * <p>Encodes this {@code String} into a sequence of bytes using the given
+ * {@linkplain java.nio.charset.Charset charset}, storing the result into a
+ * byte array that has been passed as an argument.
+ *
+ * @param destBuffer
+ * The destination array
+ *
+ * @param destOffset
+ * The start offset in the destination array
+ *
+ * @param charset
+ * The {@linkplain java.nio.charset.Charset} to be used to encode
+ * the {@code String}
+ *
+ * @return the number of bytes copied
+ *
+ * @since 1.9
+ */
+ public int getBytes(final byte[] destBuffer, final int destOffset, final Charset charset) {
+ Objects.requireNonNull(destBuffer);
+ Objects.requireNonNull(charset);
+ return StringCoding.encode(charset, value, 0, value.length, destBuffer, destOffset);
+ }
+
+ /**
+ * <p>Encodes this {@code String} into a sequence of bytes using the given
+ * {@linkplain java.nio.charset.Charset charset}, storing the result into a
+ * {@linkplain java.nio.ByteBuffer byte buffer} that has been passed as an argument.
+ *
+ * @param destBuffer
+ * 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 bytes copied
+ *
+ * @since 1.9
+ */
+ public int getBytes(final ByteBuffer destBuffer, final Charset charset) {
+ Objects.requireNonNull(destBuffer);
+ Objects.requireNonNull(charset);
+ return StringCoding.encode(charset, value, 0, value.length, destBuffer);
+ }
+
+ /**
* Encodes this {@code String} into a sequence of bytes using the
* platform's default charset, storing the result into a new byte array.
*
* <p> The behavior of this method when this string cannot be encoded in
* the default charset is unspecified. The {@link