private long readLines ( final RandomAccessFile reader ) throws IOException { final ByteArrayOutputStream lineBuf = new ByteArrayOutputStream ( 64 ) ; long pos = reader . getFilePointer ( ) ; long rePos = pos ; // position to re-read int num ; boolean seenCR = false ; // FIXME replace -1 with EOF when we're merging back into commons-io while ( ( num = reader . read ( this . inbuf ) ) != - 1 ) { for ( int i = 0 ; i < num ; i ++ ) { final byte ch = this . inbuf [ i ] ; switch ( ch ) { case ' ' : seenCR = false ; // swallow CR before LF this . listener . handle ( new String ( lineBuf . toByteArray ( ) , this . cset ) ) ; lineBuf . reset ( ) ; rePos = pos + i + 1 ; break ; case ' ' : if ( seenCR ) { lineBuf . write ( ' ' ) ; } seenCR = true ; break ; default : if ( seenCR ) { seenCR = false ; // swallow final CR this . listener . handle ( new String ( lineBuf . toByteArray ( ) , this . cset ) ) ; lineBuf . reset ( ) ; rePos = pos + i + 1 ; } lineBuf . write ( ch ) ; } } pos = reader . getFilePointer ( ) ; } IOUtils . closeQuietly ( lineBuf ) ; // not strictly necessary reader . seek ( rePos ) ; // Ensure we can re-read if necessary return rePos ; }
Read new lines .