arunpandianp commented on code in PR #37912:
URL: https://github.com/apache/beam/pull/37912#discussion_r2987482744
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/StreamUtils.java:
##########
@@ -35,34 +37,67 @@ private StreamUtils() {}
private static final int BUF_SIZE = 8192;
- private static ThreadLocal<SoftReference<byte[]>> threadLocalBuffer = new
ThreadLocal<>();
+ private static final ThreadLocal<SoftReference<byte[]>> threadLocalBuffer =
new ThreadLocal<>();
/** Efficient converting stream to bytes. */
public static byte[] getBytesWithoutClosing(InputStream stream) throws
IOException {
+ // Unwrap the stream so the below optimizations based upon class type
function properly.
+ // We don't use mark or reset in this function.
+ while (stream instanceof UnownedInputStream) {
+ stream = ((UnownedInputStream) stream).getWrappedStream();
+ }
+
if (stream instanceof ExposedByteArrayInputStream) {
// Fast path for the exposed version.
return ((ExposedByteArrayInputStream) stream).readAll();
- } else if (stream instanceof ByteArrayInputStream) {
+ }
+ if (stream instanceof ByteArrayInputStream) {
// Fast path for ByteArrayInputStream.
byte[] ret = new byte[stream.available()];
stream.read(ret);
return ret;
}
- // Falls back to normal stream copying.
+
+ // Most inputs are fully available so we attempt to first read directly
+ // into a buffer of the right size, assuming available reflects all the
bytes.
+ int available = stream.available();
+ @Nullable ByteArrayOutputStream outputStream = null;
+ if (available > 0 && available < 1024 * 1024) {
+ byte[] initialBuffer = new byte[available];
+ int initialReadSize = stream.read(initialBuffer);
+ if (initialReadSize == -1) {
+ return new byte[0];
+ }
+ int nextChar = stream.read();
Review Comment:
nit: nextByte?
--
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]