int length=0;
Buffer buf = buffer.buffer(); int len=0; if (buf instanceof NIOBuffer) { NIOBuffer nbuf = (NIOBuffer)buf; ByteBuffer bbuf=nbuf.getByteBuffer();
    public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException    {        int length=0;        Buffer buf0 = header==null?null:header.buffer();        Buffer buf1 = buffer==null?null:buffer.buffer();        Buffer buf2 = trailer==null?null:trailer.buffer();        if (_channel instanceof GatheringByteChannel &&            header!=null && header.length()!=0 && header instanceof NIOBuffer &&             buffer!=null && buffer.length()!=0 && buffer instanceof NIOBuffer)        {            NIOBuffer nbuf0 = (NIOBuffer)buf0;            NIOBuffer nbuf1 = (NIOBuffer)buf1;            NIOBuffer nbuf2 = buf2==null?null:(NIOBuffer)buf2;                        // Get the underlying NIO buffers            ByteBuffer bbuf0=nbuf0.getByteBuffer();            ByteBuffer bbuf1=nbuf1.getByteBuffer();            ByteBuffer bbuf2=nbuf2==null?null:nbuf2.getByteBuffer();                                    // We must sync because buffers may be shared (eg nbuf1 is likely to be cached content).            synchronized(nbuf0)            {                synchronized(nbuf1)                {                    try                    {                        // Adjust position indexs of buf0 and buf1                        bbuf0.position(header.getIndex());                        bbuf0.limit(header.putIndex());                        bbuf1.position(buffer.getIndex());                        bbuf1.limit(buffer.putIndex());                                                // if we don't have a buf2                        if (bbuf2==null)                        {                            synchronized(this)                            {                                // create a gether array for 2 buffers                                if (_gather2==null)                                    _gather2=new ByteBuffer[2];                                _gather2[0]=bbuf0;                                _gather2[1]=bbuf1;                                // do the gathering write.                                length=(int)((GatheringByteChannel)_channel).write(_gather2);                            }                        }                        else                        {                            // we have a third buffer, so sync on it as well                            synchronized(nbuf2)                            {                                try                                {                                    // Adjust position indexs of buf2                                    bbuf2.position(trailer.getIndex());                                    bbuf2.limit(trailer.putIndex());                                    synchronized(this)                                    {                                        // create a gether array for 3 buffers                                        if (_gather3==null)                                            _gather3=new ByteBuffer[3];                                        _gather3[0]=bbuf0;                                        _gather3[1]=bbuf1;                                        _gather3[2]=bbuf2;                                        // do the gathering write.                                        length=(int)((GatheringByteChannel)_channel).write(_gather3);                                    }                                }                                finally                                {                                    // adjust buffer 2.                                    if (!trailer.isImmutable())                                        trailer.setGetIndex(bbuf2.position());                                    bbuf2.position(0);                                    bbuf2.limit(bbuf2.capacity());                                }                            }                        }                    }                    finally                    {                        // adjust buffer 0 and 1                        if (!header.isImmutable())                            header.setGetIndex(bbuf0.position());                        if (!buffer.isImmutable())                            buffer.setGetIndex(bbuf1.position());                                               bbuf0.position(0);                        bbuf1.position(0);                        bbuf0.limit(bbuf0.capacity());                        bbuf1.limit(bbuf1.capacity());                    }                }            }        }        else        {            // TODO - consider copuing buffers buffer and trailer into header if there is space!                                    // flush header            if (header!=null && header.length()>0)                length=flush(header);                        // flush buffer            if ((header==null || header.length()==0) &&                            buffer!=null && buffer.length()>0)                length+=flush(buffer);                        // flush trailer            if ((header==null || header.length()==0) &&                            (buffer==null || buffer.length()==0) &&                            trailer!=null && trailer.length()>0)                length+=flush(trailer);        }                return length;    }