01: // ICPReply.java
02: // $Id: ICPReply.java,v 1.3 2000/08/16 21:38:04 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // please first read the full copyright statement in file COPYRIGHT.HTML
05:
06: package org.w3c.www.protocol.http.icp;
07:
08: import java.net.InetAddress;
09: import java.net.MalformedURLException;
10: import java.net.URL;
11:
12: class ICPReply extends ICPMessage {
13:
14: protected int getByteArrayLength() {
15: return (super .getByteArrayLength()
16: + ((url == null) ? 0 : url.toExternalForm().length()) + 1);
17: }
18:
19: protected int toByteArray(byte buf[]) {
20: int off = super .getByteArrayLength();
21: String strurl = (url == null) ? null : url.toExternalForm();
22: int urllen = ((strurl == null) ? 0 : strurl.length());
23: // Encode generic payload infos:
24: super .toByteArray(buf);
25: // Encode the URL (null terminated string):
26: if (urllen > 0)
27: strurl.getBytes(0, urllen, buf, off);
28: buf[off + urllen] = 0;
29: return off + urllen + 1;
30: }
31:
32: protected int parse(byte buf[], int off, int len)
33: throws ICPProtocolException {
34: off = super .parse(buf, off, len);
35: // Read in the URL:
36: for (int i = off; i < len; i++) {
37: if (buf[i] == 0) {
38: // Is there a real URL ?
39: if (i - off < 1)
40: return i;
41: // Yep, parse it:
42: String strurl = new String(buf, 0, off, i - off);
43: try {
44: this .url = new URL(strurl);
45: } catch (MalformedURLException ex) {
46: throw new ICPProtocolException("Invalid URL:"
47: + strurl);
48: }
49: return i;
50: }
51: }
52: throw new ICPProtocolException("Invalid URL encoding");
53: }
54:
55: /**
56: * Does this reply indicates a hit on the requested URL ?
57: * @return A boolean <strong>true</strong> if this reply was a hit.
58: */
59:
60: public final boolean isHit() {
61: return (opcode == ICP_OP_HIT);
62: }
63:
64: ICPReply(InetAddress addr, int port, int opcode, int version,
65: byte buf[], int off, int len) throws ICPProtocolException {
66: this .addr = addr;
67: this .port = port;
68: this .opcode = opcode;
69: this .version = version;
70: parse(buf, off, len);
71: }
72:
73: ICPReply(int id, int opcode) {
74: this.id = id;
75: this.opcode = opcode;
76: }
77:
78: }
|