@@ -717,14 +717,22 @@ private static String decode(Charset charset, byte[] bytes, int offset, int leng
717
717
}
718
718
719
719
/*
720
- * Throws iae, instead of replacing, if malformed or unmappable.
720
+ * {@return a new string by decoding from the given UTF-8 bytes array}
721
721
*
722
+ * @param offset the index of the first byte to decode
723
+ * @param length the number of bytes to decode
722
724
* @param noShare
723
725
* {@code true} if the resulting string MUST NOT share the byte array,
724
726
* {@code false} if the byte array can be exclusively used to construct
725
727
* the string and is not modified or used for any other purpose.
728
+ * @throws NullPointerException If {@code bytes} is null
729
+ * @throws StringIndexOutOfBoundsException If {@code offset} is negative,
730
+ * {@code length} is negative, or {@code offset} is greater than
731
+ * {@code bytes.length - length}
732
+ * @throws CharacterCodingException for malformed input or unmappable characters
726
733
*/
727
734
static String newStringUTF8 (byte [] bytes , int offset , int length , boolean noShare ) throws CharacterCodingException {
735
+ Objects .requireNonNull (bytes , "bytes" );
728
736
checkBoundsOffCount (offset , length , bytes .length );
729
737
if (length == 0 ) {
730
738
return "" ;
@@ -922,21 +930,38 @@ private static byte[] encodeWithEncoder(Charset cs, byte coder, byte[] val, bool
922
930
return trimArray (ba , bb .position ());
923
931
}
924
932
925
- /*
926
- * Throws iae, instead of replacing, if unmappable.
933
+ /**
934
+ * {@return the sequence of bytes obtained by encoding the given string in UTF-8}
935
+ *
936
+ * @param s the string to encode
937
+ * @throws NullPointerException If {@code s} is null
938
+ * @throws CharacterCodingException For malformed input or unmappable characters
927
939
*/
928
940
static byte [] getBytesUTF8 (String s ) throws CharacterCodingException {
941
+ Objects .requireNonNull (s , "s" );
929
942
return encodeUTF8 (s .coder (), s .value (), false );
930
943
}
931
944
932
945
private static boolean isASCII (byte [] src ) {
933
946
return !StringCoding .hasNegatives (src , 0 , src .length );
934
947
}
935
948
936
- /*
937
- * Throws CCE, instead of replacing, if unmappable.
949
+ /**
950
+ * {@return the sequence of bytes obtained by encoding the given string in
951
+ * the specified {@linkplain java.nio.charset.Charset charset}}
952
+ * <p>
953
+ * <b>WARNING: This method returns the {@code byte[]} backing the provided
954
+ * {@code String}, if the input is ASCII. Hence, the returned byte array
955
+ * must not be modified.</b>
956
+ *
957
+ * @param s the string to encode
958
+ * @param cs the charset
959
+ * @throws NullPointerException If {@code s} or {@code cs} is null
960
+ * @throws CharacterCodingException For malformed input or unmappable characters
938
961
*/
939
962
static byte [] getBytes (String s , Charset cs ) throws CharacterCodingException {
963
+ Objects .requireNonNull (s , "s" );
964
+ Objects .requireNonNull (cs , "cs" );
940
965
byte [] val = s .value ();
941
966
byte coder = s .coder ();
942
967
if (cs == UTF_8 .INSTANCE ) {
@@ -1826,8 +1851,8 @@ public byte[] getBytes(String charsetName)
1826
1851
throws UnsupportedEncodingException {
1827
1852
try {
1828
1853
return encode (lookupCharset (charsetName ), coder (), value );
1829
- } catch (CharacterCodingException uce ) {
1830
- throw cce2iae (uce );
1854
+ } catch (CharacterCodingException cce ) {
1855
+ throw cce2iae (cce );
1831
1856
}
1832
1857
}
1833
1858
@@ -1853,8 +1878,8 @@ public byte[] getBytes(Charset charset) {
1853
1878
if (charset == null ) throw new NullPointerException ();
1854
1879
try {
1855
1880
return encode (charset , coder (), value );
1856
- } catch (CharacterCodingException uce ) {
1857
- throw cce2iae (uce );
1881
+ } catch (CharacterCodingException cce ) {
1882
+ throw cce2iae (cce );
1858
1883
}
1859
1884
}
1860
1885
@@ -1875,8 +1900,8 @@ public byte[] getBytes(Charset charset) {
1875
1900
public byte [] getBytes () {
1876
1901
try {
1877
1902
return encode (Charset .defaultCharset (), coder (), value );
1878
- } catch (CharacterCodingException uce ) {
1879
- throw cce2iae (uce );
1903
+ } catch (CharacterCodingException cce ) {
1904
+ throw cce2iae (cce );
1880
1905
}
1881
1906
}
1882
1907
0 commit comments