System.err.print("flush type="+to_flush); System.err.print(" header="+(_header==null?"null":(""+_header.length()))); System.err.println(" buffer="+(_buffer==null?"null":(""+_buffer.length())));
    public long flush() throws IOException    {        try        {               if (_state == STATE_HEADER)                 throw new IllegalStateException("State==HEADER");                        prepareBuffers();                        if (_endp == null)            {                if (_needEOC && _buffer != null)                     _buffer.put(END_PACKET);                return 0;            }                        // Keep flushing while there is something to flush (except break below)            int total= 0;            long last_len = -1;            Flushing: while (true)            {                int len = -1;                int to_flush = ((_header != null && _header.length() > 0)?4:0) | ((_buffer != null && _buffer.length() > 0)?2:0) ;                                System.err.print("flush type="+to_flush);                System.err.print(" header="+(_header==null?"null":(""+_header.length())));                System.err.println(" buffer="+(_buffer==null?"null":(""+_buffer.length())));                                switch (to_flush)                {                    case 7:                        throw new IllegalStateException(); // should never happen!                    case 6:                        len = _endp.flush(_header, _buffer, null);                        break;                    case 5:                        throw new IllegalStateException(); // should never happen!                    case 4:                        len = _endp.flush(_header);                        break;                    case 3:                        throw new IllegalStateException(); // should never happen!                    case 2:                        len = _endp.flush(_buffer);                        break;                    case 1:                        throw new IllegalStateException(); // should never happen!                    case 0:                    {                        // Nothing more we can write now.                        if (_header != null)                             _header.clear();                                                _bufferPrepared = false;                                                if (_buffer != null)                        {                            _buffer.clear();                                                        // reserve some space for the header                            _buffer.setPutIndex(7);                            _buffer.setGetIndex(7);                            // Special case handling for small left over buffer from                            // an addContent that caused a buffer flush.                            if (_content != null && _content.length() < _buffer.space() && _state != STATE_FLUSHING)                            {                                _buffer.put(_content);                                _content.clear();                                _content = null;                                break Flushing;                            }                                                    }                                                // Are we completely finished for now?                        if (!_needEOC && (_content == null || _content.length() == 0))                        {                            if (_state == STATE_FLUSHING)                                _state = STATE_END;                            if (_state==STATE_END && _close)                                 _endp.close();                                                        break Flushing;                        }                                                // Try to prepare more to write.                        prepareBuffers();                    }                }                                System.err.println("flushed "+len);                                // If we failed to flush anything twice in a row break                if (len <= 0)                {                    if (last_len <= 0)                         break Flushing;                    break;                }                last_len = len;                total+=len;            }                        return total;        }        catch (IOException e)        {            Log.ignore(e);            throw (e instanceof EofException) ? e:new EofException(e);        }    }