LiveFileChunk resolveChunk ( LiveFileChunk chunk ) { List < FileLine > completeLines = new ArrayList <> ( ) ; List < FileLine > chunkLines = chunk . getLines ( ) ; if ( incompleteMultiLine . length ( ) == 0 ) { incompleteMultiLineOffset = chunk . getOffset ( ) ; incompleteMultiLineTruncated = chunk . isTruncated ( ) ; } incompleteMultiLineTruncated |= chunk . isTruncated ( ) ; int pos = 0 ; int idx = findNextMainLine ( chunk , pos ) ; // while we have main lines we keep adding/compacting into the new chunk while ( idx > - 1 ) { //any multi lines up to the next main line belong to the previous main line for ( int i = pos ; i < idx ; i ++ ) { incompleteMultiLine . append ( chunkLines . get ( i ) . getText ( ) ) ; } // if we have incomplete lines, at this point they are a complete multiline, compact and add to new chunk lines if ( incompleteMultiLine . length ( ) != 0 ) { completeLines . add ( new FileLine ( incompleteMultiLineOffset , incompleteMultiLine . toString ( ) ) ) ; incompleteMultiLineOffset += incompleteMultiLine . length ( ) ; // clear the incomplete multi lines as we just used them to create a full line incompleteMultiLine . setLength ( 0 ) ; incompleteMultiLineTruncated = false ; } // add the current main line as incomplete as we still don't if it is a complete line incompleteMultiLine . append ( chunkLines . get ( idx ) . getText ( ) ) ; // find the next main line pos = idx + 1 ; idx = findNextMainLine ( chunk , pos ) ; } // lets process the left over multi lines in the chunk after the last main line. // if any they will kept to completed with lines from the next chunk. for ( int i = pos ; i < chunkLines . size ( ) ; i ++ ) { incompleteMultiLine . append ( chunkLines . get ( i ) . getText ( ) ) ; } if ( completeLines . isEmpty ( ) ) { // didn't get a complete multi line yet, we keep storing lines but return a null chunk chunk = null ; } else { // create a new chunk with all complete multi lines chunk = new LiveFileChunk ( chunk . getTag ( ) , chunk . getFile ( ) , chunk . getCharset ( ) , completeLines , incompleteMultiLineTruncated ) ; } return chunk ; }
it there is an incomplete multiline from a previous chunk it starts from it .