2
|
1 |
/*
|
|
2 |
* Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4090383
|
|
26 |
@summary Ensure that BufferedInputStream's read method will fill the target
|
|
27 |
array whenever possible
|
|
28 |
*/
|
|
29 |
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.BufferedInputStream;
|
|
34 |
|
|
35 |
|
|
36 |
public class Fill {
|
|
37 |
|
|
38 |
/**
|
|
39 |
* A simple InputStream that is always ready but may read fewer than the
|
|
40 |
* requested number of bytes
|
|
41 |
*/
|
|
42 |
static class Source extends InputStream {
|
|
43 |
|
|
44 |
int shortFall;
|
|
45 |
byte next = 0;
|
|
46 |
|
|
47 |
Source(int shortFall) {
|
|
48 |
this.shortFall = shortFall;
|
|
49 |
}
|
|
50 |
|
|
51 |
public int read() throws IOException {
|
|
52 |
return next++;
|
|
53 |
}
|
|
54 |
|
|
55 |
public int read(byte[] buf, int off, int len) throws IOException {
|
|
56 |
int n = len - shortFall;
|
|
57 |
for (int i = off; i < n; i++)
|
|
58 |
buf[i] = next++;
|
|
59 |
return n;
|
|
60 |
}
|
|
61 |
|
|
62 |
public int available() {
|
|
63 |
return Integer.MAX_VALUE;
|
|
64 |
}
|
|
65 |
|
|
66 |
public void close() throws IOException {
|
|
67 |
}
|
|
68 |
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Test BufferedInputStream with an underlying source that always reads
|
|
73 |
* shortFall fewer bytes than requested
|
|
74 |
*/
|
|
75 |
static void go(int shortFall) throws Exception {
|
|
76 |
|
|
77 |
InputStream r = new BufferedInputStream(new Source(shortFall), 10);
|
|
78 |
byte[] buf = new byte[8];
|
|
79 |
|
|
80 |
int n1 = r.read(buf);
|
|
81 |
int n2 = r.read(buf);
|
|
82 |
System.err.println("Shortfall " + shortFall
|
|
83 |
+ ": Read " + n1 + ", then " + n2 + " bytes");
|
|
84 |
if (n1 != buf.length)
|
|
85 |
throw new Exception("First read returned " + n1);
|
|
86 |
if (n2 != buf.length)
|
|
87 |
throw new Exception("Second read returned " + n2);
|
|
88 |
|
|
89 |
}
|
|
90 |
|
|
91 |
public static void main(String[] args) throws Exception {
|
|
92 |
for (int i = 0; i < 8; i++) go(i);
|
|
93 |
}
|
|
94 |
|
|
95 |
}
|