private String readLine ( boolean trim ) throws IOException { boolean done = false ; boolean sawCarriage = false ; // bytes to trim (the \r and the \n) int removalBytes = 0 ; while ( ! done ) { if ( isReadBufferEmpty ( ) ) { offset = 0 ; end = 0 ; int bytesRead = inputStream . read ( buffer , end , Math . min ( DEFAULT_READ_COUNT , buffer . length - end ) ) ; if ( bytesRead < 0 ) { // we failed to read anything more... throw new IOException ( "Reached the end of the stream" ) ; } else { end += bytesRead ; } } int originalOffset = offset ; for ( ; ! done && offset < end ; offset ++ ) { if ( buffer [ offset ] == LF ) { int cpLength = offset - originalOffset + 1 ; if ( trim ) { int length = 0 ; if ( buffer [ offset ] == LF ) { length ++ ; if ( sawCarriage ) { length ++ ; } } cpLength -= length ; } if ( cpLength > 0 ) { copyToStrBuffer ( buffer , originalOffset , cpLength ) ; } else { // negative length means we need to trim a \r from strBuffer removalBytes = cpLength ; } done = true ; } else { // did not see newline: sawCarriage = buffer [ offset ] == CR ; } } if ( ! done ) { copyToStrBuffer ( buffer , originalOffset , end - originalOffset ) ; offset = end ; } } int strLength = strBufferIndex + removalBytes ; strBufferIndex = 0 ; return new String ( strBuffer , 0 , strLength , charset ) ; }
Reads a line from the input stream where a line is terminated by \ r \ n or \ r \ n