NIFI-5711 NLKBufferedReader appears extend and copy portions of the JDK BufferedReader

This commit is contained in:
patricker 2018-10-16 15:56:55 -06:00 committed by Mark Payne
parent 87c6b3aa7c
commit 95e4294673
1 changed files with 26 additions and 118 deletions

View File

@ -22,18 +22,6 @@ import java.io.Reader;
//NLKBufferedReader = New Line Keeper Buffered Reader
public class NLKBufferedReader 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; /* Valid only when markedChar > 0 */
private static int defaultCharBufferSize = 8192;
private static int defaultExpectedLineLength = 80;
/**
* Creates a buffering character-input stream that uses an input buffer of the specified size.
*
@ -44,18 +32,16 @@ public class NLKBufferedReader extends BufferedReader {
*/
public NLKBufferedReader(Reader in, int sz) {
super(in, sz);
this.in = in;
cb = new char[sz];
nextChar = nChars = 0;
}
/**
* Creates a buffering character-input stream that uses a default-sized input buffer.
*
* @param in A Reader
*
*/
public NLKBufferedReader(Reader in) {
this(in, defaultCharBufferSize);
super(in);
}
/**
@ -67,115 +53,37 @@ public class NLKBufferedReader extends BufferedReader {
*/
@Override
public String readLine() throws IOException {
StringBuffer s = null;
int startChar;
StringBuilder stringBuilder = new StringBuilder();
synchronized (lock) {
ensureOpen();
int intchar = read();
while(intchar != -1){
char c = (char) intchar;
stringBuilder.append(c);
bufferLoop:
for (;;) {
if (nextChar >= nChars) {
fill();
if(c == '\n') {
break;
} else if(c == '\r'){
// Peek at next character, check if it's \n
int charPeek = peek();
if(charPeek != -1 && (char)charPeek=='\n'){
stringBuilder.append((char)read());
}
if (nextChar >= nChars) { /* EOF */
if (s != null && s.length() > 0) {
return s.toString();
} else {
return null;
}
}
boolean eol = false;
char c = 0;
int i;
charLoop:
for (i = nextChar; i < nChars; i++) {
c = cb[i];
if ((c == '\n') || (c == '\r')) {
if ((c == '\r') && (cb.length > i + 1) && cb[i + 1] == '\n') { // windows case '\r\n' here verify the next character i+1
i++;
}
eol = true;
break charLoop;
}
}
startChar = nextChar;
nextChar = i;
if (eol) {
String str;
if (s == null) {
str = new String(cb, startChar, (i + 1) - startChar);
} else {
s.append(cb, startChar, (i + 1) - startChar);
str = s.toString();
}
nextChar++;
return str;
}
if (s == null) {
s = new StringBuffer(defaultExpectedLineLength);
}
s.append(cb, startChar, i - startChar);
break;
}
intchar = read();
}
String result = stringBuilder.toString();
return result.length()==0?null:result;
}
/**
* Checks to make sure that the stream has not been closed
*/
private void ensureOpen() throws IOException {
if (in == null) {
throw new IOException("Stream closed");
}
}
public int peek() throws IOException {
mark(1);
int readByte = read();
reset();
/**
* Fills the input buffer, taking the mark into account if it is valid.
*/
private void fill() throws IOException {
int dst;
if (markedChar <= UNMARKED) {
/* No mark */
dst = 0;
} else {
/* Marked */
int delta = nextChar - markedChar;
if (delta >= readAheadLimit) {
/* Gone past read-ahead limit: Invalidate mark */
markedChar = INVALIDATED;
readAheadLimit = 0;
dst = 0;
} else {
if (readAheadLimit <= cb.length) {
/* Shuffle in the current buffer */
System.arraycopy(cb, markedChar, cb, 0, delta);
markedChar = 0;
dst = delta;
} else {
/* Reallocate buffer to accommodate read-ahead limit */
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;
}
return readByte;
}
}