J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997 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 | @summary General tests for BufferedReader.mark |
| 26 | */ |
| 27 | |
| 28 | import java.io.*; |
| 29 | |
| 30 | |
| 31 | public class BufferedReaderMark { |
| 32 | |
| 33 | static int bufferSize = -1; |
| 34 | static int inputSize = -1; |
| 35 | |
| 36 | static String d2(int n) { |
| 37 | String s = Integer.toString(n); |
| 38 | if (s.length() < 2) |
| 39 | return s + " "; |
| 40 | else |
| 41 | return s; |
| 42 | } |
| 43 | |
| 44 | static void fail(String s) { |
| 45 | throw new RuntimeException(s); |
| 46 | } |
| 47 | |
| 48 | public static void main(String[] args) throws Exception { |
| 49 | PrintStream log = System.err; |
| 50 | |
| 51 | if (bufferSize == -1) |
| 52 | bufferSize = 7; |
| 53 | if (inputSize == -1) |
| 54 | inputSize = bufferSize * 3; |
| 55 | |
| 56 | try { |
| 57 | Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize); |
| 58 | char buf[] = new char[bufferSize]; |
| 59 | if (in.read(buf) != bufferSize) |
| 60 | fail("Read failed"); |
| 61 | in.reset(); |
| 62 | fail("reset() didn't throw"); |
| 63 | } |
| 64 | catch (IOException x) { |
| 65 | if (x.getMessage().equals("Stream not marked")) |
| 66 | log.println("Unmarked-stream test OK"); |
| 67 | else |
| 68 | fail(x.toString()); |
| 69 | } |
| 70 | |
| 71 | boolean err = false; |
| 72 | try { |
| 73 | Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize); |
| 74 | char buf[] = new char[bufferSize]; |
| 75 | if (in.read() == -1) |
| 76 | fail("Read 1 failed"); |
| 77 | in.mark(bufferSize); |
| 78 | while (in.read() != -1); |
| 79 | in.reset(); |
| 80 | fail("reset() didn't throw"); |
| 81 | } |
| 82 | catch (IOException x) { |
| 83 | if (x.getMessage().equals("Mark invalid")) { |
| 84 | err = true; |
| 85 | log.println("Invalid-mark test OK"); |
| 86 | } |
| 87 | else { |
| 88 | log.println("Invalid-mark test failed: " + x); |
| 89 | fail(x.toString()); |
| 90 | } |
| 91 | } |
| 92 | if (! err) |
| 93 | fail("Invalid-mark test failed: Exception not thrown"); |
| 94 | |
| 95 | int c; |
| 96 | for (int off = 0; off < bufferSize * 2; off++) { |
| 97 | for (int ra = 0; ra <= inputSize + 2; ra++) { |
| 98 | Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize); |
| 99 | |
| 100 | log.print(d2(off) + ", " + d2(ra) + " mark: "); |
| 101 | for (int i = 0; i < off; i++) { |
| 102 | if ((c = in.read()) == -1) { |
| 103 | log.print("<EOF>"); |
| 104 | break; |
| 105 | } |
| 106 | log.print((char) c); |
| 107 | } |
| 108 | in.mark(ra); |
| 109 | for (int i = 0; i < ra; i++) { |
| 110 | if ((c = in.read()) == -1) { |
| 111 | log.print("<EOF>"); |
| 112 | break; |
| 113 | } |
| 114 | log.print((char) c); |
| 115 | } |
| 116 | log.println(); |
| 117 | |
| 118 | log.print(d2(off) + ", " + d2(ra) + " reset: "); |
| 119 | in.reset(); |
| 120 | for (int i = 0; i < off; i++) |
| 121 | log.print(' '); |
| 122 | for (;;) { |
| 123 | if ((c = in.read()) == -1) { |
| 124 | log.print("<EOF>"); |
| 125 | break; |
| 126 | } |
| 127 | log.print((char) c); |
| 128 | } |
| 129 | log.println(); |
| 130 | |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |