| |

VerySource

 Forgot password?
 Register
Search
View: 735|Reply: 4

A wrapper problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-8 03:00:01
| Show all posts |Read mode
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
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-25 08:45:01
| Show all posts
There is no\r\n in the last line, so check your eol
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-29 22:15:01
| Show all posts
if ((c == '\n') || (c == '\r')) {
eol = true;
There is a problem in this place, and the judgment of whether it is the last line is added, because there is no carriage return or line feed after the last line is written.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-30 23:45:01
| Show all posts
if (s == null)
s = new StringBuffer (defaultExpectedLineLength);
s.append (cb, startChar, i-startChar);
return j + ":" + s;
That's it!
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-13 16:30:02
| Show all posts
Thank younsre434654
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list