public String readLine()

in bulkloader/src/main/java/org/apache/directory/mavibot/btree/PositionBufferedReader.java [305:428]


    public String readLine() throws IOException
    {
        synchronized ( lock )
        {
            if ( isClosed() )
            {
                throw new IOException( "File closed, cannot read from it" );
            }
            
            /* has the underlying stream been exhausted? */
            if ( pos == end && fillBuf() == -1 )
            {
                return null;
            }
            
            for ( int charPos = pos; charPos < end; charPos++ )
            {
                char ch = buf[charPos];
                
                if ( ch > '\r' )
                {
                    filePos++;
                    continue;
                }

                if ( ch == '\n' )
                {
                    String res = new String( buf, pos, charPos - pos );
                    pos = charPos + 1;
                    filePos++;

                    return res;
                }
                else if ( ch == '\r' )
                {
                    String res = new String( buf, pos, charPos - pos );
                    filePos++;
                    pos = charPos + 1;

                    if ( ( ( pos < end ) || ( fillBuf() != -1 ) )
                        && ( buf[pos] == '\n' ) )
                    {
                        filePos++;
                        pos++;
                    }

                    return res;
                }
            }

            char eol = '\0';
            StringBuilder result = new StringBuilder( 80 );
            /* Typical Line Length */

            result.append( buf, pos, end - pos );

            while ( true )
            {
                pos = end;

                /* Are there buffered characters available? */
                if ( eol == '\n' )
                {
                    return result.toString();
                }
                
                // attempt to fill buffer
                if ( fillBuf() == -1 )
                {
                    // characters or null.
                    return result.length() > 0 || eol != '\0'
                        ? result.toString()
                        : null;
                }

                filePos--;

                for ( int charPos = pos; charPos < end; charPos++ )
                {
                    char c = buf[charPos];
                    filePos++;
                    
                    if ( eol == '\0' )
                    {
                        if ( ( c == '\n' || c == '\r' ) )
                        {
                            eol = c;
                        }
                    }
                    else if ( eol == '\r' && c == '\n' )
                    {
                        if ( charPos > pos )
                        {
                            result.append( buf, pos, charPos - pos - 1 );
                        }

                        pos = charPos + 1;
                        
                        return result.toString();
                    }
                    else
                    {
                        if ( charPos > pos )
                        {
                            result.append( buf, pos, charPos - pos - 1 );
                        }

                        pos = charPos;
                        
                        return result.toString();
                    }
                }
                
                if ( eol == '\0' )
                {
                    result.append( buf, pos, end - pos );
                }
                else
                {
                    result.append( buf, pos, end - pos - 1 );
                }
            }
        }
    }