1
1
/*
2
- * Copyright (c) 2015, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
31
31
32
32
/*
33
33
* @test
34
- * @bug 8080835 8139206
34
+ * @bug 8080835 8139206 8254742
35
35
* @library /test/lib
36
36
* @build jdk.test.lib.RandomFactory
37
37
* @run main ReadNBytes
@@ -44,6 +44,7 @@ public class ReadNBytes {
44
44
private static Random generator = RandomFactory .getRandom ();
45
45
46
46
public static void main (String [] args ) throws IOException {
47
+ test ();
47
48
test (new byte []{1 , 2 , 3 });
48
49
test (createRandomBytes (1024 ));
49
50
for (int shift : new int [] {13 , 15 , 17 }) {
@@ -135,6 +136,19 @@ static void test(int max) throws IOException {
135
136
check (!in .isClosed (), "Stream unexpectedly closed" );
136
137
}
137
138
139
+ static void test () throws IOException {
140
+ final int chunkSize = 8192 ;
141
+ int size = (10 + generator .nextInt (11 ))*chunkSize ;
142
+
143
+ byte [] buf = new byte [size ];
144
+ generator .nextBytes (buf );
145
+ InputStream s = new ThrottledByteArrayInputStream (buf );
146
+
147
+ byte [] b = s .readNBytes (size );
148
+
149
+ check (Arrays .equals (b , buf ), "Arrays not equal" );
150
+ }
151
+
138
152
static byte [] createRandomBytes (int size ) {
139
153
byte [] bytes = new byte [size ];
140
154
generator .nextBytes (bytes );
@@ -150,11 +164,32 @@ static void check(boolean cond, Object ... failedArgs) {
150
164
throw new RuntimeException (sb .toString ());
151
165
}
152
166
153
-
154
167
static class WrapperInputStream extends FilterInputStream {
155
168
private boolean closed ;
156
169
WrapperInputStream (InputStream in ) { super (in ); }
157
170
@ Override public void close () throws IOException { closed = true ; in .close (); }
158
171
boolean isClosed () { return closed ; }
159
172
}
173
+
174
+ static class ThrottledByteArrayInputStream extends ByteArrayInputStream {
175
+ private int count = 0 ;
176
+
177
+ ThrottledByteArrayInputStream (byte [] buf ) {
178
+ super (buf );
179
+ }
180
+
181
+ @ Override
182
+ //
183
+ // Sometimes return zero or a smaller count than requested.
184
+ //
185
+ public int read (byte [] buf , int off , int len ) {
186
+ if (generator .nextBoolean ()) {
187
+ return 0 ;
188
+ } else if (++count / 3 == 0 ) {
189
+ len /= 3 ;
190
+ }
191
+
192
+ return super .read (buf , off , len );
193
+ }
194
+ }
160
195
}
0 commit comments