private static String[] readHeaderLines( InputStream is )
            throws IOException {
        List lineList = new ArrayList();
        StringBuffer sbuf = new StringBuffer();
        boolean hasChars = false;
        for ( int c; ( c = is.read() ) >= 0; ) {
            hasChars = true;
            switch ( c ) {

                // CRLF is the correct HTTP line terminator.
                case '\r':
                    if ( is.read() == '\n' ) {
                        if ( sbuf.length() == 0 ) {
                            return (String[])
                                   lineList.toArray( new String[ 0 ] );
                        }
                        else {
                            lineList.add( sbuf.toString() );
                            sbuf.setLength( 0 );
                        }
                    }
                    else {
                        throw new HttpException( 400, "CR w/o LF" );
                    }
                    break;

                // HTTP 1.1 recommends that a lone LF is also tolerated as a
                // line terminator.
                case '\n':
                    if ( sbuf.length() == 0 ) {
                        return (String[])
                               lineList.toArray( new String[ 0 ] );
                    }
                    else {
                        lineList.add( sbuf.toString() );
                        sbuf.setLength( 0 );
                    }
                    break;

                default:
                   sbuf.append( (char) c );
            }
        }

        // No input.
        if ( ! hasChars ) {
            return null;
        }

        // Special case: can handle HTTP/0.9 type simple requests. 
        // Probably very rare.
        if ( lineList.size() == 1 ) {
            String line = (String) lineList.get( 0 );
            if ( SIMPLE_REQUEST_PATTERN.matcher( line ).matches() ) {
                return new String[] { line };
            }
        }

        // If it's got this far, there was no blank line.
        throw new HttpException( 500, "No CRLF line" );
    }