robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1181805751


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/ByteStringOutputStream.java:
##########
@@ -153,6 +157,47 @@ public ByteString toByteStringAndReset() {
     return rval;
   }
 
+  /**
+   * Creates a byte string with the given size containing the prefix of the 
contents of this output
+   * stream.
+   */
+  public ByteString consumePrefixToByteString(int prefixSize) {
+    ByteString rval;
+    Preconditions.checkArgument(prefixSize <= size());
+    if (prefixSize == size()) {
+      return toByteStringAndReset();
+    }
+    int bytesFromBuffer = prefixSize - result.size();
+    if (bytesFromBuffer == 0) {
+      rval = result;
+      result = ByteString.EMPTY;
+      return rval;
+    }
+    if (bytesFromBuffer < 0) {
+      rval = result.substring(0, prefixSize);
+      result = result.substring(prefixSize);
+      return rval;
+    }
+    // Copy or reference the kept bytes for the rval.
+    if (shouldCopy(buffer.length, bytesFromBuffer)) {
+      byte[] bufferCopy = new byte[bytesFromBuffer];
+      System.arraycopy(buffer, 0, bufferCopy, 0, bytesFromBuffer);
+      rval = result.concat(UnsafeByteOperations.unsafeWrap(bufferCopy));

Review Comment:
   Any reason not to us ByteString.copyFrom here? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to