|
import java.io. *;
public class MyBufferedReader extends BufferedReader {
private Reader in;
private char cb [];
private int nChars, nextChar;
private static final int INVALIDATED = -2;
private static final int UNMARKED = -1;
private int markedChar = UNMARKED;
private int readAheadLimit = 0;
private boolean skipLF = false;
private boolean markedSkipLF = false;
private static int defaultCharBufferSize = 8192;
private static int defaultExpectedLineLength = 80;
private static int j = 0;
public MyBufferedReader (Reader in, int sz) {
super (in);
if (sz <= 0)
throw new IllegalArgumentException ("Buffer size <= 0");
this.in = in;
cb = new char [sz];
nextChar = nChars = 0;
}
public MyBufferedReader (Reader in) {
this (in, defaultCharBufferSize);
}
private void ensureOpen () throws IOException {
if (in == null)
throw new IOException ("Stream closed");
}
private void fill () throws IOException {
int dst;
if (markedChar <= UNMARKED) {
dst = 0;
} else {
int delta = nextChar-markedChar;
if (delta> = readAheadLimit) {
markedChar = INVALIDATED;
readAheadLimit = 0;
dst = 0;
} else {
if (readAheadLimit <= cb.length) {
System.arraycopy (cb, markedChar, cb, 0, delta);
markedChar = 0;
dst = delta;
} else {
char ncb [] = new char [readAheadLimit];
System.arraycopy (cb, markedChar, ncb, 0, delta);
cb = ncb;
markedChar = 0;
dst = delta;
}
nextChar = nChars = delta;
}
}
int n;
do {
n = in.read (cb, dst, cb.length-dst);
} while (n == 0);
if (n> 0) {
nChars = dst + n;
nextChar = dst;
}
}
String readLine (boolean ignoreLF) throws IOException {// overrides the parent's readLine () method
StringBuffer s = null;
int startChar;
boolean omitLF = ignoreLF || skipLF;
synchronized (lock) {
ensureOpen ();
bufferLoop:
for (;;) {
if (nextChar> = nChars)
fill ();
if (nextChar> = nChars) {
if (s! = null&&s.length ()> 0)
return s.toString ();
else
return null;
}
boolean eol = false;
char c = 0;
int i;
if (omitLF&&(cb [nextChar] == '\n'))
nextChar ++;
skipLF = false;
omitLF = false;
charLoop:
for (i = nextChar; i <nChars; i ++) {
c = cb [i];
if ((c == '\n') || (c == '\r')) {
eol = true;
break charLoop;
}
}
startChar = nextChar;
nextChar = i;
if (eol) {
String str;
if (s == null) {
str = new String (cb, startChar, i-startChar);
} else {
s.append (cb, startChar, i-startChar);
str = s.toString ();
}
nextChar ++;
if (c == '\r') {
skipLF = true;
}
j ++;
return str = j + ":" + str;
}
if (s == null)
s = new StringBuffer (defaultExpectedLineLength);
s.append (cb, startChar, i-startChar);
}
}
}
public String readLine () throws IOException {
return readLine (false);
}
public boolean ready () throws IOException {
synchronized (lock) {
ensureOpen ();
if (skipLF) {
if (nextChar> = nChars&&in.ready ()) {
fill ();
}
if (nextChar <nChars) {
if (cb [nextChar] == '\n')
nextChar ++;
skipLF = false;
}
}
return (nextChar <nChars) || in.ready ();
}
}
}
class Test7
{
public static void main (String args []) throws Exception
{
File f1 = new File ("test7.java");
MyBufferedReader in = new MyBufferedReader (new FileReader (f1));
while (in.ready ())
{
System.out.println (in.readLine ());
}
}
}
It's very busy here that IT prawn can help my brother solve this problem! The problem is how the last line can't be printed |
|