Skip to content

Commit c5a3469

Browse files
kluevercopybara-github
authored andcommitted
Deprecate ByteString.copyFrom(String, String) and ByteString.toString(String); prefer the Charset-accepting APIs instead.
PiperOrigin-RevId: 850392112
1 parent fa88f32 commit c5a3469

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

java/core/src/main/java/com/google/protobuf/ByteString.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
7878
*
7979
* <p>One of the noticeable costs of copying a byte[] into a new array using {@code
8080
* System.arraycopy} is nullification of a new buffer before the copy. It has been shown the
81-
* Hotspot VM is capable to intrisicfy {@code Arrays.copyOfRange} operation to avoid this
81+
* Hotspot VM is capable of intrinsic {@code Arrays.copyOfRange} operation to avoid this
8282
* expensive nullification and provide substantial performance gain. Unfortunately this does not
8383
* hold on Android runtimes and could make the copy slightly slower due to additional code in the
8484
* {@code Arrays.copyOfRange}. Thus we provide two different implementation for array copier for
@@ -538,13 +538,12 @@ public static ByteString copyFrom(ByteBuffer bytes) {
538538
* @param charsetName encoding to use
539539
* @return new {@code ByteString}
540540
* @throws UnsupportedEncodingException if the encoding isn't found
541+
* @deprecated Use {@link #copyFrom(String, Charset)} instead.
541542
*/
543+
@Deprecated
542544
public static ByteString copyFrom(String text, String charsetName)
543545
throws UnsupportedEncodingException {
544-
if (text.isEmpty()) {
545-
return EMPTY;
546-
}
547-
return new LiteralByteString(text.getBytes(charsetName));
546+
return text.isEmpty() ? EMPTY : new LiteralByteString(text.getBytes(charsetName));
548547
}
549548

550549
/**
@@ -556,10 +555,7 @@ public static ByteString copyFrom(String text, String charsetName)
556555
* @return new {@code ByteString}
557556
*/
558557
public static ByteString copyFrom(String text, Charset charset) {
559-
if (text.isEmpty()) {
560-
return EMPTY;
561-
}
562-
return new LiteralByteString(text.getBytes(charset));
558+
return text.isEmpty() ? EMPTY : new LiteralByteString(text.getBytes(charset));
563559
}
564560

565561
/**
@@ -570,10 +566,7 @@ public static ByteString copyFrom(String text, Charset charset) {
570566
* @return new {@code ByteString}
571567
*/
572568
public static ByteString copyFromUtf8(String text) {
573-
if (text.isEmpty()) {
574-
return EMPTY;
575-
}
576-
return new LiteralByteString(text.getBytes(Internal.UTF_8));
569+
return text.isEmpty() ? EMPTY : new LiteralByteString(text.getBytes(Internal.UTF_8));
577570
}
578571

579572
// =================================================================
@@ -908,7 +901,9 @@ abstract void writeToInternal(OutputStream out, int sourceOffset, int numberToWr
908901
* @param charsetName encode using this charset
909902
* @return new string
910903
* @throws UnsupportedEncodingException if charset isn't recognized
904+
* @deprecated Use {@link #toString(Charset)} instead.
911905
*/
906+
@Deprecated
912907
public final String toString(String charsetName) throws UnsupportedEncodingException {
913908
try {
914909
return toString(Charset.forName(charsetName));
@@ -927,7 +922,7 @@ public final String toString(String charsetName) throws UnsupportedEncodingExcep
927922
* @return new string
928923
*/
929924
public final String toString(Charset charset) {
930-
return size() == 0 ? "" : toStringInternal(charset);
925+
return isEmpty() ? "" : toStringInternal(charset);
931926
}
932927

933928
/**

0 commit comments

Comments
 (0)