Skip to content

Commit 10cb72c

Browse files
committed
Improve docs of touched methods and add NPE checks
1 parent b484510 commit 10cb72c

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -717,14 +717,22 @@ private static String decode(Charset charset, byte[] bytes, int offset, int leng
717717
}
718718

719719
/*
720-
* Throws iae, instead of replacing, if malformed or unmappable.
720+
* {@return a new string by decoding from the given UTF-8 bytes array}
721721
*
722+
* @param offset the index of the first byte to decode
723+
* @param length the number of bytes to decode
722724
* @param noShare
723725
* {@code true} if the resulting string MUST NOT share the byte array,
724726
* {@code false} if the byte array can be exclusively used to construct
725727
* 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
726733
*/
727734
static String newStringUTF8(byte[] bytes, int offset, int length, boolean noShare) throws CharacterCodingException {
735+
Objects.requireNonNull(bytes, "bytes");
728736
checkBoundsOffCount(offset, length, bytes.length);
729737
if (length == 0) {
730738
return "";
@@ -922,21 +930,38 @@ private static byte[] encodeWithEncoder(Charset cs, byte coder, byte[] val, bool
922930
return trimArray(ba, bb.position());
923931
}
924932

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
927939
*/
928940
static byte[] getBytesUTF8(String s) throws CharacterCodingException {
941+
Objects.requireNonNull(s, "s");
929942
return encodeUTF8(s.coder(), s.value(), false);
930943
}
931944

932945
private static boolean isASCII(byte[] src) {
933946
return !StringCoding.hasNegatives(src, 0, src.length);
934947
}
935948

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
938961
*/
939962
static byte[] getBytes(String s, Charset cs) throws CharacterCodingException {
963+
Objects.requireNonNull(s, "s");
964+
Objects.requireNonNull(cs, "cs");
940965
byte[] val = s.value();
941966
byte coder = s.coder();
942967
if (cs == UTF_8.INSTANCE) {
@@ -1826,8 +1851,8 @@ public byte[] getBytes(String charsetName)
18261851
throws UnsupportedEncodingException {
18271852
try {
18281853
return encode(lookupCharset(charsetName), coder(), value);
1829-
} catch (CharacterCodingException uce) {
1830-
throw cce2iae(uce);
1854+
} catch (CharacterCodingException cce) {
1855+
throw cce2iae(cce);
18311856
}
18321857
}
18331858

@@ -1853,8 +1878,8 @@ public byte[] getBytes(Charset charset) {
18531878
if (charset == null) throw new NullPointerException();
18541879
try {
18551880
return encode(charset, coder(), value);
1856-
} catch (CharacterCodingException uce) {
1857-
throw cce2iae(uce);
1881+
} catch (CharacterCodingException cce) {
1882+
throw cce2iae(cce);
18581883
}
18591884
}
18601885

@@ -1875,8 +1900,8 @@ public byte[] getBytes(Charset charset) {
18751900
public byte[] getBytes() {
18761901
try {
18771902
return encode(Charset.defaultCharset(), coder(), value);
1878-
} catch (CharacterCodingException uce) {
1879-
throw cce2iae(uce);
1903+
} catch (CharacterCodingException cce) {
1904+
throw cce2iae(cce);
18801905
}
18811906
}
18821907

src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -328,32 +328,32 @@ public interface JavaLangAccess {
328328
String uncheckedNewString(byte[] bytes, Charset cs) throws CharacterCodingException;
329329

330330
/**
331-
* Encode the given string into a sequence of bytes using the specified
332-
* {@linkplain java.nio.charset.Charset charset}.
331+
* {@return the sequence of bytes obtained by encoding the given string in
332+
* the specified {@linkplain java.nio.charset.Charset charset}}
333333
* <p>
334334
* <b>WARNING: This method returns the {@code byte[]} backing the provided
335335
* {@code String}, if the input is ASCII. Hence, the returned byte array
336336
* must not be modified.</b>
337-
* <p>
338-
* This method throws {@code CharacterCodingException} instead of replacing
339-
* when malformed input or unmappable characters are encountered.
340337
*
341338
* @param s the string to encode
342339
* @param cs the charset
343-
* @return the encoded bytes
344-
* @throws CharacterCodingException for malformed input or unmappable characters
340+
* @throws NullPointerException If {@code s} or {@code cs} is null
341+
* @throws CharacterCodingException For malformed input or unmappable characters
345342
*/
346343
byte[] uncheckedGetBytes(String s, Charset cs) throws CharacterCodingException;
347344

348345
/**
349-
* Returns a new string by decoding from the given UTF-8 bytes array.
346+
* {@return a new string by decoding from the given UTF-8 bytes array}
350347
*
351-
* @param off the index of the first byte to decode
352-
* @param len the number of bytes to decode
353-
* @return the newly created string
354-
* @throws IllegalArgumentException for malformed or unmappable bytes.
348+
* @param offset the index of the first byte to decode
349+
* @param length the number of bytes to decode
350+
* @throws NullPointerException If {@code bytes} is null
351+
* @throws StringIndexOutOfBoundsException If {@code offset} is negative,
352+
* {@code length} is negative, or {@code offset} is greater than
353+
* {@code bytes.length - length}
354+
* @throws CharacterCodingException for malformed input or unmappable characters
355355
*/
356-
String newStringUTF8(byte[] bytes, int off, int len) throws CharacterCodingException;
356+
String newStringUTF8(byte[] bytes, int offset, int length) throws CharacterCodingException;
357357

358358
/**
359359
* Get the {@code char} at {@code index} in a {@code byte[]} in internal
@@ -379,11 +379,11 @@ public interface JavaLangAccess {
379379
void uncheckedPutCharUTF16(byte[] bytes, int index, int ch);
380380

381381
/**
382-
* Encode the given string into a sequence of bytes using utf-8.
382+
* {@return the sequence of bytes obtained by encoding the given string in UTF-8}
383383
*
384384
* @param s the string to encode
385-
* @return the encoded bytes in utf-8
386-
* @throws IllegalArgumentException for malformed surrogates
385+
* @throws NullPointerException If {@code s} is null
386+
* @throws CharacterCodingException For malformed input or unmappable characters
387387
*/
388388
byte[] getBytesUTF8(String s) throws CharacterCodingException;
389389

0 commit comments

Comments
 (0)