Skip to content

Commit 4e896c8

Browse files
stsypanovsbrannen
authored andcommitted
Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray()
InputStream.readAllBytes() allows us to avoid the creation of an intermediate ByteArrayOutputStream and is likely to perform better. Closes gh-30155
1 parent 9421fe1 commit 4e896c8

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

spring-core/src/main/java/org/springframework/util/FileCopyUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.util;
1818

1919
import java.io.ByteArrayInputStream;
20-
import java.io.ByteArrayOutputStream;
2120
import java.io.Closeable;
2221
import java.io.File;
2322
import java.io.IOException;
@@ -146,9 +145,9 @@ public static byte[] copyToByteArray(@Nullable InputStream in) throws IOExceptio
146145
return new byte[0];
147146
}
148147

149-
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
150-
copy(in, out);
151-
return out.toByteArray();
148+
try (in) {
149+
return in.readAllBytes();
150+
}
152151
}
153152

154153

0 commit comments

Comments
 (0)