Skip to content

Commit

Permalink
Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray()
Browse files Browse the repository at this point in the history
InputStream.readAllBytes() allows us to avoid the creation of an
intermediate ByteArrayOutputStream and is likely to perform better.

Closes spring-projectsgh-30155
  • Loading branch information
stsypanov authored and sbrannen committed Mar 21, 2023
1 parent 9421fe1 commit 4e896c8
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
package org.springframework.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -146,9 +145,9 @@ public static byte[] copyToByteArray(@Nullable InputStream in) throws IOExceptio
return new byte[0];
}

ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
copy(in, out);
return out.toByteArray();
try (in) {
return in.readAllBytes();
}
}


Expand Down

0 comments on commit 4e896c8

Please sign in to comment.