-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructOutput.java
209 lines (168 loc) · 7.38 KB
/
StructOutput.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
206
207
208
209
package struct;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public abstract class StructOutput extends OutputStream {
@Override
public void write(int arg0) throws IOException {
}
public void writeObject(Object obj) throws StructException{
if(obj == null) throw new StructException("Struct classes cant be null. ");
StructData info = StructUtils.getStructInfo(obj);
boolean lengthedArray = false;
int arrayLength = 0;
for (Field currentField : info.getFields()) {
//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());
}
lengthedArray = false;
arrayLength = 0;
try{
if(fieldData.isArrayLengthMarker()){
if (fieldData.requiresGetterSetter()) {
arrayLength = ((Number)fieldData.getGetter().invoke( obj, (Object[])null)).intValue();
} else {
arrayLength = ((Number)fieldData.getField().get(obj)).intValue();
}
lengthedArray = true;
}
if ( fieldData.requiresGetterSetter()){
if(lengthedArray && arrayLength >= 0){
writeField(fieldData, fieldData.getGetter(), obj, arrayLength);
}
else writeField(fieldData, fieldData.getGetter(), obj, -1);
}
// Field is public. Access directly.
else {
if(lengthedArray && arrayLength >= 0){
writeField(fieldData, null, obj, arrayLength);
}
// Array is null if Length is negative.
else {
writeField(fieldData, null, obj, -1);
}
}
}
catch (Exception e) {
throw new StructException(e);
}
}
}
/**
* Write a fields value. Field can be an primitive, array or another object.
*/
public void writeField( StructFieldData fieldData, Method getter, Object obj, int len )
throws IllegalAccessException, IOException, InvocationTargetException, StructException {
Field field = fieldData.getField();
if ( !field.getType().isArray() )
{
switch(fieldData.getType()) {
case BOOLEAN:
if(getter != null) writeBoolean((Boolean)getter.invoke(obj, (Object[])null));
else writeBoolean(field.getBoolean(obj));
break;
case BYTE:
if(getter != null) writeByte((Byte)getter.invoke(obj, (Object[])null));
else writeByte(field.getByte(obj));
break;
case SHORT:
if(getter != null) writeShort((Short)getter.invoke(obj, (Object[])null));
else writeShort(field.getShort(obj));
break;
case INT:
if(getter != null) writeInt((Integer)getter.invoke(obj, (Object[])null));
else writeInt(field.getInt(obj));
break;
case LONG:
long longValue;
if(getter != null) longValue = (Long)getter.invoke(obj, (Object[])null);
else longValue = field.getLong(obj);
writeLong(longValue);
break;
case CHAR:
if(getter != null) writeChar((Character)getter.invoke(obj, (Object[])null));
else writeChar(field.getChar(obj));
break;
case FLOAT:
if(getter != null) writeFloat((Float)getter.invoke(obj, (Object[])null));
else writeFloat(field.getFloat(obj));
break;
case DOUBLE:
if(getter != null) writeDouble((Double)getter.invoke(obj, (Object[])null));
else writeDouble(field.getDouble(obj));
break;
// Object
default :
if(getter != null) handleObject(field, getter.invoke(obj, (Object[])null));
else handleObject(field, obj);
break;
}
} else {
switch(fieldData.getType()) {
case BOOLEAN:
if(getter != null) writeBooleanArray((boolean[])getter.invoke(obj, (Object[])null), len);
else writeBooleanArray((boolean[]) field.get(obj), len);
break;
case BYTE:
if(getter != null) writeByteArray((byte[])getter.invoke(obj, (Object[])null), len);
else writeByteArray((byte[]) field.get(obj), len);
break;
case CHAR:
if(getter != null) writeCharArray((char[])getter.invoke(obj, (Object[])null), len);
else writeCharArray((char[]) field.get(obj), len);
break;
case SHORT:
if(getter != null) writeShortArray((short[])getter.invoke(obj, (Object[])null), len);
else writeShortArray((short[]) field.get(obj), len);
break;
case INT:
if(getter != null) writeIntArray((int[])getter.invoke(obj, (Object[])null), len);
else writeIntArray((int[]) field.get(obj), len);
break;
case LONG:
if(getter != null) writeLongArray((long[])getter.invoke(obj, (Object[])null), len);
else writeLongArray((long[]) field.get(obj), len);
break;
case FLOAT:
if(getter != null) writeFloatArray((float[])getter.invoke(obj, (Object[])null), len);
else writeFloatArray((float[]) field.get(obj), len);
break;
case DOUBLE:
if(getter != null) writeDoubleArray((double[])getter.invoke(obj, (Object[])null), len);
else writeDoubleArray((double[]) field.get(obj), len);
break;
default:
if(getter != null) writeObjectArray((Object[])getter.invoke(obj, (Object[])null), len);
else writeObjectArray((Object[]) field.get(obj), len);
break;
}
}
}
public void handleObject(Field field, Object obj)
throws IllegalArgumentException, StructException,
IllegalAccessException, IOException {
writeObject(field.get(obj));
}
public abstract void writeBoolean(boolean value) throws IOException;
public abstract void writeByte(byte value) throws IOException ;
public abstract void writeShort(short value) throws IOException;
public abstract void writeInt(int value) throws IOException;
public abstract void writeLong(long value) throws IOException ;
public abstract void writeChar(char value) throws IOException ;
public abstract void writeFloat(float value) throws IOException;
public abstract void writeDouble(double value) throws IOException;
public abstract void writeBooleanArray(boolean buffer[], int len) throws IOException;
public abstract void writeByteArray(byte buffer[], int len) throws IOException ;
public abstract void writeCharArray(char buffer[], int len) throws IOException;
public abstract void writeShortArray(short buffer[], int len) throws IOException;
public abstract void writeIntArray(int buffer[], int len) throws IOException;
public abstract void writeLongArray(long buffer[], int len) throws IOException;
public abstract void writeFloatArray(float buffer[], int len) throws IOException;
public abstract void writeDoubleArray(double buffer[], int len) throws IOException ;
public abstract void writeObjectArray(Object buffer[], int len) throws IOException,
IllegalAccessException, InvocationTargetException, StructException;
}