1 |
14 |
jlechner |
/*************************************************************************
|
2 |
|
|
/* BufferedReaderTest.java -- Tests BufferedReader's
|
3 |
|
|
/*
|
4 |
|
|
/* Copyright (c) 1998 Free Software Foundation, Inc.
|
5 |
|
|
/* Written by Aaron M. Renn (arenn@urbanophile.com)
|
6 |
|
|
/*
|
7 |
|
|
/* This program is free software; you can redistribute it and/or modify
|
8 |
|
|
/* it under the terms of the GNU General Public License as published
|
9 |
|
|
/* by the Free Software Foundation, either version 2 of the License, or
|
10 |
|
|
/* (at your option) any later version.
|
11 |
|
|
/*
|
12 |
|
|
/* This program is distributed in the hope that it will be useful, but
|
13 |
|
|
/* WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
|
|
/* GNU General Public License for more details.
|
16 |
|
|
/*
|
17 |
|
|
/* You should have received a copy of the GNU General Public License
|
18 |
|
|
/* along with this program; if not, write to the Free Software Foundation
|
19 |
|
|
/* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 |
|
|
/*************************************************************************/
|
21 |
|
|
|
22 |
|
|
import java.io.*;
|
23 |
|
|
|
24 |
|
|
public class BufferedReaderTest extends CharArrayReader
|
25 |
|
|
{
|
26 |
|
|
|
27 |
|
|
// Hehe. We override CharArrayReader.markSupported() in order to return
|
28 |
|
|
// false so that we can test BufferedReader's handling of mark/reset in
|
29 |
|
|
// both the case where the underlying stream does and does not support
|
30 |
|
|
// mark/reset
|
31 |
|
|
public boolean
|
32 |
|
|
markSupported()
|
33 |
|
|
{
|
34 |
|
|
return(false);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
public
|
38 |
|
|
BufferedReaderTest(char[] buf)
|
39 |
|
|
{
|
40 |
|
|
super(buf);
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
public static int
|
44 |
|
|
marktest(Reader ins) throws IOException
|
45 |
|
|
{
|
46 |
|
|
BufferedReader br = new BufferedReader(ins, 15);
|
47 |
|
|
|
48 |
|
|
int chars_read;
|
49 |
|
|
int total_read = 0;
|
50 |
|
|
char[] buf = new char[12];
|
51 |
|
|
|
52 |
|
|
chars_read = br.read(buf);
|
53 |
|
|
total_read += chars_read;
|
54 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
55 |
|
|
|
56 |
|
|
chars_read = br.read(buf);
|
57 |
|
|
total_read += chars_read;
|
58 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
59 |
|
|
|
60 |
|
|
br.mark(75);
|
61 |
|
|
br.read();
|
62 |
|
|
br.read(buf);
|
63 |
|
|
br.read(buf);
|
64 |
|
|
br.read(buf);
|
65 |
|
|
br.reset();
|
66 |
|
|
|
67 |
|
|
chars_read = br.read(buf);
|
68 |
|
|
total_read += chars_read;
|
69 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
70 |
|
|
|
71 |
|
|
br.mark(555);
|
72 |
|
|
|
73 |
|
|
chars_read = br.read(buf);
|
74 |
|
|
total_read += chars_read;
|
75 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
76 |
|
|
|
77 |
|
|
br.reset();
|
78 |
|
|
|
79 |
|
|
br.read(buf);
|
80 |
|
|
chars_read = br.read(buf);
|
81 |
|
|
total_read += chars_read;
|
82 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
83 |
|
|
|
84 |
|
|
chars_read = br.read(buf);
|
85 |
|
|
total_read += chars_read;
|
86 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
87 |
|
|
|
88 |
|
|
br.mark(14);
|
89 |
|
|
|
90 |
|
|
br.read(buf);
|
91 |
|
|
|
92 |
|
|
br.reset();
|
93 |
|
|
|
94 |
|
|
chars_read = br.read(buf);
|
95 |
|
|
total_read += chars_read;
|
96 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
97 |
|
|
|
98 |
|
|
while ((chars_read = br.read(buf)) != -1)
|
99 |
|
|
{
|
100 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
101 |
|
|
total_read += chars_read;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
return(total_read);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
public static void
|
108 |
|
|
main(String[] argv)
|
109 |
|
|
{
|
110 |
|
|
System.out.println("Started test of BufferedReader");
|
111 |
|
|
|
112 |
|
|
try
|
113 |
|
|
{
|
114 |
|
|
System.out.println("Test 1: Simple Read Test");
|
115 |
|
|
|
116 |
|
|
String str = "My 5th grade teacher was named Mr. Thompson. Terry\n" +
|
117 |
|
|
"George Thompson to be precise. He had these sideburns like\n" +
|
118 |
|
|
"Isaac Asimov's, only uglier. One time he had a contest and said\n" +
|
119 |
|
|
"that if any kid who could lift 50lbs worth of weights on a barbell\n" +
|
120 |
|
|
"all the way over their head, he would shave it off. Nobody could\n" +
|
121 |
|
|
"though. One time I guess I made a comment about how stupid his\n" +
|
122 |
|
|
"sideburns worked and he not only kicked me out of class, he called\n" +
|
123 |
|
|
"my mother. Jerk.\n";
|
124 |
|
|
|
125 |
|
|
StringReader sr = new StringReader(str);
|
126 |
|
|
BufferedReader br = new BufferedReader(sr, 15);
|
127 |
|
|
|
128 |
|
|
char[] buf = new char[12];
|
129 |
|
|
int chars_read;
|
130 |
|
|
while((chars_read = br.read(buf)) != -1)
|
131 |
|
|
System.out.print(new String(buf, 0, chars_read));
|
132 |
|
|
|
133 |
|
|
br.close();
|
134 |
|
|
System.out.println("PASSED: Simple Read Test");
|
135 |
|
|
}
|
136 |
|
|
catch (IOException e)
|
137 |
|
|
{
|
138 |
|
|
System.out.println("FAILED: Simple Read Test: " + e);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
try
|
142 |
|
|
{
|
143 |
|
|
System.out.println("Test 2: First mark/reset Test");
|
144 |
|
|
|
145 |
|
|
String str = "Growing up in a rural area brings such delights. One\n" +
|
146 |
|
|
"time my uncle called me up and asked me to come over and help him\n" +
|
147 |
|
|
"out with something. Since he lived right across the field, I\n" +
|
148 |
|
|
"walked right over. Turned out he wanted me to come down to the\n" +
|
149 |
|
|
"barn and help him castrate a calf. Oh, that was fun. Not.\n";
|
150 |
|
|
|
151 |
|
|
StringReader sr = new StringReader(str);
|
152 |
|
|
// BufferedReader br = new BufferedReader(sr);
|
153 |
|
|
|
154 |
|
|
int total_read = marktest(sr);
|
155 |
|
|
|
156 |
|
|
if (total_read == str.length())
|
157 |
|
|
System.out.println("PASSED: First mark/reset Test");
|
158 |
|
|
else
|
159 |
|
|
System.out.println("FAILED: First mark/reset Test");
|
160 |
|
|
}
|
161 |
|
|
catch (IOException e)
|
162 |
|
|
{
|
163 |
|
|
System.out.println("FAILED: First mark/reset Test: " + e);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
try
|
167 |
|
|
{
|
168 |
|
|
System.out.println("Test 3: Second mark/reset Test");
|
169 |
|
|
|
170 |
|
|
String str = "Growing up we heated our house with a wood stove. That\n" +
|
171 |
|
|
"thing could pump out some BTU's, let me tell you. No matter how\n" +
|
172 |
|
|
"cold it got outside, it was always warm inside. Of course the\n" +
|
173 |
|
|
"downside is that somebody had to chop the wood for the stove. That\n" +
|
174 |
|
|
"somebody was me. I was slave labor. My uncle would go back and\n" +
|
175 |
|
|
"chain saw up dead trees and I would load the wood in wagons and\n" +
|
176 |
|
|
"split it with a maul. Somehow my no account brother always seemed\n" +
|
177 |
|
|
"to get out of having to work.\n";
|
178 |
|
|
|
179 |
|
|
char[] buf = new char[str.length()];
|
180 |
|
|
str.getChars(0, str.length(), buf, 0);
|
181 |
|
|
BufferedReaderTest brt = new BufferedReaderTest(buf);
|
182 |
|
|
// BufferedReader br = new BufferedReader(car);
|
183 |
|
|
|
184 |
|
|
int total_read = marktest(brt);
|
185 |
|
|
|
186 |
|
|
if (total_read == str.length())
|
187 |
|
|
System.out.println("PASSED: Second mark/reset Test");
|
188 |
|
|
else
|
189 |
|
|
System.out.println("FAILED: Second mark/reset Test");
|
190 |
|
|
}
|
191 |
|
|
catch (IOException e)
|
192 |
|
|
{
|
193 |
|
|
System.out.println("FAILED: Second mark/reset Test: " + e);
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
System.out.println("Finished test of BufferedReader");
|
197 |
|
|
} // main
|
198 |
|
|
|
199 |
|
|
} // class BufferedReaderTest
|
200 |
|
|
|