hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1229203 - /httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
Date Mon, 09 Jan 2012 15:27:04 GMT
Author: jonm
Date: Mon Jan  9 15:27:04 2012
New Revision: 1229203

URL: http://svn.apache.org/viewvc?rev=1229203&view=rev
Log:
HTTPCLIENT-1152: Also apply type check before byte[] cast on memcached retrieval.

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java?rev=1229203&r1=1229202&r2=1229203&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
Mon Jan  9 15:27:04 2012
@@ -127,13 +127,25 @@ public class MemcachedHttpCacheStorage i
         client.set(url, 0, bos.toByteArray());
     }
 
-    public HttpCacheEntry getEntry(String url) throws IOException {
-        byte[] data = (byte[]) client.get(url);
-        if (null == data)
+    private byte[] convertToByteArray(Object o) {
+        if (o == null) return null;
+        if (!(o instanceof byte[])) {
+            log.warn("got a non-bytearray back from memcached: " + o);
             return null;
-        InputStream bis = new ByteArrayInputStream(data);
+        }
+        return (byte[])o;
+    }
+
+    private HttpCacheEntry reconstituteEntry(Object o) throws IOException {
+        byte[] out = convertToByteArray(o);
+        if (out == null) return null;
+        InputStream bis = new ByteArrayInputStream(out);
         return serializer.readFrom(bis);
     }
+    
+    public HttpCacheEntry getEntry(String url) throws IOException {
+        return reconstituteEntry(client.get(url));
+    }
 
     public void removeEntry(String url) throws IOException {
         client.delete(url);
@@ -145,19 +157,8 @@ public class MemcachedHttpCacheStorage i
         do {
 
             CASValue<Object> v = client.gets(url);
-            byte[] oldBytes = null;
-            if (v != null) {
-                if (v.getValue() instanceof byte[]) {
-                    oldBytes = (byte[])v.getValue();
-                } else {
-                    log.warn("got non-bytearray back from memcached");
-                }
-            }
-            HttpCacheEntry existingEntry = null;
-            if (oldBytes != null) {
-                ByteArrayInputStream bis = new ByteArrayInputStream(oldBytes);
-                existingEntry = serializer.readFrom(bis);
-            }
+            HttpCacheEntry existingEntry = (v == null) ? null
+                    : reconstituteEntry(v.getValue());
             HttpCacheEntry updatedEntry = callback.update(existingEntry);
 
             if (v == null) {



Mime
View raw message