public int read(ByteBuffer buffer) {
        if (disconnected)
            return -1;
        int startPos = readBuffer.position();
        int bufferRemaining = buffer.remaining();
        int readBufferRemaining = readBuffer.remaining();
        if (bufferRemaining >= readBufferRemaining)
            buffer.put(readBuffer);
        else {
            // TODO this could be optimized
            for (int i = 0; i < bufferRemaining; i++)
                buffer.put(readBuffer.get());
        }
        return readBuffer.position() - startPos;
    }
public int read(ByteBuffer buffer) {
        if (disconnected)
            return -1;
        int startPos = readBuffer.position();
        int bufferRemaining = buffer.remaining();
        int readBufferRemaining = readBuffer.remaining();
        if (bufferRemaining >= readBufferRemaining)
            buffer.put(readBuffer);
        else {
           
            for (int i = 0; i < bufferRemaining; i++)
                buffer.put(readBuffer.get());
        }
        return readBuffer.position() - startPos;
    }
public int read(bytebuffer buffer) { if (disconnected) return -1; int startpos = readbuffer.position(); int bufferremaining = buffer.remaining(); int readbufferremaining = readbuffer.remaining(); if (bufferremaining >= readbufferremaining) buffer.put(readbuffer); else { for (int i = 0; i < bufferremaining; i++) buffer.put(readbuffer.get()); } return readbuffer.position() - startpos; }