Skip to content

Commit

Permalink
NIFI-3495 polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
olegz committed Feb 17, 2017
1 parent d65ceef commit b9c4cca
Showing 1 changed file with 21 additions and 25 deletions.
Expand Up @@ -110,9 +110,7 @@ public OffsetInfo nextOffsetInfo(byte[] startsWith) {
for (i = this.index; i < this.bufferLength; i++) {
byteVal = this.buffer[i];
lineLength++;
int[] eol = computeEol(byteVal, i + 1);
int crlfLength = eol[0];
boolean readAheadPerformed = eol[1] == 1;
int crlfLength = computeEol(byteVal, i + 1);
if (crlfLength > 0) {
i += crlfLength;
if (crlfLength == 2) {
Expand All @@ -122,9 +120,6 @@ public OffsetInfo nextOffsetInfo(byte[] startsWith) {
if (startsWith != null) {
token = this.extractDataToken(lineLength);
}
if (!readAheadPerformed) {
this.index = i;
}
this.mark = this.index;
break lineLoop;
}
Expand Down Expand Up @@ -155,37 +150,40 @@ public OffsetInfo nextOffsetInfo(byte[] startsWith) {
}

/**
* Determines if the line terminates. Returns int array with two elements.
* The first elements specifies the length of the CRLF (only CR or CR and
* LF) and therefore can only have values: 0 - not the end of the line 1 -
* the end of the line either via CR or LF 2 - the end of the line with both
* CR and LF
* Determines if the line terminates. Returns int specifying the length of
* the CRLF (i.e., only CR or LF or CR and LF) and therefore can only have
* values of:
* 0 - not the end of the line
* 1 - the end of the line either via CR or LF
* 2 - the end of the line with both CR and LF
*
* It performs the read ahead on the buffer if need to. If such read ahead
* was performed, then second element will carry value of 1 otherwise 0.
* It performs the read ahead on the buffer if need to.
*/
private int[] computeEol(byte currentByte, int currentIndex) {
private int computeEol(byte currentByte, int providedIndex) {
int actualIndex = providedIndex - 1;
boolean readAhead = false;
int crlfLength = 0;
if (currentByte == '\n') {
crlfLength = 1;
} else if (currentByte == '\r') {
if (currentIndex >= this.bufferLength) {
this.index = currentIndex;
if (providedIndex >= this.bufferLength) {
this.index = this.bufferLength;
this.fill();
currentIndex = this.index;
providedIndex = this.index;
readAhead = true;
}
crlfLength = 1;
if (currentIndex < this.buffer.length - 1) {
currentByte = this.buffer[currentIndex];
if (providedIndex < this.buffer.length - 1) {
currentByte = this.buffer[providedIndex];
crlfLength = currentByte == '\n' ? 2 : 1;
if (readAhead) {
this.index += (crlfLength - 1);
}
}
}
return new int[] { crlfLength, readAhead ? 1 : 0 };

if (crlfLength > 0) {
this.index = readAhead ? this.index + (crlfLength - 1) : (actualIndex + crlfLength);
}

return crlfLength;
}

private byte[] extractDataToken(int length) {
Expand All @@ -209,9 +207,7 @@ private void fill() {
this.buffer = newBuff;
} else { // shuffle
int length = this.index - this.mark;

System.arraycopy(this.buffer, this.mark, this.buffer, 0, length);

this.index = length;
this.mark = 0;
}
Expand Down

0 comments on commit b9c4cca

Please sign in to comment.