public int read(ByteBuffer dst) throws IOException {
        int initialDstPosition = dst.position();
        // Read data from |readableByteChannel| into |buffer|.
        buffer.limit(buffer.capacity());
        buffer.position(nextBufferWritePosition);
        if (buffer.capacity() > 0) {
            readableByteChannel.read(buffer);
            nextBufferWritePosition = buffer.position();
        }

        // Read data from |buffer| into |dst|.
        buffer.position(nextBufferReadPosition);
        buffer.limit(nextBufferWritePosition);
        if (buffer.remaining() > dst.remaining()) {
            buffer.limit(buffer.position() + dst.remaining());
        }
        dst.put(buffer);
        nextBufferReadPosition = buffer.position();

        // If |dst| still has capacity then read data from |readableByteChannel|.
        int bytesRead = readableByteChannel.read(dst);
        if (bytesRead > 0) {
            // We passed the buffering capacity. It will not be possible to rewind
            // |readableByteChannel| anymore.
            passedRewindPoint = true;
        } else if ((bytesRead == -1) && (dst.position() - initialDstPosition == 0)) {
            return -1;
        }
        return dst.position() - initialDstPosition;
    }
public int read ( ByteBuffer dst ) throws IOException { int initialDstPosition = dst . position ( ) ; buffer . limit ( buffer . capacity ( ) ) ; buffer . position ( nextBufferWritePosition ) ; if ( buffer . capacity ( ) > 0 ) { readableByteChannel . read ( buffer ) ; nextBufferWritePosition = buffer . position ( ) ; } buffer . position ( nextBufferReadPosition ) ; buffer . limit ( nextBufferWritePosition ) ; if ( buffer . remaining ( ) > dst . remaining ( ) ) { buffer . limit ( buffer . position ( ) + dst . remaining ( ) ) ; } dst . put ( buffer ) ; nextBufferReadPosition = buffer . position ( ) ; int bytesRead = readableByteChannel . read ( dst ) ; if ( bytesRead > 0 ) { passedRewindPoint = true ; } else if ( ( bytesRead == - 1 ) && ( dst . position ( ) - initialDstPosition == 0 ) ) { return - 1 ; } return dst . position ( ) - initialDstPosition ; }
@see ReadableByteChannel#read(ByteBuffer)