public void handleRead()

in evcache-core/src/main/java/net/spy/memcached/protocol/ascii/MetaGetOperationImpl.java [60:112]


    public void handleRead(ByteBuffer b) {
        if(log.isDebugEnabled()) log.debug("readOffset: {}, length: {}", readOffset, data.length);
        // If we're not looking for termination, we're still looking for data
        if (lookingFor == '\0') {
            int toRead = data.length - readOffset;
            int available = b.remaining();
            toRead = Math.min(toRead, available);
            if(log.isDebugEnabled()) log.debug("Reading {} bytes", toRead);
            b.get(data, readOffset, toRead);
            readOffset += toRead;
        }
        // Transition us into a ``looking for \r\n'' kind of state if we've
        // read enough and are still in a data state.
        if (readOffset == data.length && lookingFor == '\0') {
            // The callback is most likely a get callback. If it's not, then
            // it's a gets callback.
            OperationCallback cb = getCallback();
            if (cb instanceof MetaGetOperation.Callback) {
                MetaGetOperation.Callback mgcb = (MetaGetOperation.Callback) cb;
                mgcb.gotData(key, currentFlag, data);
            }
            lookingFor = '\r';
        }
        // If we're looking for an ending byte, let's go find it.
        if (lookingFor != '\0' && b.hasRemaining()) {
            do {
                byte tmp = b.get();
                assert tmp == lookingFor : "Expecting " + lookingFor + ", got "
                        + (char) tmp;
                switch (lookingFor) {
                case '\r':
                    lookingFor = '\n';
                    
                    break;
                case '\n':
                    lookingFor = '\0';
                    break;
                default:
                    assert false : "Looking for unexpected char: " + (char) lookingFor;
                }
            } while (lookingFor != '\0' && b.hasRemaining());
            // Completed the read, reset stuff.
            if (lookingFor == '\0') {
                data = null;
                readOffset = 0;
                currentFlag = -1;
                getCallback().receivedStatus(END);
                transitionState(OperationState.COMPLETE);
                getLogger().debug("Setting read type back to line.");
                setReadType(OperationReadType.LINE);
            }
        }
    }