Skip to content

Commit 6661788

Browse files
committed
Ensure HTTP classes don't close streams
Prior to this commit several HTTP classes made use of FileCopyUtils when reading from or writing to streams. This has the unfortunate side effect of closing streams that should really be left open. The problem is particularly noticeable when dealing with a FormHttpMessageConverter that is writing a multi-part response. Relevant HTTP classes have now been refactored to make use of a new StreamUtils class that works in a similar way FileCopyUtils but does not close streams. The NonClosingOutputStream class from SimpleStreamingClientHttpRequest has also been refactored to a StreamUtils method. Issue: SPR-10095
1 parent 08e1cbc commit 6661788

File tree

13 files changed

+360
-80
lines changed

13 files changed

+360
-80
lines changed

spring-core/src/main/java/org/springframework/util/FileCopyUtils.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,19 +31,19 @@
3131
import java.io.Writer;
3232

3333
/**
34-
* Simple utility methods for file and stream copying.
35-
* All copy methods use a block size of 4096 bytes,
36-
* and close all affected streams when done.
34+
* Simple utility methods for file and stream copying. All copy methods use a block size
35+
* of 4096 bytes, and close all affected streams when done. A variation of the copy
36+
* methods from this class that leave streams open can be found in {@link StreamUtils}.
3737
*
38-
* <p>Mainly for use within the framework,
39-
* but also useful for application code.
38+
* <p>Mainly for use within the framework, but also useful for application code.
4039
*
4140
* @author Juergen Hoeller
4241
* @since 06.10.2003
42+
* @see StreamUtils
4343
*/
4444
public abstract class FileCopyUtils {
4545

46-
public static final int BUFFER_SIZE = 4096;
46+
public static final int BUFFER_SIZE = StreamUtils.BUFFER_SIZE;
4747

4848

4949
//---------------------------------------------------------------------
@@ -106,15 +106,7 @@ public static int copy(InputStream in, OutputStream out) throws IOException {
106106
Assert.notNull(in, "No InputStream specified");
107107
Assert.notNull(out, "No OutputStream specified");
108108
try {
109-
int byteCount = 0;
110-
byte[] buffer = new byte[BUFFER_SIZE];
111-
int bytesRead = -1;
112-
while ((bytesRead = in.read(buffer)) != -1) {
113-
out.write(buffer, 0, bytesRead);
114-
byteCount += bytesRead;
115-
}
116-
out.flush();
117-
return byteCount;
109+
return StreamUtils.copy(in, out);
118110
}
119111
finally {
120112
try {
@@ -208,7 +200,7 @@ public static int copy(Reader in, Writer out) throws IOException {
208200

209201
/**
210202
* Copy the contents of the given String to the given output Writer.
211-
* Closes the write when done.
203+
* Closes the writer when done.
212204
* @param in the String to copy from
213205
* @param out the Writer to copy to
214206
* @throws IOException in case of I/O errors
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.FilterInputStream;
21+
import java.io.FilterOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.io.OutputStream;
26+
import java.io.OutputStreamWriter;
27+
import java.io.Writer;
28+
import java.nio.charset.Charset;
29+
30+
31+
/**
32+
* Simple utility methods for dealing with streams. The copy methods of this class are
33+
* similar to those defined in {@link FileCopyUtils} except that all affected streams are
34+
* left open when done. All copy methods use a block size of 4096 bytes.
35+
*
36+
* <p>Mainly for use within the framework, but also useful for application code.
37+
*
38+
* @author Juergen Hoeller
39+
* @author Phillip Webb
40+
* @since 3.2
41+
* @see FileCopyUtils
42+
*/
43+
public abstract class StreamUtils {
44+
45+
public static final int BUFFER_SIZE = 4096;
46+
47+
48+
/**
49+
* Copy the contents of the given InputStream into a new byte array.
50+
* Leaves the stream open when done.
51+
* @param in the stream to copy from
52+
* @return the new byte array that has been copied to
53+
* @throws IOException in case of I/O errors
54+
*/
55+
public static byte[] copyToByteArray(InputStream in) throws IOException {
56+
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
57+
copy(in, out);
58+
return out.toByteArray();
59+
}
60+
61+
/**
62+
* Copy the contents of the given InputStream into a String.
63+
* Leaves the stream open when done.
64+
* @param in the InputStream to copy from
65+
* @return the String that has been copied to
66+
* @throws IOException in case of I/O errors
67+
*/
68+
public static String copyToString(InputStream in, Charset charset) throws IOException {
69+
Assert.notNull(in, "No InputStream specified");
70+
StringBuilder out = new StringBuilder();
71+
InputStreamReader reader = new InputStreamReader(in, charset);
72+
char[] buffer = new char[BUFFER_SIZE];
73+
int bytesRead = -1;
74+
while ((bytesRead = reader.read(buffer)) != -1) {
75+
out.append(buffer, 0, bytesRead);
76+
}
77+
return out.toString();
78+
}
79+
80+
/**
81+
* Copy the contents of the given byte array to the given OutputStream.
82+
* Leaves the stream open when done.
83+
* @param in the byte array to copy from
84+
* @param out the OutputStream to copy to
85+
* @throws IOException in case of I/O errors
86+
*/
87+
public static void copy(byte[] in, OutputStream out) throws IOException {
88+
Assert.notNull(in, "No input byte array specified");
89+
Assert.notNull(out, "No OutputStream specified");
90+
out.write(in);
91+
}
92+
93+
/**
94+
* Copy the contents of the given String to the given output OutputStream.
95+
* Leaves the stream open when done.
96+
* @param in the String to copy from
97+
* @param charset the Charset
98+
* @param out the OutputStream to copy to
99+
* @throws IOException in case of I/O errors
100+
*/
101+
public static void copy(String in, Charset charset, OutputStream out) throws IOException {
102+
Assert.notNull(in, "No input String specified");
103+
Assert.notNull(charset, "No charset specified");
104+
Assert.notNull(out, "No OutputStream specified");
105+
Writer writer = new OutputStreamWriter(out, charset);
106+
writer.write(in);
107+
writer.flush();
108+
}
109+
110+
/**
111+
* Copy the contents of the given InputStream to the given OutputStream.
112+
* Leaves both streams open when done.
113+
* @param in the InputStream to copy from
114+
* @param out the OutputStream to copy to
115+
* @return the number of bytes copied
116+
* @throws IOException in case of I/O errors
117+
*/
118+
public static int copy(InputStream in, OutputStream out) throws IOException {
119+
Assert.notNull(in, "No InputStream specified");
120+
Assert.notNull(out, "No OutputStream specified");
121+
int byteCount = 0;
122+
byte[] buffer = new byte[BUFFER_SIZE];
123+
int bytesRead = -1;
124+
while ((bytesRead = in.read(buffer)) != -1) {
125+
out.write(buffer, 0, bytesRead);
126+
byteCount += bytesRead;
127+
}
128+
out.flush();
129+
return byteCount;
130+
}
131+
132+
/**
133+
* Returns a variant of the given {@link InputStream} where calling
134+
* {@link InputStream#close() close()} has no effect.
135+
* @param in the InputStream to decorate
136+
* @return a version of the InputStream that ignores calls to close
137+
*/
138+
public static InputStream nonClosing(InputStream in) {
139+
Assert.notNull(in, "No InputStream specified");
140+
return new NonClosingInputStream(in);
141+
}
142+
143+
/**
144+
* Returns a variant of the given {@link OutputStream} where calling
145+
* {@link OutputStream#close() close()} has no effect.
146+
* @param in the OutputStream to decorate
147+
* @return a version of the OutputStream that ignores calls to close
148+
*/
149+
public static OutputStream nonClosing(OutputStream out) {
150+
Assert.notNull(out, "No OutputStream specified");
151+
return new NonClosingOutputStream(out);
152+
}
153+
154+
155+
private static class NonClosingInputStream extends FilterInputStream {
156+
157+
public NonClosingInputStream(InputStream in) {
158+
super(in);
159+
}
160+
161+
@Override
162+
public void close() throws IOException {
163+
}
164+
}
165+
166+
167+
private static class NonClosingOutputStream extends FilterOutputStream {
168+
169+
public NonClosingOutputStream(OutputStream out) {
170+
super(out);
171+
}
172+
173+
@Override
174+
public void write(byte[] b, int off, int let) throws IOException {
175+
// It is critical that we override this method for performance
176+
out.write(b, off, let);
177+
}
178+
179+
@Override
180+
public void close() throws IOException {
181+
}
182+
}
183+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.junit.Assert.assertThat;
21+
import static org.mockito.Mockito.inOrder;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.never;
24+
import static org.mockito.Mockito.spy;
25+
import static org.mockito.Mockito.verify;
26+
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.InputStream;
30+
import java.io.OutputStream;
31+
import java.nio.charset.Charset;
32+
import java.util.Random;
33+
import java.util.UUID;
34+
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.mockito.InOrder;
38+
39+
/**
40+
* Tests for {@link StreamUtils}.
41+
*
42+
* @author Phillip Webb
43+
*/
44+
public class StreamUtilsTests {
45+
46+
private byte[] bytes = new byte[StreamUtils.BUFFER_SIZE + 10];
47+
48+
private String string = "";
49+
50+
@Before
51+
public void setup() {
52+
new Random().nextBytes(bytes);
53+
while (string.length() < StreamUtils.BUFFER_SIZE + 10) {
54+
string += UUID.randomUUID().toString();
55+
}
56+
}
57+
58+
@Test
59+
public void copyToByteArray() throws Exception {
60+
InputStream inputStream = spy(new ByteArrayInputStream(bytes));
61+
byte[] actual = StreamUtils.copyToByteArray(inputStream);
62+
assertThat(actual, equalTo(bytes));
63+
verify(inputStream, never()).close();
64+
}
65+
66+
@Test
67+
public void copyToString() throws Exception {
68+
Charset charset = Charset.defaultCharset();
69+
InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset)));
70+
String actual = StreamUtils.copyToString(inputStream, charset);
71+
assertThat(actual, equalTo(string));
72+
verify(inputStream, never()).close();
73+
}
74+
75+
@Test
76+
public void copyBytes() throws Exception {
77+
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
78+
StreamUtils.copy(bytes, out);
79+
assertThat(out.toByteArray(), equalTo(bytes));
80+
verify(out, never()).close();
81+
}
82+
83+
@Test
84+
public void copyString() throws Exception {
85+
Charset charset = Charset.defaultCharset();
86+
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
87+
StreamUtils.copy(string, charset, out);
88+
assertThat(out.toByteArray(), equalTo(string.getBytes(charset)));
89+
verify(out, never()).close();
90+
}
91+
92+
@Test
93+
public void copyStream() throws Exception {
94+
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
95+
StreamUtils.copy(new ByteArrayInputStream(bytes), out);
96+
assertThat(out.toByteArray(), equalTo(bytes));
97+
verify(out, never()).close();
98+
}
99+
100+
@Test
101+
public void nonClosingInputStream() throws Exception {
102+
InputStream source = mock(InputStream.class);
103+
InputStream nonClosing = StreamUtils.nonClosing(source);
104+
nonClosing.read();
105+
nonClosing.read(bytes);
106+
nonClosing.read(bytes, 1, 2);
107+
nonClosing.close();
108+
InOrder ordered = inOrder(source);
109+
ordered.verify(source).read();
110+
ordered.verify(source).read(bytes, 0, bytes.length);
111+
ordered.verify(source).read(bytes, 1, 2);
112+
ordered.verify(source, never()).close();
113+
}
114+
115+
@Test
116+
public void nonClosingOutputStream() throws Exception {
117+
OutputStream source = mock(OutputStream.class);
118+
OutputStream nonClosing = StreamUtils.nonClosing(source);
119+
nonClosing.write(1);
120+
nonClosing.write(bytes);
121+
nonClosing.write(bytes, 1, 2);
122+
nonClosing.close();
123+
InOrder ordered = inOrder(source);
124+
ordered.verify(source).write(1);
125+
ordered.verify(source).write(bytes, 0, bytes.length);
126+
ordered.verify(source).write(bytes, 1, 2);
127+
ordered.verify(source, never()).close();
128+
}
129+
}

spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
import org.springframework.http.HttpHeaders;
2424
import org.springframework.http.HttpMethod;
2525
import org.springframework.util.Assert;
26-
import org.springframework.util.FileCopyUtils;
26+
import org.springframework.util.StreamUtils;
2727

2828
/**
2929
* Simple implementation of {@link ClientHttpRequest} that wraps another request.
@@ -53,8 +53,7 @@ public URI getURI() {
5353
@Override
5454
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
5555
this.request.getHeaders().putAll(headers);
56-
OutputStream body = this.request.getBody();
57-
FileCopyUtils.copy(bufferedOutput, body);
56+
StreamUtils.copy(bufferedOutput, this.request.getBody());
5857
ClientHttpResponse response = this.request.execute();
5958
return new BufferingClientHttpResponseWrapper(response);
6059
}

0 commit comments

Comments
 (0)