01: // ICPQuery.java
02: // $Id: ICPQuery.java,v 1.4 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 ICPQuery extends ICPMessage {
13:
14: protected int getByteArrayLength() {
15: return (super .getByteArrayLength() + 4 // sender's address
16: + 4 // requestor's adress
17: + ((url == null) ? 0 : url.toExternalForm().length()) + 1);
18: }
19:
20: protected int toByteArray(byte buf[]) {
21: int off = super .getByteArrayLength();
22: String strurl = (url == null) ? null : url.toExternalForm();
23: int urllen = ((strurl == null) ? 0 : strurl.length());
24: // Encode generic payload infos:
25: super .toByteArray(buf);
26: // Encode query specific fields:
27: // Skip the sender's address:
28: off += 4;
29: // Skip the requestor's address
30: off += 4;
31: // Encode the URL (null terminated string):
32: if (urllen > 0)
33: strurl.getBytes(0, urllen, buf, off);
34: buf[off + urllen] = 0;
35: return off + urllen + 1;
36: }
37:
38: protected int parse(byte buf[], int off, int len)
39: throws ICPProtocolException {
40: off = super .parse(buf, off, len);
41: // Skip sender's address:
42: off += 4;
43: // Skip the requestor's host address:
44: off += 4;
45: // Read in the URL:
46: for (int i = off; i < len; i++) {
47: if (buf[i] == 0) {
48: // Is there a real URL ?
49: if (i - off < 1)
50: return i;
51: // Yep, parse it:
52: String strurl = new String(buf, 0, off, i - off);
53: try {
54: this .url = new URL(strurl);
55: } catch (MalformedURLException ex) {
56: throw new ICPProtocolException("Invalid URL:"
57: + strurl);
58: }
59: return i;
60: }
61: }
62: throw new ICPProtocolException("Invalid URL encoding");
63: }
64:
65: /**
66: * Create an ICP query.
67: * @param id The identifier of the message.
68: * @param url The queried URL.
69: */
70:
71: ICPQuery(int id, URL url) {
72: this .opcode = ICP_OP_QUERY;
73: this .id = id;
74: this .url = url;
75: }
76:
77: ICPQuery(InetAddress addr, int port, int opcode, int version,
78: byte buf[], int off, int len) throws ICPProtocolException {
79: this.addr = addr;
80: this.port = port;
81: this.opcode = opcode;
82: this.version = version;
83: parse(buf, off, len);
84: }
85:
86: }
|