01: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
02:
03: package org.xbill.DNS;
04:
05: /**
06: * DNS Name Compression object.
07: * @see Message
08: * @see Name
09: *
10: * @author Brian Wellington
11: */
12:
13: public class Compression {
14:
15: private static class Entry {
16: Name name;
17: int pos;
18: Entry next;
19: }
20:
21: private static final int TABLE_SIZE = 17;
22: private Entry[] table;
23: private boolean verbose = Options.check("verbosecompression");
24:
25: /**
26: * Creates a new Compression object.
27: */
28: public Compression() {
29: table = new Entry[TABLE_SIZE];
30: }
31:
32: /**
33: * Adds a compression entry mapping a name to a position in a message.
34: * @param pos The position at which the name is added.
35: * @param name The name being added to the message.
36: */
37: public void add(int pos, Name name) {
38: int row = (name.hashCode() & 0x7FFFFFFF) % TABLE_SIZE;
39: Entry entry = new Entry();
40: entry.name = name;
41: entry.pos = pos;
42: entry.next = table[row];
43: table[row] = entry;
44: if (verbose)
45: System.err.println("Adding " + name + " at " + pos);
46: }
47:
48: /**
49: * Retrieves the position of the given name, if it has been previously
50: * included in the message.
51: * @param name The name to find in the compression table.
52: * @return The position of the name, or -1 if not found.
53: */
54: public int get(Name name) {
55: int row = (name.hashCode() & 0x7FFFFFFF) % TABLE_SIZE;
56: int pos = -1;
57: for (Entry entry = table[row]; entry != null; entry = entry.next) {
58: if (entry.name.equals(name))
59: pos = entry.pos;
60: }
61: if (verbose)
62: System.err
63: .println("Looking for " + name + ", found " + pos);
64: return pos;
65: }
66:
67: }
|