I do simple rownumber calculation in InputStream (calc number of NewLines #10)
for (int i = 0; i < readBytes ; i++) { if ( b[ i + off ] == 10 ) { // New Line (10) rowCount++; } } Can I do it faster? Without iteration by one byte? Probably I am looking for some class which able to use CPU specific instructions (simd/sse).
All code:
@Override public int read(byte[] b, int off, int len) throws IOException { int readBytes = in.read(b, off, len); for (int i = 0; i < readBytes ; i++) { hadBytes = true; // at least once we read something lastByteIsNewLine = false; if ( b[ i + off ] == 10 ) { // New Line (10) rowCount++; lastByteIsNewLine = (i == readBytes - 1); // last byte in buffer was the newline } } if ( hadBytes && readBytes == -1 && ! lastByteIsNewLine ) { // file is not empty + EOF + last byte was not NewLine rowCount++; } return readBytes; }
readBytes? Please post its content