--- old/src/java.base/share/classes/java/lang/String.java 2014-10-26 21:08:16.320864260 +0000 +++ new/src/java.base/share/classes/java/lang/String.java 2014-10-26 21:08:16.208864259 +0000 @@ -28,6 +28,7 @@ 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; @@ -435,7 +436,7 @@ * The number of bytes to decode * * @param charset - * The {@linkplain java.nio.charset.Charset charset} to be used to + * The {@linkplain java.nio.charset.Charset charset} to be used to * decode the {@code bytes} * * @throws IndexOutOfBoundsException @@ -448,7 +449,41 @@ if (charset == null) throw new NullPointerException("charset"); checkBounds(bytes, offset, length); - this.value = StringCoding.decode(charset, 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 remaining number of bytes in the + * {@linkplain java.nio.ByteBuffer byte buffer}. + * + *

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. + * + *

+ * Bytes are read between position() and limit() + * of the {@link java.nio.ByteBuffer}. + * After the bytes have been read the position() will be + * updated. + * + * @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} + * + * @since 1.9 + */ + public String(ByteBuffer bytes, Charset charset) { + if (charset == null) + throw new NullPointerException("charset"); + this.value = StringCoding.decode(charset, bytes); } /** @@ -925,9 +960,70 @@ * @since 1.6 */ public byte[] getBytes(Charset charset) { - if (charset == null) throw new NullPointerException(); + Objects.requireNonNull(charset); return StringCoding.encode(charset, value, 0, value.length); } + + /** + * 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. + *

+ * The number of bytes written during encoding is a function of the charset + * used to perform the encoding and is returned from this method. It may not + * be equal to the length of this + * String. At most destBuffer.length - destOffset bytes will be written. + * + * @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 written + * + * @since 1.9 + */ + public int getBytes(byte[] destBuffer, int destOffset, Charset charset) { + Objects.requireNonNull(destBuffer); + Objects.requireNonNull(charset); + return StringCoding.encode(charset, value, 0, value.length, destBuffer, destOffset); + } + + /** + * Encodes as many characters as possible from 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. + *

+ * The number of bytes written during encoding is a function of the charset + * used to perform the encoding and is returned from this method. It may not + * be equal to the length of this + * String. At most destBuffer.remaining() bytes will be written. + *

+ * The buffer's position will be advanced to reflect the characters read and + * the bytes written, but its mark and limit will not be modified. + * + * @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 written + * + * @since 1.9 + */ + public int getBytes(ByteBuffer destBuffer, 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