01: /*
02: * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
03: * California. All rights reserved.
04: *
05: * Permission to use, copy, modify, and distribute this software and its
06: * documentation for any purpose, without fee, and without written agreement is
07: * hereby granted, provided that the above copyright notice and the following
08: * two paragraphs appear in all copies of this software.
09: *
10: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14: *
15: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20: *
21: * Author: Matt Welsh <mdw@cs.berkeley.edu>
22: *
23: */
24:
25: package seda.sandStorm.lib.Gnutella;
26:
27: import java.util.*;
28: import java.io.*;
29: import java.net.*;
30:
31: /**
32: * This class represents the GUID contained in Gnutella network packets.
33: */
34: public class GnutellaGUID implements GnutellaConst {
35:
36: private static final boolean DEBUG = false;
37:
38: byte data[];
39: private int hash;
40:
41: private static Random rand = null;
42:
43: public GnutellaGUID() {
44: if (rand == null)
45: rand = new Random();
46: data = new byte[16];
47: rand.nextBytes(data);
48: hash = GnutellaPacket.readLEInt(data, 0);
49: }
50:
51: public GnutellaGUID(byte barr[], int offset) {
52: data = new byte[16];
53: System.arraycopy(barr, offset, data, 0, 16);
54: hash = GnutellaPacket.readLEInt(data, 0);
55: }
56:
57: public void dump(byte barr[], int offset) {
58: System.arraycopy(data, 0, barr, offset, 16);
59: }
60:
61: public String toString() {
62: String s = "[GUID ";
63: for (int i = 0; i < 16; i++) {
64: int c = data[i] & 0xff;
65: String s1 = Integer.toHexString(c);
66: if (c < 0x10)
67: s += "0" + s1;
68: else
69: s += s1;
70:
71: }
72: s += "]";
73: return s;
74: }
75:
76: public int hashCode() {
77: return hash;
78: }
79:
80: public boolean equals(Object o) {
81: if (!(o instanceof GnutellaGUID))
82: return false;
83: GnutellaGUID guid = (GnutellaGUID) o;
84: boolean same = true;
85: for (int i = 0; i < 16; i++) {
86: if (guid.data[i] != data[i])
87: same = false;
88: }
89: return same;
90: }
91:
92: }
|