File tree Expand file tree Collapse file tree 2 files changed +9
-13
lines changed
spring-core/src/main/java/org/springframework/util
spring-web/src/test/java/org/springframework/http/client Expand file tree Collapse file tree 2 files changed +9
-13
lines changed Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .util ;
18
18
19
- import java .io .ByteArrayInputStream ;
20
19
import java .io .ByteArrayOutputStream ;
21
20
import java .io .FilterInputStream ;
22
21
import java .io .FilterOutputStream ;
@@ -62,7 +61,7 @@ public abstract class StreamUtils {
62
61
*/
63
62
public static byte [] copyToByteArray (@ Nullable InputStream in ) throws IOException {
64
63
if (in == null ) {
65
- return new byte [ 0 ] ;
64
+ return EMPTY_CONTENT ;
66
65
}
67
66
68
67
ByteArrayOutputStream out = new ByteArrayOutputStream (BUFFER_SIZE );
@@ -215,22 +214,15 @@ else if (bytesRead <= bytesToCopy) {
215
214
*/
216
215
public static int drain (InputStream in ) throws IOException {
217
216
Assert .notNull (in , "No InputStream specified" );
218
- byte [] buffer = new byte [BUFFER_SIZE ];
219
- int bytesRead = -1 ;
220
- int byteCount = 0 ;
221
- while ((bytesRead = in .read (buffer )) != -1 ) {
222
- byteCount += bytesRead ;
223
- }
224
- return byteCount ;
217
+ return (int ) in .transferTo (OutputStream .nullOutputStream ());
225
218
}
226
-
227
219
/**
228
220
* Return an efficient empty {@link InputStream}.
229
- * @return a {@link ByteArrayInputStream} based on an empty byte array
221
+ * @return an InputStream which contains no bytes
230
222
* @since 4.2.2
231
223
*/
232
224
public static InputStream emptyInput () {
233
- return new ByteArrayInputStream ( EMPTY_CONTENT );
225
+ return InputStream . nullInputStream ( );
234
226
}
235
227
236
228
/**
Original file line number Diff line number Diff line change 28
28
29
29
import static org .assertj .core .api .Assertions .assertThat ;
30
30
import static org .mockito .ArgumentMatchers .any ;
31
+ import static org .mockito .ArgumentMatchers .anyInt ;
31
32
import static org .mockito .BDDMockito .given ;
32
33
import static org .mockito .BDDMockito .willDoNothing ;
33
34
import static org .mockito .Mockito .mock ;
@@ -99,7 +100,10 @@ public void shouldNotDrainWhenErrorStreamClosed() throws Exception {
99
100
InputStream is = mock (InputStream .class );
100
101
given (this .connection .getErrorStream ()).willReturn (is );
101
102
willDoNothing ().given (is ).close ();
102
- given (is .read (any ())).willThrow (new NullPointerException ("from HttpURLConnection#ErrorStream" ));
103
+ given (is .transferTo (any ())).willCallRealMethod ();
104
+ given (is .read (any (), anyInt (), anyInt ())).willThrow (new NullPointerException ("from HttpURLConnection#ErrorStream" ));
105
+
106
+ is .readAllBytes ();
103
107
104
108
InputStream responseStream = this .response .getBody ();
105
109
responseStream .close ();
You can’t perform that action at this time.
0 commit comments