-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructPacker.java
142 lines (116 loc) · 3.87 KB
/
StructPacker.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package struct;
import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteOrder;
public class StructPacker extends StructOutput{
private ByteArrayOutputStream bos;
protected DataOutput dataOutput;
protected void init(OutputStream outStream, ByteOrder order) {
if (order == ByteOrder.LITTLE_ENDIAN) {
dataOutput = new LEDataOutputStream(outStream);
} else {
dataOutput = new DataOutputStream(outStream);
}
}
public StructPacker(){
this(new ByteArrayOutputStream(), ByteOrder.BIG_ENDIAN);
}
public StructPacker(ByteOrder order){
this(new ByteArrayOutputStream(), order);
}
public StructPacker(OutputStream os, ByteOrder order){
init(os, order);
bos = (ByteArrayOutputStream)os;
}
public byte[] pack(Object objectToPack) throws StructException {
writeObject(objectToPack);
return bos.toByteArray();
}
public void writeBoolean(boolean value) throws IOException {
dataOutput.writeBoolean(value);
}
public void writeByte(byte value) throws IOException {
dataOutput.writeByte(value);
}
public void writeShort(short value) throws IOException {
dataOutput.writeShort(value);
}
public void writeInt(int value) throws IOException {
dataOutput.writeInt(value);
}
public void writeLong(long value) throws IOException {
dataOutput.writeLong(value);
}
public void writeChar(char value) throws IOException {
dataOutput.writeChar(value);
}
public void writeFloat(float value) throws IOException {
dataOutput.writeFloat(value);
}
public void writeDouble(double value) throws IOException {
dataOutput.writeDouble(value);
}
public void writeBooleanArray(boolean buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeBoolean(buffer[i]);
}
public void writeByteArray(byte buffer[], int len) throws IOException {
if (len == 0) {
return;
}
if (len == -1 || len > buffer.length)
len = buffer.length;
dataOutput.write(buffer, 0, len);
}
public void writeCharArray(char buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeChar(buffer[i]);
}
public void writeShortArray(short buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeShort(buffer[i]);
}
public void writeIntArray(int buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeInt(buffer[i]);
}
public void writeLongArray(long buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeLong(buffer[i]);
}
public void writeFloatArray(float buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeFloat(buffer[i]);
}
public void writeDoubleArray(double buffer[], int len) throws IOException {
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
dataOutput.writeDouble(buffer[i]);
}
public void writeObjectArray(Object buffer[], int len) throws IOException,
IllegalAccessException, InvocationTargetException, StructException {
if (buffer == null || len == 0)
return;
if (len == -1 || len > buffer.length)
len = buffer.length;
for (int i = 0; i < len; i++)
writeObject(buffer[i]);
}
}