abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r429386 - in /incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol: cache/InMemoryCachedResponse.java util/CachingInputStream.java
Date Mon, 07 Aug 2006 16:46:11 GMT
Author: jmsnell
Date: Mon Aug  7 09:46:11 2006
New Revision: 429386

URL: http://svn.apache.org/viewvc?rev=429386&view=rev
Log:
Fixing up the in-memory buffer.  I had originally been experimenting with a buffer that would
be incrementally filled as 
the original request consumed the stream.  Unfortunately, this doesn't work because of the
way the Stax parser incrementally
consumes the stream.  If a second request against the cache was executed before the first
request completely processing, the
second would either block until the first completed or would end up overrunning the cached
buffer.  Not good.  So, for now, 
we'll consume and buffer the stream immediately upon creating the in-memory cached response,
and from that point on simply serve
out ByteArrayInputStreams.

Removed:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CachingInputStream.java
Modified:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/cache/InMemoryCachedResponse.java

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/cache/InMemoryCachedResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/cache/InMemoryCachedResponse.java?rev=429386&r1=429385&r2=429386&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/cache/InMemoryCachedResponse.java
(original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/cache/InMemoryCachedResponse.java
Mon Aug  7 09:46:11 2006
@@ -27,7 +27,6 @@
 import java.util.Map;
 
 import org.apache.abdera.protocol.client.Response;
-import org.apache.abdera.protocol.util.CachingInputStream;
 
 public class InMemoryCachedResponse 
   extends CachedResponseBase
@@ -37,8 +36,7 @@
   private String status_text = null;
   private String uri = null;
   private Map<String,List<String>> headers = null;
-  private ByteArrayOutputStream out;
-  private InputStream in = null;
+  private byte[] buf = null;
   
   public InMemoryCachedResponse(
     Cache cache,
@@ -59,11 +57,26 @@
     }
     getServerDate();
     getInitialAge();
-    out = new ByteArrayOutputStream();
-    in = new CachingInputStream(response.getInputStream(),out,true);
-    response.setInputStream(in);
+    cacheStream(response.getInputStream());
+    response.setInputStream(getInputStream());
   }
 
+  /**
+   * This is terribly inefficient, but it is an in-memory cache
+   * that is being used by parsers that incrementally consume 
+   * InputStreams at different rates.  There's really no other
+   * way to do it.
+   */
+  private void cacheStream(InputStream in) throws IOException {
+    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
+    byte[] buf = new byte[1024];
+    int m = -1;
+    while((m = in.read(buf)) != -1) {
+      out.write(buf,0,m);
+    }
+    this.buf = buf;
+  }
+  
   private Map<String,List<String>> getHeaders() {
     if (headers == null)
       headers = new HashMap<String,List<String>>();
@@ -118,7 +131,7 @@
 
   @Override
   public InputStream getInputStream() throws IOException {
-    return new ByteArrayInputStream(out.toByteArray());
+    return new ByteArrayInputStream(this.buf);
   }
 
   @Override



Mime
View raw message