-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructUnpacker.java
205 lines (174 loc) · 7.04 KB
/
StructUnpacker.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package struct;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.ByteOrder;
/**
* StructUnpacker is the default deserializer in Javastruct. It uses
* DataStreams (Both Big endian and Little endian)
*
* @author mdakin
*
*/
public class StructUnpacker extends StructInput {
DataInput dataInput;
protected void init( InputStream inStream, ByteOrder order) {
if ( order == ByteOrder.LITTLE_ENDIAN){
dataInput = new LEDataInputStream(inStream);
}
else {
dataInput = new DataInputStream( inStream );
}
}
public StructUnpacker(byte[] bufferToUnpack){
this(new ByteArrayInputStream(bufferToUnpack), ByteOrder.BIG_ENDIAN);
}
public StructUnpacker(byte[] bufferToUnpack, ByteOrder order){
this(new ByteArrayInputStream(bufferToUnpack), order);
}
public StructUnpacker(InputStream is, ByteOrder order){
init(is, order);
}
public void unpack(Object objectToUnpack) throws StructException{
readObject(objectToUnpack);
}
@Override
public void readObject( Object obj) throws StructException{
if(obj == null) throw new StructException("Struct objects cannot be null.");
StructData info = StructUtils.getStructInfo(obj);
Field[] fields = info.getFields();
for (Field currentField : fields) {
//System.out.println("Processing field: " + currentField.getName());
StructFieldData fieldData = info.getFieldData(currentField.getName());
if(fieldData == null) {
throw new StructException("Field Data not found for field: " + currentField.getName());
}
int arrayLength = -1;
boolean lengthedArray = false;
try{
if(info.isLenghtedArray(currentField)){
Field f = info.getLenghtedArray(currentField.getName());
StructFieldData lengthMarker = info.getFieldData(f.getName());
if (lengthMarker.requiresGetterSetter()) {
arrayLength = ((Number)lengthMarker.getGetter().invoke( obj, (Object[])null)).intValue();
} else {
arrayLength = ((Number)lengthMarker.getField().get(obj)).intValue();
}
lengthedArray = true;
}
// For private and protected fields, use getFieldName or isFieldName
if ( fieldData.requiresGetterSetter()){
Method getter = fieldData.getGetter();
Method setter = fieldData.getSetter();
if(getter == null || setter == null){
throw new StructException(" getter/setter required for : "+ currentField.getName());
}
if(lengthedArray && arrayLength >= 0){
Object ret = Array.newInstance(currentField.getType().getComponentType(),arrayLength);
setter.invoke(obj,new Object[]{ret});
if(currentField.getType().getComponentType().isPrimitive() == false){
Object[] array = (Object[])ret;
for(int j=0; j<arrayLength; j++){
array[j] = currentField.getType().getComponentType().newInstance();
}
}
}
if(lengthedArray == false && currentField.getType().isArray()){
if(getter.invoke (obj, (Object[])null) == null){
throw new StructException("Arrays can not be null :"+ currentField.getName());
}
}
readField(fieldData, getter, setter, obj);
}
// If public, use directly.
else {
if(lengthedArray && arrayLength >= 0) {
Object ret = Array.newInstance(currentField.getType().getComponentType(),arrayLength);
currentField.set(obj,ret);
if(currentField.getType().getComponentType().isPrimitive() == false){
Object[] array = (Object[])ret;
for(int j=0; j<arrayLength; j++){
array[j] = currentField.getType().getComponentType().newInstance();
}
}
}
if(lengthedArray == false && currentField.getType().isArray()){
if(currentField.get(obj)== null){
throw new StructException("Arrays can not be null. : "+ currentField.getName());
}
}
if(lengthedArray == false || (lengthedArray == true && arrayLength >= 0)){
readField(fieldData, null, null, obj);
}
}
}
catch (Exception e) {
throw new StructException(e);
}
}
}
protected boolean readBoolean() throws IOException {
return dataInput.readBoolean();
}
protected byte readByte() throws IOException {
return dataInput.readByte();
}
protected short readShort() throws IOException {
return dataInput.readShort();
}
protected int readInt() throws IOException {
return dataInput.readInt();
}
protected long readLong() throws IOException {
return dataInput.readLong();
}
protected char readChar() throws IOException {
return dataInput.readChar();
}
protected float readFloat() throws IOException {
return dataInput.readFloat();
}
protected double readDouble() throws IOException {
return dataInput.readDouble();
}
protected void readBooleanArray(boolean buffer[]) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readBoolean();
}
protected void readByteArray( byte buffer[] ) throws IOException {
dataInput.readFully(buffer);
}
protected void readCharArray( char buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readChar();
}
protected void readShortArray( short buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readShort();
}
protected void readIntArray( int buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readInt();
}
protected void readLongArray( long buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readLong();
}
protected void readFloatArray( float buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readFloat();
}
protected void readDoubleArray( double buffer[] ) throws IOException {
for ( int i=0; i<buffer.length; i++)
buffer[i] = readDouble();
}
protected void readObjectArray( Object objects[] ) throws IOException, StructException {
for ( int i=0; i<objects.length; i++)
readObject( objects[i] );
}
}