// TODO this could be optimized
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;
    }
buffer.put(readBuffer);
else {
// TODO this could be optimized
for (int i = 0; i < bufferRemaining; i++)
buffer.put(readBuffer.get());
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 {
// TODO this could be optimized
for (int i = 0; i < bufferRemaining; i++)
buffer.put(readBuffer.get());
}
return readBuffer.position() - startPos;
}