Use InputStream's contracts instead of StreamUtils

Since Spring Framework 6.0 requires Java 17, we can now use
`InputStream` new contracts when manipulating streams.

Closes gh-27702
This commit is contained in:
유예본(Yebon You)/Platform Engineering팀/11ST 2021-11-19 16:08:14 +09:00 committed by Brian Clozel
parent 0770d86936
commit fa1f7f6dc7
4 changed files with 12 additions and 24 deletions

View File

@ -108,12 +108,10 @@ public abstract class FileCopyUtils {
Assert.notNull(in, "No InputStream specified"); Assert.notNull(in, "No InputStream specified");
Assert.notNull(out, "No OutputStream specified"); Assert.notNull(out, "No OutputStream specified");
try { try (in; out) {
return StreamUtils.copy(in, out); int count = (int) in.transferTo(out);
} out.flush();
finally { return count;
close(in);
close(out);
} }
} }

View File

@ -64,9 +64,7 @@ public abstract class StreamUtils {
return EMPTY_CONTENT; return EMPTY_CONTENT;
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); return in.readAllBytes();
copy(in, out);
return out.toByteArray();
} }
/** /**
@ -152,15 +150,9 @@ public abstract class StreamUtils {
Assert.notNull(in, "No InputStream specified"); Assert.notNull(in, "No InputStream specified");
Assert.notNull(out, "No OutputStream specified"); Assert.notNull(out, "No OutputStream specified");
int byteCount = 0; int count = (int) in.transferTo(out);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
out.flush(); out.flush();
return byteCount; return count;
} }
/** /**

View File

@ -16,7 +16,6 @@
package org.springframework.http.converter; package org.springframework.http.converter;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
@ -53,11 +52,7 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
@Override @Override
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException { public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
long contentLength = inputMessage.getHeaders().getContentLength(); return inputMessage.getBody().readAllBytes();
ByteArrayOutputStream bos =
new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE);
StreamUtils.copy(inputMessage.getBody(), bos);
return bos.toByteArray();
} }
@Override @Override

View File

@ -19,6 +19,7 @@ package org.springframework.http.converter;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
@ -144,7 +145,9 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
try { try {
InputStream in = resource.getInputStream(); InputStream in = resource.getInputStream();
try { try {
StreamUtils.copy(in, outputMessage.getBody()); OutputStream out = outputMessage.getBody();
in.transferTo(out);
out.flush();
} }
catch (NullPointerException ex) { catch (NullPointerException ex) {
// ignore, see SPR-13620 // ignore, see SPR-13620