1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
38
38
* Tests for {@link StreamUtils}.
39
39
*
40
40
* @author Phillip Webb
41
+ * @author Juergen Hoeller
41
42
*/
42
43
class StreamUtilsTests {
43
44
44
45
private byte [] bytes = new byte [StreamUtils .BUFFER_SIZE + 10 ];
45
46
46
47
private String string = "" ;
47
48
49
+
48
50
@ BeforeEach
49
51
void setup () {
50
52
new Random ().nextBytes (bytes );
@@ -53,6 +55,7 @@ void setup() {
53
55
}
54
56
}
55
57
58
+
56
59
@ Test
57
60
void copyToByteArray () throws Exception {
58
61
InputStream inputStream = new ByteArrayInputStream (bytes );
@@ -91,11 +94,30 @@ void copyStream() throws Exception {
91
94
}
92
95
93
96
@ Test
94
- void copyRange () throws Exception {
97
+ void copyRangeWithinBuffer () throws Exception {
98
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
99
+ ByteArrayInputStream in = new ByteArrayInputStream (bytes );
100
+ StreamUtils .copyRange (in , out , 0 , 100 );
101
+ assertThat (in .available ()).isEqualTo (bytes .length - 101 );
102
+ assertThat (out .toByteArray ()).isEqualTo (Arrays .copyOfRange (bytes , 0 , 101 ));
103
+ }
104
+
105
+ @ Test
106
+ void copyRangeBeyondBuffer () throws Exception {
95
107
ByteArrayOutputStream out = new ByteArrayOutputStream ();
96
- StreamUtils .copyRange (new ByteArrayInputStream (bytes ), out , 0 , 100 );
97
- byte [] range = Arrays .copyOfRange (bytes , 0 , 101 );
98
- assertThat (out .toByteArray ()).isEqualTo (range );
108
+ ByteArrayInputStream in = new ByteArrayInputStream (bytes );
109
+ StreamUtils .copyRange (in , out , 0 , 8200 );
110
+ assertThat (in .available ()).isEqualTo (1 );
111
+ assertThat (out .toByteArray ()).isEqualTo (Arrays .copyOfRange (bytes , 0 , 8201 ));
112
+ }
113
+
114
+ @ Test
115
+ void copyRangeBeyondAvailable () throws Exception {
116
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
117
+ ByteArrayInputStream in = new ByteArrayInputStream (bytes );
118
+ StreamUtils .copyRange (in , out , 0 , 8300 );
119
+ assertThat (in .available ()).isEqualTo (0 );
120
+ assertThat (out .toByteArray ()).isEqualTo (Arrays .copyOfRange (bytes , 0 , 8202 ));
99
121
}
100
122
101
123
@ Test
@@ -127,4 +149,5 @@ void nonClosingOutputStream() throws Exception {
127
149
ordered .verify (source ).write (bytes , 1 , 2 );
128
150
ordered .verify (source , never ()).close ();
129
151
}
152
+
130
153
}
0 commit comments