private int getTokenizedHeaderLine ( final InputStream stream , List < String > list ) throws IOException { // Preallocate usual line size. StringBuilder buffer = new StringBuilder ( 2048 + 20 ) ; int read = 0 ; int previous = - 1 ; for ( int c = - 1 ; true ; ) { previous = c ; c = stream . read ( ) ; if ( c == - 1 ) { throw new RecoverableIOException ( "Hit EOF before header EOL." ) ; } c &= 0xff ; read ++ ; if ( read > MAX_HEADER_LINE_LENGTH ) { throw new IOException ( "Header line longer than max allowed " + " -- " + String . valueOf ( MAX_HEADER_LINE_LENGTH ) + " -- or passed buffer doesn't contain a line (Read: " + buffer . length ( ) + "). Here's" + " some of what was read: " + buffer . substring ( 0 , Math . min ( buffer . length ( ) , 256 ) ) ) ; } if ( c == LINE_SEPARATOR ) { if ( buffer . length ( ) == 0 ) { // Empty line at start of buffer. Skip it and try again. continue ; } if ( list != null ) { list . add ( buffer . toString ( ) ) ; } // LOOP TERMINATION. break ; } else if ( c == HEADER_FIELD_SEPARATOR ) { if ( ! isStrict ( ) && previous == HEADER_FIELD_SEPARATOR ) { // Early ARCs sometimes had multiple spaces between fields. continue ; } if ( list != null ) { list . add ( buffer . toString ( ) ) ; } // reset to empty buffer . setLength ( 0 ) ; } else { buffer . append ( ( char ) c ) ; } } // List must have at least 3 elements in it and no more than 10. If // it has other than this, then bogus parse. if ( list != null && ( list . size ( ) < 3 || list . size ( ) > 100 ) ) { throw new IOException ( "Unparseable header line: " + list ) ; } // save verbatim header String this . headerString = StringUtils . join ( list , " " ) ; return read ; }
Get a record header line as list of tokens .