private void split ( ) throws IOException { hasSplit = true ; IntegerBuffer before = new IntegerBuffer ( ) ; IntegerBuffer after = new IntegerBuffer ( ) ; int current ; boolean ready = false ; boolean stillNeedToSeeDoctype = true ; while ( ! ready && ( current = original . read ( ) ) != - 1 ) { if ( Character . isWhitespace ( ( char ) current ) ) { before . append ( current ) ; } else if ( current == ' ' ) { // could be XML declaration, comment, PI, DOCTYPE // or the first element int [ ] elementOrDeclOr = readUntilCloseCharIsReached ( ) ; if ( elementOrDeclOr . length > 0 ) { if ( elementOrDeclOr [ 0 ] == ' ' ) { // XML declaration or PI before . append ( ' ' ) ; before . append ( elementOrDeclOr ) ; } else if ( elementOrDeclOr [ 0 ] != ' ' ) { // first element after . append ( ' ' ) ; after . append ( elementOrDeclOr ) ; stillNeedToSeeDoctype = false ; ready = true ; } else { // comment or doctype IntegerBuffer b = new IntegerBuffer ( elementOrDeclOr . length ) ; b . append ( elementOrDeclOr ) ; if ( b . indexOf ( DOCTYPE_INTS ) == - 1 ) { after . append ( ' ' ) ; after . append ( elementOrDeclOr ) ; } else { // swallow old declaration stillNeedToSeeDoctype = false ; } ready = true ; } } else { after . append ( ' ' ) ; stillNeedToSeeDoctype = false ; ready = true ; } } else { after . append ( current ) ; stillNeedToSeeDoctype = false ; ready = true ; } } // need to eliminate original DOCTYPE if it exists while ( stillNeedToSeeDoctype && ( current = original . read ( ) ) != - 1 ) { if ( Character . isWhitespace ( ( char ) current ) ) { after . append ( current ) ; } else if ( current == ' ' ) { int [ ] elementOrDeclOr = readUntilCloseCharIsReached ( ) ; if ( elementOrDeclOr . length > 0 ) { if ( elementOrDeclOr [ 0 ] == ' ' ) { // PI after . append ( ' ' ) ; after . append ( elementOrDeclOr ) ; } else if ( elementOrDeclOr [ 0 ] != ' ' ) { // first element after . append ( ' ' ) ; after . append ( elementOrDeclOr ) ; stillNeedToSeeDoctype = false ; } else { // comment or doctype IntegerBuffer b = new IntegerBuffer ( elementOrDeclOr . length ) ; b . append ( elementOrDeclOr ) ; if ( b . indexOf ( DOCTYPE_INTS ) == - 1 ) { after . append ( ' ' ) ; after . append ( elementOrDeclOr ) ; } else { // swallow old declaration stillNeedToSeeDoctype = false ; } } } else { after . append ( ' ' ) ; stillNeedToSeeDoctype = false ; } } else { after . append ( current ) ; stillNeedToSeeDoctype = false ; } } beforeDoctype = before . size ( ) > 0 ? new IntBufferReadable ( before ) : null ; afterDoctype = after . size ( ) > 0 ? new IntBufferReadable ( after ) : null ; }
Reads enough of the original Readable to know where to place the declaration . Fills beforeDecl and afterDecl from the data read ahead . Swallows the original DOCTYPE if there is one .