@@ -176,11 +176,15 @@ public class MemCachedClient {
176176 private static final String NOTSTORED = "NOT_STORED" ; // data not stored
177177 private static final String OK = "OK" ; // success
178178 private static final String END = "END" ; // end of data from server
179+
179180 private static final String ERROR = "ERROR" ; // invalid command name from client
180181 private static final String CLIENT_ERROR = "CLIENT_ERROR" ; // client error in input line - invalid protocol
181182 private static final String SERVER_ERROR = "SERVER_ERROR" ; // server error
182183
183- private static final byte [] THE_END = "END\r \n " .getBytes ();
184+ private static final byte [] B_END = "END\r \n " .getBytes ();
185+ private static final byte [] B_NOTFOUND = "NOT_FOUND\r \n " .getBytes ();
186+ private static final byte [] B_DELETED = "DELETED\r \r " .getBytes ();
187+ private static final byte [] B_STORED = "STORED\r \r " .getBytes ();
184188
185189 // default compression threshold
186190 private static final int COMPRESS_THRESH = 30720 ;
@@ -384,7 +388,7 @@ public void setCompressThreshold( long compressThreshold ) {
384388 public boolean keyExists ( String key ) {
385389 return ( this .get ( key , null , true ) != null );
386390 }
387-
391+
388392 /**
389393 * Deletes an object from cache given cache key.
390394 *
@@ -1266,15 +1270,15 @@ public Object get( String key, Integer hashCode, boolean asString ) {
12661270 new HashMap <String ,StringBuilder >();
12671271
12681272 cmdMap .put ( sock .getHost (),
1269- new StringBuilder ( String .format ( "get %s\r \n " , key ) ) );
1273+ new StringBuilder ( String .format ( "get %s" , key ) ) );
12701274
12711275 sock .close ();
12721276
12731277 // build empty map
12741278 // and fill it from server
12751279 Map <String ,Object > hm =
12761280 new HashMap <String ,Object >();
1277- (new NIOLoader ()).loadItemsNIO ( asString , cmdMap , new String [] { key }, hm );
1281+ (new NIOLoader ()).doMulti ( asString , cmdMap , new String [] { key }, hm );
12781282
12791283 // return the value for this key if we found it
12801284 // else return null
@@ -1440,7 +1444,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14401444 new HashMap <String ,Object >( keys .length );
14411445
14421446 // now use new NIO implementation
1443- (new NIOLoader ()).loadItemsNIO ( asString , cmdMap , keys , ret );
1447+ (new NIOLoader ()).doMulti ( asString , cmdMap , keys , ret );
14441448
14451449 // fix the return array in case we had to rewrite any of the keys
14461450 for ( String key : keys ) {
@@ -1484,7 +1488,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14841488 * @param asString if true, and if we are using NativehHandler, return string val
14851489 * @throws IOException if io exception happens while reading from socket
14861490 */
1487- private void loadItems ( LineInputStream input , Map <String ,Object > hm , boolean asString ) throws IOException {
1491+ private void loadMulti ( LineInputStream input , Map <String ,Object > hm , boolean asString ) throws IOException {
14881492
14891493 while ( true ) {
14901494 String line = input .readLine ();
@@ -1961,16 +1965,22 @@ public boolean isDone() {
19611965
19621966 // else find out the hard way
19631967 int maxBuf = incoming .size ()-1 ;
1964- int strPos = THE_END .length -1 ;
1968+ int strPos = B_END .length -1 ;
1969+
1970+ // Need to check if last bytes are:
1971+ // - END\r\n
1972+ // - NOT_FOUND\r\n
1973+ // - DELETED\r\n
19651974
19661975 int bi = maxBuf ;
19671976 while ( bi >= 0 && strPos >= 0 ) {
19681977 ByteBuffer buf = incoming .get ( bi );
19691978 int pos = buf .position ()-1 ;
1970- while ( pos >= 0 && strPos >= 0 ) {
1971- if ( buf .get ( pos -- ) != THE_END [strPos --] )
1979+ while ( pos >= 0 && strPos >= 0 ) {
1980+ if ( buf .get ( pos -- ) != B_END [strPos --] )
19721981 return false ;
19731982 }
1983+
19741984 bi --;
19751985 }
19761986
@@ -1995,7 +2005,7 @@ public String toString() {
19952005 }
19962006 }
19972007
1998- public void loadItemsNIO ( boolean asString , Map <String , StringBuilder > sockKeys , String [] keys , Map <String , Object > ret ) {
2008+ public void doMulti ( boolean asString , Map <String , StringBuilder > sockKeys , String [] keys , Map <String , Object > ret ) {
19992009
20002010 long timeRemaining = 0 ;
20012011 try {
@@ -2076,7 +2086,7 @@ public void loadItemsNIO( boolean asString, Map<String, StringBuilder> sockKeys,
20762086 for ( Connection c : conns ) {
20772087 try {
20782088 if ( c .incoming .size () > 0 && c .isDone () )
2079- loadItems ( new ByteBufArrayInputStream ( c .incoming ), ret , asString );
2089+ loadMulti ( new ByteBufArrayInputStream ( c .incoming ), ret , asString );
20802090 }
20812091 catch ( Exception e ) {
20822092 // shouldn't happen; we have all the data already
0 commit comments