Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / www / protocol / http / icp / ICPReply.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/www/protocol/http/icp/ICPReply.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/www/protocol/http/icp/ICPReply.java
new file mode 100644 (file)
index 0000000..5e4a18d
--- /dev/null
@@ -0,0 +1,81 @@
+// ICPReply.java\r
+// $Id: ICPReply.java,v 1.1 2010/06/15 12:27:35 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// please first read the full copyright statement in file COPYRIGHT.HTML\r
+\r
+package org.w3c.www.protocol.http.icp;\r
+\r
+import java.net.InetAddress;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+class ICPReply extends ICPMessage {\r
+\r
+    protected int getByteArrayLength() {\r
+       return (super.getByteArrayLength()\r
+               + ((url == null) ? 0 : url.toExternalForm().length()) + 1);\r
+    }\r
+\r
+    protected int toByteArray(byte buf[]) {\r
+       int    off    = super.getByteArrayLength();\r
+       String strurl = (url == null) ? null : url.toExternalForm();\r
+       int    urllen = ((strurl == null) ? 0 : strurl.length());\r
+       // Encode generic payload infos:\r
+       super.toByteArray(buf);\r
+       // Encode the URL (null terminated string):\r
+       if ( urllen > 0 )\r
+           strurl.getBytes(0, urllen, buf, off);\r
+       buf[off+urllen] = 0;\r
+       return off+urllen+1;\r
+    }\r
+       \r
+    protected int parse(byte buf[], int off, int len)\r
+       throws ICPProtocolException\r
+    {\r
+       off = super.parse(buf, off, len);\r
+       // Read in the URL:\r
+       for (int i = off ; i < len ; i++) {\r
+           if (buf[i] == 0) {\r
+               // Is there a real URL ?\r
+               if ( i - off < 1 )\r
+                   return i;\r
+               // Yep, parse it:\r
+               String strurl = new String(buf, 0, off, i-off);\r
+               try {\r
+                   this.url = new URL(strurl);\r
+               } catch (MalformedURLException ex) {\r
+                   throw new ICPProtocolException("Invalid URL:"+strurl);\r
+               }\r
+               return i;\r
+           }\r
+       }\r
+       throw new ICPProtocolException("Invalid URL encoding");\r
+    }\r
+\r
+    /**\r
+     * Does this reply indicates a hit on the requested URL ?\r
+     * @return A boolean <strong>true</strong> if this reply was a hit.\r
+     */\r
+\r
+    public final boolean isHit() {\r
+       return (opcode == ICP_OP_HIT);\r
+    }\r
+\r
+    ICPReply(InetAddress addr, int port\r
+            , int opcode, int version\r
+            , byte buf[], int off, int len) \r
+       throws ICPProtocolException\r
+    {\r
+       this.addr    = addr;\r
+       this.port    = port;\r
+       this.opcode  = opcode;\r
+       this.version = version;\r
+       parse(buf, off, len);\r
+    }\r
+\r
+    ICPReply(int id, int opcode) {\r
+       this.id     = id;\r
+       this.opcode = opcode;\r
+    }\r
+\r
+}\r