From fa1f7f6dc7b86d0588dbf90ce9d255de109d0f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B2=E1=84=8B=E1=85=A8=E1=84=87=E1=85=A9?= =?UTF-8?q?=E1=86=AB=28Yebon=20You=29/Platform=20Engineering=E1=84=90?= =?UTF-8?q?=E1=85=B5=E1=86=B7/11ST?= Date: Fri, 19 Nov 2021 16:08:14 +0900 Subject: [PATCH] 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 --- .../org/springframework/util/FileCopyUtils.java | 10 ++++------ .../java/org/springframework/util/StreamUtils.java | 14 +++----------- .../converter/ByteArrayHttpMessageConverter.java | 7 +------ .../converter/ResourceHttpMessageConverter.java | 5 ++++- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java index 37fb1a960b5..73140dd80e7 100644 --- a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java @@ -108,12 +108,10 @@ public abstract class FileCopyUtils { Assert.notNull(in, "No InputStream specified"); Assert.notNull(out, "No OutputStream specified"); - try { - return StreamUtils.copy(in, out); - } - finally { - close(in); - close(out); + try (in; out) { + int count = (int) in.transferTo(out); + out.flush(); + return count; } } diff --git a/spring-core/src/main/java/org/springframework/util/StreamUtils.java b/spring-core/src/main/java/org/springframework/util/StreamUtils.java index a5f296be2ae..a991ca37349 100644 --- a/spring-core/src/main/java/org/springframework/util/StreamUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StreamUtils.java @@ -64,9 +64,7 @@ public abstract class StreamUtils { return EMPTY_CONTENT; } - ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); - copy(in, out); - return out.toByteArray(); + return in.readAllBytes(); } /** @@ -152,15 +150,9 @@ public abstract class StreamUtils { Assert.notNull(in, "No InputStream specified"); Assert.notNull(out, "No OutputStream specified"); - int byteCount = 0; - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - byteCount += bytesRead; - } + int count = (int) in.transferTo(out); out.flush(); - return byteCount; + return count; } /** diff --git a/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java index 648f2406bd9..b5be96421b4 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java @@ -16,7 +16,6 @@ package org.springframework.http.converter; -import java.io.ByteArrayOutputStream; import java.io.IOException; import org.springframework.http.HttpInputMessage; @@ -53,11 +52,7 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter< @Override public byte[] readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { - long contentLength = inputMessage.getHeaders().getContentLength(); - ByteArrayOutputStream bos = - new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE); - StreamUtils.copy(inputMessage.getBody(), bos); - return bos.toByteArray(); + return inputMessage.getBody().readAllBytes(); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java index e49bdd25510..6eb6cea988a 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java @@ -19,6 +19,7 @@ package org.springframework.http.converter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.InputStreamResource; @@ -144,7 +145,9 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter