duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998 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 BufferedReader's read method will fill the target array |
| 27 | whenever possible |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.io.Reader; |
| 33 | import java.io.BufferedReader; |
| 34 | |
| 35 | |
| 36 | public class Fill { |
| 37 | |
| 38 | /** |
| 39 | * A simple Reader that is always ready but may read fewer than the |
| 40 | * requested number of characters |
| 41 | */ |
| 42 | static class Source extends Reader { |
| 43 | |
| 44 | int shortFall; |
| 45 | char next = 0; |
| 46 | |
| 47 | Source(int shortFall) { |
| 48 | this.shortFall = shortFall; |
| 49 | } |
| 50 | |
| 51 | public int read(char[] cbuf, int off, int len) throws IOException { |
| 52 | int n = len - shortFall; |
| 53 | for (int i = off; i < n; i++) |
| 54 | cbuf[i] = next++; |
| 55 | return n; |
| 56 | } |
| 57 | |
| 58 | public boolean ready() { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | public void close() throws IOException { |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test BufferedReader with an underlying source that always reads |
| 69 | * shortFall fewer characters than requested |
| 70 | */ |
| 71 | static void go(int shortFall) throws Exception { |
| 72 | |
| 73 | Reader r = new BufferedReader(new Source(shortFall), 10); |
| 74 | char[] cbuf = new char[8]; |
| 75 | |
| 76 | int n1 = r.read(cbuf); |
| 77 | int n2 = r.read(cbuf); |
| 78 | System.err.println("Shortfall " + shortFall |
| 79 | + ": Read " + n1 + ", then " + n2 + " chars"); |
| 80 | if (n1 != cbuf.length) |
| 81 | throw new Exception("First read returned " + n1); |
| 82 | if (n2 != cbuf.length) |
| 83 | throw new Exception("Second read returned " + n2); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | public static void main(String[] args) throws Exception { |
| 88 | for (int i = 0; i < 8; i++) go(i); |
| 89 | } |
| 90 | |
| 91 | } |