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] 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;