1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
+
16
+ package software .amazon .awssdk .core .internal .io ;
17
+
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .mockito .ArgumentMatchers .any ;
20
+ import static org .mockito .Mockito .mock ;
21
+ import static org .mockito .Mockito .verify ;
22
+ import static org .mockito .Mockito .when ;
23
+
24
+ import java .io .ByteArrayInputStream ;
25
+ import java .io .IOException ;
26
+ import java .io .InputStream ;
27
+ import org .junit .jupiter .api .BeforeEach ;
28
+ import org .junit .jupiter .api .Test ;
29
+
30
+ class SdkLengthAwareInputStreamTest {
31
+ private InputStream delegateStream ;
32
+
33
+ @ BeforeEach
34
+ void setup () {
35
+ delegateStream = mock (InputStream .class );
36
+ }
37
+
38
+ @ Test
39
+ void read_lengthIs0_returnsEof () throws IOException {
40
+ when (delegateStream .available ()).thenReturn (Integer .MAX_VALUE );
41
+
42
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 0 );
43
+
44
+ assertThat (is .read ()).isEqualTo (-1 );
45
+ assertThat (is .read (new byte [16 ], 0 , 16 )).isEqualTo (-1 );
46
+ }
47
+
48
+ @ Test
49
+ void read_lengthNonZero_delegateEof_returnsEof () throws IOException {
50
+ when (delegateStream .read ()).thenReturn (-1 );
51
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenReturn (-1 );
52
+
53
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 0 );
54
+
55
+ assertThat (is .read ()).isEqualTo (-1 );
56
+ assertThat (is .read (new byte [16 ], 0 , 16 )).isEqualTo (-1 );
57
+ }
58
+
59
+ @ Test
60
+ void readByte_lengthNonZero_delegateHasAvailable_returnsDelegateData () throws IOException {
61
+ when (delegateStream .read ()).thenReturn (42 );
62
+
63
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
64
+
65
+ assertThat (is .read ()).isEqualTo (42 );
66
+ }
67
+
68
+ @ Test
69
+ void readArray_lengthNonZero_delegateHasAvailable_returnsDelegateData () throws IOException {
70
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenReturn (8 );
71
+
72
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
73
+
74
+ assertThat (is .read (new byte [16 ], 0 , 16 )).isEqualTo (8 );
75
+ }
76
+
77
+ @ Test
78
+ void readArray_lengthNonZero_propagatesCallToDelegate () throws IOException {
79
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenReturn (8 );
80
+
81
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
82
+ byte [] buff = new byte [16 ];
83
+ is .read (buff , 0 , 16 );
84
+
85
+ verify (delegateStream ).read (buff , 0 , 16 );
86
+ }
87
+
88
+ @ Test
89
+ void read_markAndReset_availableReflectsNewLength () throws IOException {
90
+ delegateStream = new ByteArrayInputStream (new byte [32 ]);
91
+
92
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
93
+
94
+ for (int i = 0 ; i < 4 ; ++i ) {
95
+ is .read ();
96
+ }
97
+ assertThat (is .available ()).isEqualTo (12 );
98
+
99
+ is .mark (16 );
100
+
101
+ for (int i = 0 ; i < 4 ; ++i ) {
102
+ is .read ();
103
+ }
104
+ assertThat (is .available ()).isEqualTo (8 );
105
+
106
+ is .reset ();
107
+
108
+ assertThat (is .available ()).isEqualTo (12 );
109
+ }
110
+
111
+ @ Test
112
+ void skip_markAndReset_availableReflectsNewLength () throws IOException {
113
+ delegateStream = new ByteArrayInputStream (new byte [32 ]);
114
+
115
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
116
+
117
+ is .skip (4 );
118
+
119
+ assertThat (is .remaining ()).isEqualTo (12 );
120
+
121
+ is .mark (16 );
122
+
123
+ for (int i = 0 ; i < 4 ; ++i ) {
124
+ is .read ();
125
+ }
126
+
127
+ assertThat (is .remaining ()).isEqualTo (8 );
128
+
129
+ is .reset ();
130
+
131
+ assertThat (is .remaining ()).isEqualTo (12 );
132
+ }
133
+
134
+ @ Test
135
+ void skip_delegateSkipsLessThanRequested_availableUpdatedCorrectly () throws IOException {
136
+ when (delegateStream .skip (any (long .class ))).thenAnswer (i -> {
137
+ Long n = i .getArgument (0 , Long .class );
138
+ return n / 2 ;
139
+ });
140
+
141
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenReturn (1 );
142
+
143
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
144
+
145
+ long skipped = is .skip (4 );
146
+
147
+ assertThat (skipped ).isEqualTo (2 );
148
+ assertThat (is .remaining ()).isEqualTo (14 );
149
+ }
150
+
151
+ @ Test
152
+ void readArray_delegateReadsLessThanRequested_availableUpdatedCorrectly () throws IOException {
153
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenAnswer (i -> {
154
+ Integer n = i .getArgument (2 , Integer .class );
155
+ return n / 2 ;
156
+ });
157
+
158
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
159
+
160
+ long read = is .read (new byte [16 ], 0 , 8 );
161
+
162
+ assertThat (read ).isEqualTo (4 );
163
+ assertThat (is .remaining ()).isEqualTo (12 );
164
+ }
165
+
166
+ @ Test
167
+ void read_delegateAtEof_returnsEof () throws IOException {
168
+ when (delegateStream .read ()).thenReturn (-1 );
169
+
170
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
171
+
172
+ assertThat (is .read ()).isEqualTo (-1 );
173
+ }
174
+
175
+ @ Test
176
+ void readArray_delegateAtEof_returnsEof () throws IOException {
177
+ when (delegateStream .read (any (byte [].class ), any (int .class ), any (int .class ))).thenReturn (-1 );
178
+
179
+ SdkLengthAwareInputStream is = new SdkLengthAwareInputStream (delegateStream , 16 );
180
+
181
+ assertThat (is .read (new byte [8 ], 0 , 8 )).isEqualTo (-1 );
182
+ }
183
+ }
0 commit comments