// We reached the end of the buffer and did not find a new line.
// reset positionInBuffer
// Continue and read in more data
protected final char[] readLine() throws IOException {
		boolean foundLine = false;
		char[] returnLine = null;
		char[] partialLine = null;
		while (!foundLine) {
			// System.out.println("readline " + positionInBuffer + " " + new
			// String(buffer));
			if (positionInBuffer < 0 || !buffer.hasRemaining()) {
				// read more data
				charactersRead = read();
				totalCharactersRead += charactersRead;
				positionInBuffer = 0;
			}
			int[] lineBoundaries = getNextLineBoundaries(positionInBuffer,
					charactersRead, buffer);
			int lengthOfLine;
			if (lineBoundaries[0] < 0 && lineBoundaries[1] < 0) {
				// We reached the end of the buffer and did not find a new line.
				// reset positionInBuffer
				// Continue and read in more data
				if (charactersRead < 0) {
					return partialLine;
				}
				positionInBuffer = -1;
				continue;
			} else if (lineBoundaries[1] >= 0) {
				// Newline found
				lengthOfLine = lineBoundaries[1]
						- Math.max(0, lineBoundaries[0]) + 1;
			} else {
				lengthOfLine = charactersRead - lineBoundaries[0];
			}
			/************************************************************************************************
			 * TODO I think this is broken because lineBounardaries[0] is not
			 * taken into account when populating the buffer like it was in
			 * System.arrayCopy
			 *************************************************************************************************/
			if (charactersRead < 0) {
				// We've reached the end of the file
				return partialLine;
			} else if (lineBoundaries[0] >= 0) {
				if (lineBoundaries[1] < 0) {
					// If partialLine is already created, just add to it.
					// TODO this is found below. Break it out as a function or
					// change the if statement?
					if (partialLine == null) {
						partialLine = new char[lengthOfLine];
						buffer.position(lineBoundaries[0]);
						buffer.get(partialLine, 0, lengthOfLine);
						// System.arraycopy(buffer, lineBoundaries[0],
						// partialLine, 0, lengthOfLine);
					} else {
						char[] newPartialLine = new char[lengthOfLine
								+ partialLine.length];
						// TODO do we need two copies?
						System.arraycopy(partialLine, 0, newPartialLine, 0,
								partialLine.length);
						// TODO this does not look right
						buffer.position(lineBoundaries[0]);
						buffer.get(newPartialLine, partialLine.length,
								lengthOfLine);
						// System.arraycopy(buffer, lineBoundaries[0],
						// newPartialLine, partialLine.length, lengthOfLine);
						partialLine = newPartialLine;
					}
					positionInBuffer = -1;
				} else if (lineBoundaries[0] == lineBoundaries[1]
						&& (buffer.get(lineBoundaries[0]) == '\n' || buffer
								.get(lineBoundaries[0]) == '\r')) {
					// Found a new line. If there is a partial line, return it
					// otherwise keep looking.
					// Todo put a better check than this
					positionInBuffer = lineBoundaries[1] + 2;
					if (partialLine != null) {
						return partialLine;
					}
				} else {
					if (partialLine == null) {
						returnLine = new char[lengthOfLine];
						buffer.position(lineBoundaries[0]);
						buffer.get(returnLine, 0, lengthOfLine);
						// System.arraycopy(buffer, lineBoundaries[0],
						// returnLine, 0, lengthOfLine);
					} else {
						returnLine = new char[lengthOfLine + partialLine.length];
						// TODO is this right?
						System.arraycopy(partialLine, 0, returnLine, 0,
								partialLine.length);
						buffer.position(lineBoundaries[0]);
						buffer.get(returnLine, partialLine.length, lengthOfLine);
						// System.arraycopy(buffer, lineBoundaries[0],
						// returnLine, partialLine.length, lengthOfLine);
						partialLine = null;
					}
					positionInBuffer = lineBoundaries[1] + 2;
					return returnLine;
				}
			} else {
				// Keep looping. The only case where I can think of that this
				// would happen is if
				// the buffer is full of new line characters. Hopefully that
				// will never happen.
			}
		}
		return returnLine;
	}
int lengthOfLine;
if (lineBoundaries[0] < 0 && lineBoundaries[1] < 0) {
// We reached the end of the buffer and did not find a new line.
// reset positionInBuffer
// Continue and read in more data
if (charactersRead < 0) {
return partialLine;
if (positionInBuffer < 0 || !buffer.hasRemaining()) {
// read more data
charactersRead = read();
totalCharactersRead += charactersRead;
positionInBuffer = 0;
}
int[] lineBoundaries = getNextLineBoundaries(positionInBuffer,
charactersRead, buffer);
int lengthOfLine;
if (lineBoundaries[0] < 0 && lineBoundaries[1] < 0) {
// We reached the end of the buffer and did not find a new line.
// reset positionInBuffer
// Continue and read in more data
if (charactersRead < 0) {
return partialLine;
}
positionInBuffer = -1;
continue;
} else if (lineBoundaries[1] >= 0) {
// Newline found
lengthOfLine = lineBoundaries[1]
- Math.max(0, lineBoundaries[0]) + 1;
} else {
protected final char[] readLine() throws IOException {
boolean foundLine = false;
char[] returnLine = null;
char[] partialLine = null;
while (!foundLine) {
// System.out.println("readline " + positionInBuffer + " " + new
// String(buffer));
if (positionInBuffer < 0 || !buffer.hasRemaining()) {
// read more data
charactersRead = read();
totalCharactersRead += charactersRead;
positionInBuffer = 0;
}
int[] lineBoundaries = getNextLineBoundaries(positionInBuffer,
charactersRead, buffer);
int lengthOfLine;
if (lineBoundaries[0] < 0 && lineBoundaries[1] < 0) {
// We reached the end of the buffer and did not find a new line.
// reset positionInBuffer
// Continue and read in more data
if (charactersRead < 0) {
return partialLine;
}
positionInBuffer = -1;
continue;
} else if (lineBoundaries[1] >= 0) {
// Newline found
lengthOfLine = lineBoundaries[1]
- Math.max(0, lineBoundaries[0]) + 1;
} else {
lengthOfLine = charactersRead - lineBoundaries[0];
}
/************************************************************************************************
* TODO I think this is broken because lineBounardaries[0] is not
* taken into account when populating the buffer like it was in
* System.arrayCopy
*************************************************************************************************/
if (charactersRead < 0) {
// We've reached the end of the file
return partialLine;