Skip to content

Commit 9b00a7b

Browse files
Switch to use Utf8.isValidUtf8() on the entire range we copy instead of first validating a subrange and then copying it to a dedicated byte[].
This close to an effective no-op today, but in future changes we should be able to be more efficient when we are validating an entire byte[] instead of only a subrange of one. PiperOrigin-RevId: 866457946
1 parent 46dfb31 commit 9b00a7b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,11 @@ static ByteString copyFrom(byte[] bytes, int offset, int size, boolean requireUt
409409
return EMPTY;
410410
}
411411
checkRange(offset, offset + size, bytes.length);
412-
if (requireUtf8 && !Utf8.isValidUtf8(bytes, offset, offset + size)) {
412+
byte[] copy = byteArrayCopier.copyFrom(bytes, offset, size);
413+
if (requireUtf8 && !Utf8.isValidUtf8(copy)) {
413414
throw InvalidProtocolBufferException.invalidUtf8();
414415
}
415-
return new LiteralByteString(byteArrayCopier.copyFrom(bytes, offset, size));
416+
return new LiteralByteString(copy);
416417
}
417418

418419
/**

0 commit comments

Comments
 (0)