📄 lineinput.java
字号:
fill();
if (_pos >= _avail)
b = -1;
else
b = _buf[_pos++] & 255;
return b;
}
/* ------------------------------------------------------------ */
public synchronized int read(byte b[], int off, int len) throws IOException {
int avail = _avail - _pos;
if (avail <= 0) {
fill();
avail = _avail - _pos;
}
if (avail <= 0)
len = -1;
else {
len = (avail < len) ? avail : len;
System.arraycopy(_buf, _pos, b, off, len);
_pos += len;
}
return len;
}
/* ------------------------------------------------------------ */
public long skip(long n) throws IOException {
int avail = _avail - _pos;
if (avail <= 0) {
fill();
avail = _avail - _pos;
}
if (avail <= 0)
n = 0;
else {
n = (avail < n) ? avail : n;
_pos += n;
}
return n;
}
/* ------------------------------------------------------------ */
public synchronized int available() throws IOException {
int in_stream = in.available();
if (_byteLimit >= 0 && in_stream > _byteLimit)
in_stream = _byteLimit;
return _avail - _pos + in_stream;
}
/* ------------------------------------------------------------ */
public synchronized void mark(int limit) throws IllegalArgumentException {
if (limit > _buf.length) {
byte[] new_buf = new byte[limit];
System.arraycopy(_buf, _pos, new_buf, _pos, _avail - _pos);
_buf = new_buf;
if (_byteBuffer != null)
_byteBuffer.setBuffer(_buf);
}
_mark = _pos;
}
/* ------------------------------------------------------------ */
public synchronized void reset() throws IOException {
if (_mark < 0)
throw new IOException("Resetting to invalid mark");
_pos = _mark;
_mark = -1;
}
/* ------------------------------------------------------------ */
public boolean markSupported() {
return true;
}
/* ------------------------------------------------------------ */
private void fill() throws IOException {
// if the mark is in the middle of the buffer
if (_mark > 0) {
// moved saved bytes to start of buffer
int saved = _contents - _mark;
System.arraycopy(_buf, _mark, _buf, 0, saved);
_pos -= _mark;
_avail -= _mark;
_contents = saved;
_mark = 0;
} else if (_mark < 0 && _pos > 0) {
// move remaining bytes to start of buffer
int saved = _contents - _pos;
System.arraycopy(_buf, _pos, _buf, 0, saved);
_avail -= _pos;
_contents = saved;
_pos = 0;
} else if (_mark == 0 && _pos > 0 && _contents == _buf.length) {
// Discard the mark as we need the space.
_mark = -1;
fill();
return;
}
// Get ready to top up the buffer
int n = 0;
_eof = false;
// Handle byte limited EOF
if (_byteLimit == 0)
_eof = true;
// else loop until something is read.
else
while (!_eof && n == 0 && _buf.length > _contents) {
// try to read as much as will fit.
int space = _buf.length - _contents;
n = in.read(_buf, _contents, space);
if (n <= 0) {
// If no bytes - we could be NBIO, so we want to avoid
// a busy loop.
if (n == 0) {
// Yield to give a chance for some bytes to turn up
Thread.yield();
// Do a byte read as that is blocking
int b = in.read();
if (b >= 0) {
n = 1;
_buf[_contents++] = (byte) b;
} else
_eof = true;
} else
_eof = true;
} else
_contents += n;
_avail = _contents;
// If we have a byte limit
if (_byteLimit > 0) {
// adjust the bytes available
if (_contents - _pos >= _byteLimit)
_avail = _byteLimit + _pos;
if (n > _byteLimit)
_byteLimit = 0;
else if (n >= 0)
_byteLimit -= n;
else if (n == -1)
throw new IOException("Premature EOF");
}
}
// If we have some characters and the last read was a CR and
// the first char is a LF, skip it
if (_avail - _pos > 0 && _lastCr && _buf[_pos] == LF) {
_seenCrLf = true;
_pos++;
if (_mark >= 0)
_mark++;
_lastCr = false;
// If the byte limit has just been imposed, dont count
// LF as content.
if (_byteLimit >= 0 && _newByteLimit) {
if (_avail < _contents)
_avail++;
else
_byteLimit++;
}
// If we ate all that ws filled, fill some more
if (_pos == _avail)
fill();
}
_newByteLimit = false;
}
/* ------------------------------------------------------------ */
private int fillLine(int maxLen) throws IOException {
_mark = _pos;
if (_pos >= _avail)
fill();
if (_pos >= _avail)
return -1;
byte b;
boolean cr = _lastCr;
boolean lf = false;
_lastCr = false;
int len = 0;
LineLoop: while (_pos <= _avail) {
// if we have gone past the end of the buffer
while (_pos == _avail) {
// If EOF or no more space in the buffer,
// return a line.
if (_eof || (_mark == 0 && _contents == _buf.length)) {
_lastCr = !_eof && _buf[_avail - 1] == CR;
cr = true;
lf = true;
break LineLoop;
}
// If we have a CR and no more characters are available
if (cr && in.available() == 0 && !_seenCrLf) {
_lastCr = true;
cr = true;
lf = true;
break LineLoop;
} else {
// Else just wait for more...
_pos = _mark;
fill();
_pos = len;
cr = false;
}
}
// Get the byte
b = _buf[_pos++];
switch (b) {
case LF:
if (cr)
_seenCrLf = true;
lf = true;
break LineLoop;
case CR:
if (cr) {
// Double CR
if (_pos > 1) {
_pos--;
break LineLoop;
}
}
cr = true;
break;
default:
if (cr) {
if (_pos == 1)
cr = false;
else {
_pos--;
break LineLoop;
}
}
len++;
if (len == maxLen) {
// look for EOL
if (_mark != 0 && _pos + 2 >= _avail && _avail < _buf.length)
fill();
if (_pos < _avail && _buf[_pos] == CR) {
cr = true;
_pos++;
}
if (_pos < _avail && _buf[_pos] == LF) {
lf = true;
_pos++;
}
if (!cr && !lf) {
// fake EOL
lf = true;
cr = true;
}
break LineLoop;
}
break;
}
}
if (!cr && !lf && len == 0)
len = -1;
return len;
}
/* ------------------------------------------------------------ */
private static class ByteBuffer extends ByteArrayInputStream {
ByteBuffer(byte[] buffer) {
super(buffer);
}
void setBuffer(byte[] buffer) {
buf = buffer;
}
void setStream(int offset, int length) {
pos = offset;
count = offset + length;
mark = -1;
}
}
/* ------------------------------------------------------------ */
/**
* Reusable LineBuffer. Externalized LineBuffer for fast line parsing.
*/
public static class LineBuffer {
public char[] buffer;
public int size;
public LineBuffer(int maxLineLength) {
buffer = new char[maxLineLength];
}
public String toString() {
return new String(buffer, 0, size);
}
}
/* ------------------------------------------------------------ */
public void destroy() {
ByteArrayPool.returnByteArray(_buf);
_byteBuffer = null;
_reader = null;
_lineBuffer = null;
_encoding = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -