-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAShortUInt.java
84 lines (65 loc) · 1.86 KB
/
AShortUInt.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
//Class represeting a type in AMQP, such as long-string, short-string, long, long-long etc
//Inherited by other subclasses
public class AShortUInt extends AMQPNativeType {
long value;
//Constructor
AShortUInt(ByteArrayBuffer byteArrayBuffer) throws InvalidTypeException {
if (byteArrayBuffer.length() < 2) throw new InvalidTypeException("Invalid type length");
this.type = AMQPNativeType.Type.LONG_UINT;
this.buffer = byteArrayBuffer.pop(2);
//Convert data from wire to value
this.value = ByteArrayBuffer.bytesToLong(this.buffer.get());
}
AShortUInt(long value) {
this.type = AMQPNativeType.Type.SHORT_UINT;
this.value = value;
}
public long toLong() {
return value;
}
public String toString() {
return "" + value;
}
//Encode data type to wire
public ByteArrayBuffer toWire() {
//A long is 8 bytes, cut the 6 MSB
//Make a new buffer
ByteArrayBuffer ret = new ByteArrayBuffer(ByteArrayBuffer.longToBytes(value));
//Cut 6 bytes
ret.pop(6);
//Return the remaining value
return ret;
}
//Get value as a boolean array
public String toFlagString() {
//To be returned
String ret = Long.toBinaryString(value);
//Make sure we build the string so that it is exacly 16 chars long
while (ret.length() < 16) {
ret = "0" + ret;
}
return ret;
}
//Get value as a boolean array
public boolean[] getFlags() {
//Return value
boolean[] ret = new boolean[16];
byte[] tmp = toFlagString().getBytes();
for(int i=0; i!=16; ++i) {
ret[i] = (tmp[15-i] == '1');
}
return ret;
}
public boolean equals(AShortUInt other) {
return (this.value == other.value);
}
//Get a specific bit mask flag
//0 = LSB
//15 = MSB
public boolean getFlag(int flag) {
return getFlags()[flag];
}
public int toInt() {
return (int) value;
}
};