public int readLine ( final byte [ ] array , final int off , final int len , final EnumSet < LineTerminator > terminators ) throws IOException { ByteArrays . ensureOffsetLength ( array , off , len ) ; if ( len == 0 ) return 0 ; // 0-length reads always return 0 if ( noMoreCharacters ( ) ) return - 1 ; int i , k = 0 , remaining = len , read = 0 ; // The number of bytes still to be read for ( ; ; ) { for ( i = 0 ; i < avail && i < remaining && ( k = buffer [ pos + i ] ) != ' ' && k != ' ' ; i ++ ) ; System . arraycopy ( buffer , pos , array , off + read , i ) ; pos += i ; avail -= i ; read += i ; remaining -= i ; if ( remaining == 0 ) { readBytes += read ; return read ; // We did not stop because of a terminator } if ( avail > 0 ) { // We met a terminator if ( k == ' ' ) { // LF first pos ++ ; avail -- ; if ( terminators . contains ( LineTerminator . LF ) ) { readBytes += read + 1 ; return read ; } else { array [ off + read ++ ] = ' ' ; remaining -- ; } } else if ( k == ' ' ) { // CR first pos ++ ; avail -- ; if ( terminators . contains ( LineTerminator . CR_LF ) ) { if ( avail > 0 ) { if ( buffer [ pos ] == ' ' ) { // CR/LF with LF already in the buffer. pos ++ ; avail -- ; readBytes += read + 2 ; return read ; } } else { // We must search for the LF. if ( noMoreCharacters ( ) ) { // Not found a matching LF because of end of file, will return CR in buffer if not a terminator if ( ! terminators . contains ( LineTerminator . CR ) ) { array [ off + read ++ ] = ' ' ; remaining -- ; readBytes += read ; } else readBytes += read + 1 ; return read ; } if ( buffer [ 0 ] == ' ' ) { // Found matching LF, won't return terminators in the buffer pos ++ ; avail -- ; readBytes += read + 2 ; return read ; } } } if ( terminators . contains ( LineTerminator . CR ) ) { readBytes += read + 1 ; return read ; } array [ off + read ++ ] = ' ' ; remaining -- ; } } else if ( noMoreCharacters ( ) ) { readBytes += read ; return read ; } } }
Reads a line into the given byte - array fragment .