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