1 // BufferedReaderTest.java
2 // submitted by Dalibor Topic <robilad@yahoo.com>
5 public class BufferedReaderTest {
7 static class PseudoReader extends Reader {
11 public int read(char buf[], int offset, int count) {
12 System.out.println("count = " + count);
17 static class PseudoReader2 extends Reader {
18 public int closeCounter = 0;
24 public int read(char buf[], int offset, int count) {
29 static class PseudoReader3 extends Reader {
33 public void close () {
36 /* This version of read returns a string of characters
37 without EOL on the first call,
38 then 0 characters on the second.
40 If its caller is blocking, the caller will try again,
41 otherwise it will have enough.
43 If it tries again, and calls read for the
44 third time, an IOException is thrown.
47 public int read(char[] buf2, int offset, int count)
51 throw (new IOException("you keep trying ..."));
60 String s = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
62 s.getChars(0, count, buf2, offset);
69 public static void main(String [] args) throws Exception {
71 // This test shows that the default buffersize for
72 // a BufferedReader is 8192 characters.
74 final int defaultsize = 8 * 1024;
76 BufferedReader br = new BufferedReader(new PseudoReader());
79 // Mark br, to make sure the data is copied into
84 // This should call the read method of PseudoReader
85 // only once if the default buffersize of
86 // a buffered reader is 8192.
88 br.read(new char[defaultsize], 0, defaultsize);
90 // And now call it to get one character, and
91 // it should refill its buffer
93 br.read(new char[1], 0, 1);
96 catch (IOException e) {
101 // Let's try to set a mark with
102 // a negative read ahead,
103 // this should throw an IllegalArgumentException
105 StringReader sr = new StringReader("sr");
106 br = new BufferedReader(sr);
111 catch (Exception e) {
112 System.out.println(e.toString());
116 // This is a simple test of BufferedReader
119 sr = new StringReader("1234567");
121 br = new BufferedReader(sr);
124 char [] buf = new char[3];
126 if (br.read(buf) == 3) {
127 System.out.println(buf);
130 System.out.println("Couldn't read 3 characters");
137 if (br.read(buf) == 4) {
138 System.out.println(buf);
141 System.out.println("Couldn't read 4 characters");
146 if (br.read(buf) == 4) {
147 System.out.println(buf);
150 System.out.println("Couldn't re-read 4 characters");
153 catch (IOException e) {
158 /* Trying to create a BufferedReader with
159 input Reader = null should not result in
160 an IllegalArgumentException being thrown,
161 instead a NullPointerException should be thrown
162 when the Reader is accessed.
165 sr = new StringReader("1234567");
166 br = new BufferedReader(sr);
169 br = new BufferedReader(null);
171 catch (NullPointerException e) {
172 System.out.println(e.toString());
178 catch (Exception e) {
179 System.out.println(e.toString());
182 sr = new StringReader("1234567");
184 /* Trying to create a BufferedReader with
185 buffer size <= 0 should result in
186 an IllegalArgumentException being thrown.
190 br = new BufferedReader(sr, 0);
192 catch (IllegalArgumentException e) {
193 System.out.println(e.toString());
196 /* A call to close a BufferedReader that has
197 already been closed should do nothing.
198 So the expected result should be "1"
201 PseudoReader2 pr2 = new PseudoReader2();
202 br = new BufferedReader(pr2);
208 catch (IOException e) {
209 System.out.println(e.toString());
212 System.out.println(pr2.closeCounter);
215 /* Trying to read from a BufferedReader that has
216 already been closed should generate an IOException.
222 catch (IOException e) {
223 System.out.println(e.toString());
228 catch (IOException e) {
229 System.out.println(e.toString());
234 catch (IOException e) {
235 System.out.println(e.toString());
238 br.read(new char[1]);
240 catch (IOException e) {
241 System.out.println(e.toString());
244 br.read(new char[1], 0, 1);
246 catch (IOException e) {
247 System.out.println(e.toString());
252 catch (IOException e) {
253 System.out.println(e.toString());
258 catch (IOException e) {
259 System.out.println(e.toString());
264 catch (IOException e) {
265 System.out.println(e.toString());
270 catch (IOException e) {
271 System.out.println(e.toString());
274 int bufferSize = 1000000;
276 sr = new StringReader("a");
278 // Allocate a really huge buffer
279 br = new BufferedReader(sr, bufferSize);
281 /* A closed BufferReader should free its buffer */
283 // close buffered reader
285 long before = Runtime.getRuntime().freeMemory();
288 // Call garbage collector and hope he does his work
291 long after = Runtime.getRuntime().freeMemory();
293 // System.out.println(before + " " + after);
295 // If garbage collector was successful, and close() did
296 // let go the buffer, we expect to see some dramatic
297 // change in the amount of free memory.
299 if (after - before >= bufferSize) {
300 // System.out.println("SUCCESS");
303 // System.out.println("FAILURE");
306 catch (IOException e) {
311 /* Test to see what happens when we try to
312 skip a negative number of characters
315 sr = new StringReader("0123456789");
316 br = new BufferedReader(sr,1);
319 System.out.println(br.skip(5));
320 System.out.println(br.skip(-5));
321 System.out.println((char) br.read());
323 catch (Exception e) {
324 System.out.println(e.toString());
328 /* Testing the reset method */
330 sr = new StringReader("ABCDEFGH");
331 br = new BufferedReader(sr,1);
333 /* Trying to reset an unmarked reader should result in
334 an Exception being thrown
337 System.out.println("Reset unmarked");
342 catch (Exception e) {
343 System.out.println(e.toString());
347 /* Now we try something slightly different.
348 We try to invalidate the mark by reading further
349 than the read ahead value passed as the parameter
350 when mark was called.
351 If we try to reset the buffer afterwards,
352 we should receive a nice Exception.
355 System.out.println("Reset invalidated");
359 System.out.println(br.read());
360 System.out.println(br.read());
363 catch (Exception e) {
364 System.out.println(e.toString());
367 // let's see what happens when I skip over
368 // marked read ahead buffer.
370 System.out.println("Skipping over marked buffer");
375 System.out.println(br.read());
377 // According to JDK 1.1.8 behaviour,
378 // this should invalidate the
379 // mark, for this buffer size at least.
382 System.out.println(br.read());
384 catch (Exception e) {
385 System.out.println(e.toString());
389 Actually, that seams to depend on the
390 buffer size. I've run tests with different
391 buffer sizes, and when the buffer is large
392 enough to serve the read ahead buffer and the
393 subsequent reads, then reset still works.
396 sr = new StringReader("ABCDEFGH");
397 br = new BufferedReader(sr);
399 System.out.println("Reset invalidated");
403 System.out.println(br.read());
404 System.out.println(br.read());
407 catch (Exception e) {
408 System.out.println(e.toString());
411 // let's see what happens when I skip over
412 // marked read ahead buffer, when
413 // buffer size is large enough to accomodate
414 // the requests without the need for refilling.
416 System.out.println("Skipping over marked buffer");
421 System.out.println(br.read());
423 // According to JDK 1.1.8 behaviour,
424 // this should not invalidate the
425 // mark. That is a strong hint that
426 // mark validation/invalidation should
427 // happen in the fillOutBuffer method.
430 System.out.println(br.read());
432 catch (Exception e) {
433 System.out.println(e.toString());
437 /* And now to something completely different:
438 BufferedReader.read (char [], int, int).
440 Here is a small test to check whether all
441 int arguments are checked against their
442 constraints (Java Class Lib. Vol. 1, 2nd Ed.)
444 This test should generate a lot of
448 sr = new StringReader("sr");
449 br = new BufferedReader(sr);
450 char [] buf = new char[1];
452 System.out.println("Null pointer");
457 catch (Exception e) {
458 System.out.println(e.toString());
461 System.out.println("offset >= buf.length");
466 catch (Exception e) {
467 System.out.println(e.toString());
470 System.out.println("offset >= buf.length");
474 /* The funny thing is that this one *doesn't*
475 generate an exception in JDK1.1.8, although
476 the offset parameter is out of bounds,
477 since there is no buf[1].
481 catch (Exception e) {
482 System.out.println(e.toString());
485 System.out.println("offset >= buf.length");
490 catch (Exception e) {
491 System.out.println(e.toString());
494 System.out.println("count > buf.length - offset");
499 catch (Exception e) {
500 System.out.println(e.toString());
503 System.out.println("offset < 0");
508 catch (Exception e) {
509 System.out.println(e.toString());
512 System.out.println("count < 0");
517 catch (Exception e) {
518 System.out.println(e.toString());
521 /* Test whether readLine is blocking or not */
523 PseudoReader3 pr3 = new PseudoReader3();
524 br = new BufferedReader(pr3, 10);
528 /* Java Class Libraries Vol 1 2nd Edition
529 says that readLine is blocking.
530 JDK 1.1.8 behaves that way.
532 System.out.println(br.readLine());
534 catch (Exception e) {
535 System.out.println(e.toString());
543 java.lang.IllegalArgumentException: Read-ahead limit < 0
547 java.lang.NullPointerException
548 java.lang.IllegalArgumentException: Buffer size <= 0
550 java.io.IOException: Stream closed
551 java.io.IOException: Stream closed
552 java.io.IOException: Stream closed
553 java.io.IOException: Stream closed
554 java.io.IOException: Stream closed
555 java.io.IOException: Stream closed
556 java.io.IOException: Stream closed
557 java.io.IOException: Stream closed
559 java.lang.IllegalArgumentException: skip value is negative
561 java.io.IOException: Stream not marked
565 java.io.IOException: Mark invalid
566 Skipping over marked buffer
568 java.io.IOException: Mark invalid
572 Skipping over marked buffer
576 java.lang.NullPointerException
578 java.lang.IndexOutOfBoundsException
581 java.lang.IndexOutOfBoundsException
582 count > buf.length - offset
583 java.lang.IndexOutOfBoundsException
585 java.lang.IndexOutOfBoundsException
587 java.lang.IndexOutOfBoundsException
588 java.io.IOException: you keep trying ...