EMMA Coverage Report (generated Tue Oct 27 11:32:50 PDT 2009)
[all classes][net.spy.memcached.protocol.ascii]

COVERAGE SUMMARY FOR SOURCE FILE [OperationImpl.java]

nameclass, %method, %block, %line, %
OperationImpl.java100% (1/1)100% (9/9)91%  (227/250)93%  (55.9/60)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class OperationImpl100% (1/1)100% (9/9)91%  (227/250)93%  (55.9/60)
classifyError (String): OperationErrorType 100% (1/1)75%  (18/24)75%  (6/8)
readFromBuffer (ByteBuffer): void 100% (1/1)84%  (81/96)92%  (22/24)
<static initializer> 100% (1/1)89%  (17/19)94%  (1.9/2)
OperationImpl (): void 100% (1/1)100% (14/14)100% (5/5)
OperationImpl (OperationCallback): void 100% (1/1)100% (17/17)100% (6/6)
getReadType (): OperationReadType 100% (1/1)100% (3/3)100% (1/1)
matchStatus (String, OperationStatus []): OperationStatus 100% (1/1)100% (35/35)100% (7/7)
setArguments (ByteBuffer, Object []): void 100% (1/1)100% (38/38)100% (8/8)
setReadType (OperationReadType): void 100% (1/1)100% (4/4)100% (2/2)

1// Copyright (c) 2006  Dustin Sallings <dustin@spy.net>
2 
3package net.spy.memcached.protocol.ascii;
4 
5import java.io.ByteArrayOutputStream;
6import java.io.IOException;
7import java.nio.ByteBuffer;
8 
9import net.spy.memcached.KeyUtil;
10import net.spy.memcached.ops.Operation;
11import net.spy.memcached.ops.OperationCallback;
12import net.spy.memcached.ops.OperationErrorType;
13import net.spy.memcached.ops.OperationState;
14import net.spy.memcached.ops.OperationStatus;
15import net.spy.memcached.protocol.BaseOperationImpl;
16 
17/**
18 * Operations on a memcached connection.
19 */
20abstract class OperationImpl extends BaseOperationImpl implements Operation {
21 
22        protected static final byte[] CRLF={'\r', '\n'};
23        private static final String CHARSET = "UTF-8";
24 
25        private final ByteArrayOutputStream byteBuffer=new ByteArrayOutputStream();
26        OperationReadType readType=OperationReadType.LINE;
27        boolean foundCr=false;
28 
29        protected OperationImpl() {
30                super();
31        }
32 
33        protected OperationImpl(OperationCallback cb) {
34                super();
35                callback=cb;
36        }
37 
38        /**
39         * Match the status line provided against one of the given
40         * OperationStatus objects.  If none match, return a failure status with
41         * the given line.
42         *
43         * @param line the current line
44         * @param statii several status objects
45         * @return the appropriate status object
46         */
47        protected final OperationStatus matchStatus(String line,
48                        OperationStatus... statii) {
49                OperationStatus rv=null;
50                for(OperationStatus status : statii) {
51                        if(line.equals(status.getMessage())) {
52                                rv=status;
53                        }
54                }
55                if(rv == null) {
56                        rv=new OperationStatus(false, line);
57                }
58                return rv;
59        }
60 
61        /* (non-Javadoc)
62         * @see net.spy.memcached.protocol.ascii.Operation#getReadType()
63         */
64        protected final OperationReadType getReadType() {
65                return readType;
66        }
67 
68        /**
69         * Set the read type of this operation.
70         */
71        protected final void setReadType(OperationReadType to) {
72                readType=to;
73        }
74 
75        /**
76         * Set some arguments for an operation into the given byte buffer.
77         */
78        protected final void setArguments(ByteBuffer bb, Object... args) {
79                boolean wasFirst=true;
80                for(Object o : args) {
81                        if(wasFirst) {
82                                wasFirst=false;
83                        } else {
84                                bb.put((byte)' ');
85                        }
86                        bb.put(KeyUtil.getKeyBytes(String.valueOf(o)));
87                }
88                bb.put(CRLF);
89        }
90 
91        OperationErrorType classifyError(String line) {
92                OperationErrorType rv=null;
93                if(line.startsWith("ERROR")) {
94                        rv=OperationErrorType.GENERAL;
95                } else if(line.startsWith("CLIENT_ERROR")) {
96                        rv=OperationErrorType.CLIENT;
97                } else if(line.startsWith("SERVER_ERROR")) {
98                        rv=OperationErrorType.SERVER;
99                }
100                return rv;
101        }
102 
103        @Override
104        public void readFromBuffer(ByteBuffer data) throws IOException {
105                // Loop while there's data remaining to get it all drained.
106                while(getState() != OperationState.COMPLETE && data.remaining() > 0) {
107                        if(readType == OperationReadType.DATA) {
108                                handleRead(data);
109                        } else {
110                                int offset=-1;
111                                for(int i=0; data.remaining() > 0; i++) {
112                                        byte b=data.get();
113                                        if(b == '\r') {
114                                                foundCr=true;
115                                        } else if(b == '\n') {
116                                                assert foundCr: "got a \\n without a \\r";
117                                                offset=i;
118                                                foundCr=false;
119                                                break;
120                                        } else {
121                                                assert !foundCr : "got a \\r without a \\n";
122                                                byteBuffer.write(b);
123                                        }
124                                }
125                                if(offset >= 0) {
126                                        String line=new String(byteBuffer.toByteArray(), CHARSET);
127                                        byteBuffer.reset();
128                                        OperationErrorType eType=classifyError(line);
129                                        if(eType != null) {
130                                                handleError(eType, line);
131                                        } else {
132                                                handleLine(line);
133                                        }
134                                }
135                        }
136                }
137        }
138 
139        /* (non-Javadoc)
140         * @see net.spy.memcached.protocol.ascii.Operation#handleLine(java.lang.String)
141         */
142        public abstract void handleLine(String line);
143}

[all classes][net.spy.memcached.protocol.ascii]
EMMA 2.0.5312 (C) Vladimir Roubtsov