-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAShortString.java
More file actions
50 lines (40 loc) · 1.52 KB
/
AShortString.java
File metadata and controls
50 lines (40 loc) · 1.52 KB
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
import java.util.*;
//Class represeting a type in AMQP, such as long-string, short-string, long, long-long etc
//Inherited by other subclasses
public class AShortString extends AMQPNativeType {
//Constructor
AShortString(ByteArrayBuffer byteArrayBuffer) throws InvalidTypeException {
this.type = AMQPNativeType.Type.SHORT_STRING;
AOctet length = new AOctet(byteArrayBuffer);
if (length.toInt() > byteArrayBuffer.length()) {
throw new InvalidTypeException("Specified short string length is longer than existing buffer");
}
this.buffer = byteArrayBuffer.pop(length.toInt());
}
//Hack just to get another constructor from which we can create short strings
//internally, since we want to be able to create a string directly from a
//ByteArrayBuffer without specifying the length
AShortString(int length, ByteArrayBuffer byteArrayBuffer) {
this.type = AMQPNativeType.Type.SHORT_STRING;
this.buffer = byteArrayBuffer;
}
//Constructor from string
AShortString(String value) {
this.type = AMQPNativeType.Type.SHORT_STRING;
this.buffer = new ByteArrayBuffer(value);
}
//Encode data type to wire
public ByteArrayBuffer toWire() {
AOctet len = new AOctet(buffer.length());
//Return length (1 octet) + the string itself
ByteArrayBuffer ret = len.toWire();
ret.put(buffer);
return ret;
}
public boolean equals(AShortString other) {
return Arrays.equals(this.buffer.buffer, other.buffer.buffer);
}
public String toString() {
return buffer.toString();
}
};