-
Notifications
You must be signed in to change notification settings - Fork 25
/
Address.java
85 lines (74 loc) · 2.39 KB
/
Address.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package avm;
/**
* Represents an address of account in the Aion Network.
* NOTE: This is just the API class. Runtime instance will be of type p.avm.Address if using the org.aion.avm.core implementation.
*/
public class Address {
/**
* The length of an address.
*/
public static final int LENGTH = 32;
private final byte[] raw = new byte[LENGTH];
/**
* Create an Address with the contents of the given raw byte array.
*
* @param raw a byte array
* @throws NullPointerException when the input byte array is null.
* @throws IllegalArgumentException when the input byte array length is invalid.
*/
public Address(byte[] raw) throws IllegalArgumentException {
if (raw == null) {
throw new NullPointerException();
}
if (raw.length != LENGTH) {
throw new IllegalArgumentException();
}
System.arraycopy(raw, 0, this.raw, 0, LENGTH);
}
/**
* Converts the receiver to a new byte array.
*
* @return a byte array containing a copy of the receiver.
*/
public byte[] toByteArray() {
byte[] copy = new byte[LENGTH];
System.arraycopy(this.raw, 0, copy, 0, LENGTH);
return copy;
}
@Override
public int hashCode() {
// Just a really basic implementation.
int code = 0;
for (byte elt : this.raw) {
code += (int) elt;
}
return code;
}
@Override
public boolean equals(Object obj) {
boolean isEqual = this == obj;
if (!isEqual && (obj instanceof Address)) {
Address other = (Address) obj;
isEqual = true;
for (int i = 0; isEqual && (i < LENGTH); ++i) {
isEqual = (this.raw[i] == other.raw[i]);
}
}
return isEqual;
}
@Override
public java.lang.String toString() {
return toHexStringForAPI(this.raw);
}
private static java.lang.String toHexStringForAPI(byte[] bytes) {
int length = bytes.length;
char[] hexChars = new char[length * 2];
for (int i = 0; i < length; i++) {
int v = bytes[i] & 0xFF;
hexChars[i * 2] = hexArray[v >>> 4];
hexChars[i * 2 + 1] = hexArray[v & 0x0F];
}
return new java.lang.String(hexChars);
}
private static final char[] hexArray = "0123456789abcdef".toCharArray();
}