From a17a9d36dafe6d0fc9b6eb7b3ae263d2bc206ac4 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 1/2] use InputStream's new method instead of StreamUtils --- .../springframework/util/FileCopyUtils.java | 12 ++++----- .../org/springframework/util/StreamUtils.java | 27 +------------------ .../util/StreamUtilsTests.java | 7 ----- .../ByteArrayHttpMessageConverter.java | 6 +---- .../ResourceHttpMessageConverter.java | 24 ++++++----------- 5 files changed, 15 insertions(+), 61 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 37fb1a960b55..c9c99c94cadb 100644 --- a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java @@ -108,13 +108,11 @@ public static int copy(InputStream in, OutputStream out) throws IOException { 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 f57943a29e8d..5b5a2d3300bb 100644 --- a/spring-core/src/main/java/org/springframework/util/StreamUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StreamUtils.java @@ -65,9 +65,7 @@ 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(); + return in.readAllBytes(); } /** @@ -141,29 +139,6 @@ public static void copy(String in, Charset charset, OutputStream out) throws IOE writer.flush(); } - /** - * Copy the contents of the given InputStream to the given OutputStream. - *

Leaves both streams open when done. - * @param in the InputStream to copy from - * @param out the OutputStream to copy to - * @return the number of bytes copied - * @throws IOException in case of I/O errors - */ - public static int copy(InputStream in, OutputStream out) throws IOException { - 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; - } - out.flush(); - return byteCount; - } - /** * Copy a range of content of the given InputStream to the given OutputStream. *

If the specified range exceeds the length of the InputStream, this copies diff --git a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java index fad81878e7ed..450a22058e45 100644 --- a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java @@ -83,13 +83,6 @@ void copyString() throws Exception { assertThat(out.toByteArray()).isEqualTo(string.getBytes(charset)); } - @Test - void copyStream() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - StreamUtils.copy(new ByteArrayInputStream(bytes), out); - assertThat(out.toByteArray()).isEqualTo(bytes); - } - @Test void copyRange() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); 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 648f2406bd9d..adc64dfc186c 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 @@ -53,11 +53,7 @@ public boolean supports(Class clazz) { @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 f587aecf7ff9..0b54b1ca085e 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 @@ 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; @@ -131,22 +132,13 @@ protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) protected void writeContent(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { - try { - InputStream in = resource.getInputStream(); - try { - StreamUtils.copy(in, outputMessage.getBody()); - } - catch (NullPointerException ex) { - // ignore, see SPR-13620 - } - finally { - try { - in.close(); - } - catch (Throwable ex) { - // ignore, see SPR-12999 - } - } + try (InputStream in = resource.getInputStream()){ + OutputStream out = outputMessage.getBody(); + in.transferTo(out); + out.flush(); + } + catch (NullPointerException ex) { + // ignore, see SPR-13620 } catch (FileNotFoundException ex) { // ignore, see SPR-12999 From cd39462500f8648ec96409fa746263af9a979085 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: Tue, 23 Nov 2021 16:32:25 +0900 Subject: [PATCH 2/2] undo removing unused method to support legacy projects --- .../springframework/util/FileCopyUtils.java | 2 +- .../org/springframework/util/StreamUtils.java | 23 +++++++++++++++++++ .../util/StreamUtilsTests.java | 7 ++++++ .../ByteArrayHttpMessageConverter.java | 1 - 4 files changed, 31 insertions(+), 2 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 c9c99c94cadb..73140dd80e70 100644 --- a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java @@ -112,7 +112,7 @@ public static int copy(InputStream in, OutputStream out) throws IOException { 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 5b5a2d3300bb..04fcd15b90e6 100644 --- a/spring-core/src/main/java/org/springframework/util/StreamUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StreamUtils.java @@ -139,6 +139,29 @@ public static void copy(String in, Charset charset, OutputStream out) throws IOE writer.flush(); } + /** + * Copy the contents of the given InputStream to the given OutputStream. + *

Leaves both streams open when done. + * @param in the InputStream to copy from + * @param out the OutputStream to copy to + * @return the number of bytes copied + * @throws IOException in case of I/O errors + */ + public static int copy(InputStream in, OutputStream out) throws IOException { + 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; + } + out.flush(); + return byteCount; + } + /** * Copy a range of content of the given InputStream to the given OutputStream. *

If the specified range exceeds the length of the InputStream, this copies diff --git a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java index 450a22058e45..fad81878e7ed 100644 --- a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java @@ -83,6 +83,13 @@ void copyString() throws Exception { assertThat(out.toByteArray()).isEqualTo(string.getBytes(charset)); } + @Test + void copyStream() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + StreamUtils.copy(new ByteArrayInputStream(bytes), out); + assertThat(out.toByteArray()).isEqualTo(bytes); + } + @Test void copyRange() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); 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 adc64dfc186c..b5be96421b46 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;