--- /dev/null
+// PushCacheEntityResource.java\r
+// $Id: PushEntityCachedResource.java,v 1.1 2010/06/15 12:25:45 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 2001.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.www.protocol.http.cache.push;\r
+\r
+import org.w3c.www.protocol.http.Request;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+\r
+import org.w3c.www.protocol.http.cache.CachedResource;\r
+import org.w3c.www.protocol.http.cache.EntityCachedResource;\r
+\r
+/**\r
+ * PushEntityCachedResource\r
+ * EntityCachedResource that reads data from the file rather than attempting\r
+ * to use ActiveStream to tee output to a client that is not there.\r
+ *\r
+ * @author Paul Henshaw, The Fantastic Corporation, Paul.Henshaw@fantastic.com\r
+ * @version $Revision: 1.1 $\r
+ * $Id: PushEntityCachedResource.java,v 1.1 2010/06/15 12:25:45 smhuang Exp $\r
+ */\r
+public class PushEntityCachedResource extends EntityCachedResource {\r
+\r
+ /**\r
+ * This constructor required to handle startup when cache already\r
+ * contains PushEntityCachedResources\r
+ */\r
+ public PushEntityCachedResource() {\r
+ super();\r
+ }\r
+\r
+ /**\r
+ * Construct a PushEntityCachedResource \r
+ *\r
+ * Used by the PushCacheManager to actually store a PUSH resource.\r
+ * Note PushEntityCachedResource are used only when saving \r
+ * resources. When extracting resources from the cache, \r
+ * EntityCachedResources are used.\r
+ * \r
+ * @param filter the PushCacheFilter that in fact has not done \r
+ * anything yet, but which knows how to handle a \r
+ * PUSHed resource\r
+ * @param req the forged request for a URL\r
+ * @param rep the forged reply for the URL\r
+ */\r
+ protected PushEntityCachedResource(PushCacheFilter filter, Request req, \r
+ PushReply rep) {\r
+ try {\r
+ invalidated = false;\r
+ setValue(ATTR_IDENTIFIER, req.getURL().toExternalForm());\r
+\r
+ // Keep fast track of the filter:\r
+ this.filter = filter;\r
+\r
+ // update the headers\r
+ updateInfo(req, rep);\r
+\r
+ // and do some calculation according to the validator\r
+ filter.getValidator().updateExpirationInfo(this, req, rep);\r
+\r
+ // Save the content of resource into the content cache:\r
+ java.io.File outfile=filter.getPushCacheStore().getNewEntryFile();\r
+\r
+ FileOutputStream os=new FileOutputStream(outfile);\r
+ FileInputStream is=rep.getStream();\r
+\r
+ byte[] buffer=new byte[4096];\r
+ int wrote=0;\r
+ int wantedsize=rep.getContentLength();\r
+ int thistime;\r
+ while(wrote<wantedsize) {\r
+ thistime=Math.min(is.available(), 4096);\r
+ is.read(buffer,0,thistime);\r
+ os.write(buffer,0,thistime);\r
+ wrote+=thistime;\r
+ }\r
+\r
+ os.close();\r
+ setFile(outfile);\r
+ setLoadState(STATE_LOAD_COMPLETE);\r
+ setCurrentLength(wrote);\r
+ }\r
+ catch(Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+}\r