Lines 49-54
Link Here
|
49 |
private static final int T_SHORT = 15; |
49 |
private static final int T_SHORT = 15; |
50 |
private static final int T_BYTE = 16; |
50 |
private static final int T_BYTE = 16; |
51 |
private static final int T_OBJECT = 17; |
51 |
private static final int T_OBJECT = 17; |
|
|
52 |
private static final int T_LONG = 18; |
52 |
|
53 |
|
53 |
private static final String ENUMIMPL_SUFFIX = "Enum"; |
54 |
private static final String ENUMIMPL_SUFFIX = "Enum"; |
54 |
|
55 |
|
Lines 60-97
Link Here
|
60 |
write(outputStream, object, null); |
61 |
write(outputStream, object, null); |
61 |
} |
62 |
} |
62 |
|
63 |
|
|
|
64 |
/** |
65 |
* Writes an integer to the output stream. It tries to optimize for space a little: |
66 |
* small values are stored just as one byte. Values <= 0xffff are stored as |
67 |
* 3 bytes, little-endian and other values as 5 bytes, little-endian |
68 |
*/ |
63 |
public static void writeInt(OutputStream outputStream, int val) throws IOException { |
69 |
public static void writeInt(OutputStream outputStream, int val) throws IOException { |
64 |
if ((int) ((byte) val & 0xFF) == val) { |
70 |
if ((int) ((byte) val & 0x7F) == val) { |
65 |
outputStream.write(T_BYTE); |
|
|
66 |
outputStream.write((byte)val); |
71 |
outputStream.write((byte)val); |
67 |
} else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) { |
72 |
} else if ((int) ((byte) (val >>> 8) & 0xFF) == val >>> 8) { |
68 |
outputStream.write(T_SHORT); |
73 |
outputStream.write(T_SHORT | 0x80); |
69 |
outputStream.write((byte)(val & 0xff)); |
74 |
outputStream.write((byte)(val & 0xff)); |
70 |
outputStream.write((byte)((val & 0xff00) >> 8)); |
75 |
outputStream.write((byte)((val & 0xff00) >>> 8)); |
71 |
} else { |
76 |
} else { |
72 |
outputStream.write(T_INTEGER); |
77 |
outputStream.write(T_INTEGER | 0x80); |
73 |
outputStream.write((byte)(val & 0xff)); |
78 |
outputStream.write((byte)(val & 0xff)); |
74 |
outputStream.write((byte)((val & 0xff00) >> 8)); |
79 |
outputStream.write((byte)((val & 0xff00) >>> 8)); |
75 |
outputStream.write((byte)((val & 0xff0000) >> 16)); |
80 |
outputStream.write((byte)((val & 0xff0000) >>> 16)); |
76 |
val = (val >> 24) & 0xff; |
81 |
val = (val >> 24) & 0xff; |
77 |
outputStream.write((byte)val); |
82 |
outputStream.write((byte)val); |
78 |
} |
83 |
} |
79 |
} |
84 |
} |
80 |
|
85 |
|
|
|
86 |
public static void writeLong(OutputStream outputStream, long val) throws IOException { |
87 |
if ((int) ((byte) val & 0x7F) == val) { |
88 |
outputStream.write((byte)val); |
89 |
} else if ((int) ((byte) (val >>> 24) & 0xFF) == val >>> 24) { |
90 |
outputStream.write(T_INTEGER | 0x80); |
91 |
outputStream.write((byte)(val & 0xff)); |
92 |
outputStream.write((byte)((val & 0xff00) >>> 8)); |
93 |
outputStream.write((byte)((val & 0xff0000) >>> 16)); |
94 |
outputStream.write((byte)((val >>> 24) & 0xff)); |
95 |
} else { |
96 |
outputStream.write(T_LONG | 0x80); |
97 |
outputStream.write((byte)(val & 0xff)); |
98 |
outputStream.write((byte)((val & 0xff00) >>> 8)); |
99 |
outputStream.write((byte)((val >>> 16) & 0xff)); |
100 |
outputStream.write((byte)((val >>> 24) & 0xff)); |
101 |
outputStream.write((byte)((val >>> 32) & 0xff)); |
102 |
outputStream.write((byte)((val >>> 48) & 0xff)); |
103 |
outputStream.write((byte)((val >>> 56) & 0xff)); |
104 |
} |
105 |
} |
106 |
|
81 |
public static void writeBoolean(OutputStream outputStream, boolean val) throws IOException { |
107 |
public static void writeBoolean(OutputStream outputStream, boolean val) throws IOException { |
82 |
outputStream.write(T_BOOLEAN); |
|
|
83 |
outputStream.write(val ? 1 : 0); |
108 |
outputStream.write(val ? 1 : 0); |
84 |
} |
109 |
} |
85 |
|
110 |
|
86 |
public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException { |
111 |
public static void writeString(OutputStream outputStream, String val) throws IOException { |
87 |
if (object == null) { |
112 |
if (val == null) { |
88 |
outputStream.write(T_NULL); |
113 |
writeInt(outputStream, 0); |
89 |
|
114 |
return; |
90 |
} else if (object instanceof String) { |
115 |
} |
91 |
String val = (String) object; |
|
|
92 |
outputStream.write(T_STRING); |
93 |
int len = val.length(); |
116 |
int len = val.length(); |
94 |
writeInt(outputStream, len); |
117 |
writeInt(outputStream, len + 1); |
95 |
for (int i = 0; i < len; i++) { |
118 |
for (int i = 0; i < len; i++) { |
96 |
char ch = val.charAt(i); |
119 |
char ch = val.charAt(i); |
97 |
if (ch <= 0x7f) { |
120 |
if (ch <= 0x7f) { |
Lines 105-114
Link Here
|
105 |
outputStream.write((byte) (0x80 | (ch & 0x3f))); |
128 |
outputStream.write((byte) (0x80 | (ch & 0x3f))); |
106 |
} |
129 |
} |
107 |
} |
130 |
} |
|
|
131 |
} |
132 |
|
133 |
public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException { |
134 |
if (object == null) { |
135 |
outputStream.write(T_NULL); |
136 |
|
137 |
} else if (object instanceof String) { |
138 |
outputStream.write(T_STRING); |
139 |
writeString(outputStream, (String)object); |
108 |
} else if (object instanceof Integer) { |
140 |
} else if (object instanceof Integer) { |
109 |
int val = ((Integer)object).intValue(); |
141 |
int val = ((Integer)object).intValue(); |
|
|
142 |
outputStream.write(T_INTEGER); |
110 |
writeInt(outputStream, val); |
143 |
writeInt(outputStream, val); |
111 |
} else if (object instanceof Boolean) { |
144 |
} else if (object instanceof Boolean) { |
|
|
145 |
outputStream.write(T_BOOLEAN); |
112 |
writeBoolean(outputStream, ((Boolean) object).booleanValue()); |
146 |
writeBoolean(outputStream, ((Boolean) object).booleanValue()); |
113 |
|
147 |
|
114 |
} else if (object instanceof Map) { |
148 |
} else if (object instanceof Map) { |
Lines 160-166
Link Here
|
160 |
writeBoolean(outputStream, desc.isUnique()); |
194 |
writeBoolean(outputStream, desc.isUnique()); |
161 |
writeBoolean(outputStream, desc.isChangeable()); |
195 |
writeBoolean(outputStream, desc.isChangeable()); |
162 |
writeBoolean(outputStream, desc.isIndexed()); |
196 |
writeBoolean(outputStream, desc.isIndexed()); |
163 |
|
197 |
writeInt(outputStream, desc.getSerialCode()); |
164 |
} else if (object instanceof StorableClass.ReferenceDescriptor) { |
198 |
} else if (object instanceof StorableClass.ReferenceDescriptor) { |
165 |
outputStream.write(T_MOF_REFERENCE); |
199 |
outputStream.write(T_MOF_REFERENCE); |
166 |
write(outputStream, ((StorableClass.ReferenceDescriptor) object).getAssociationId()); |
200 |
write(outputStream, ((StorableClass.ReferenceDescriptor) object).getAssociationId()); |
Lines 193-199
Link Here
|
193 |
} |
227 |
} |
194 |
|
228 |
|
195 |
public static boolean readBoolean(InputStream is) throws IOException { |
229 |
public static boolean readBoolean(InputStream is) throws IOException { |
196 |
if (is.read() != T_BOOLEAN) throw new DebugException("Not a boolean value."); |
|
|
197 |
return (is.read() == 1); |
230 |
return (is.read() == 1); |
198 |
} |
231 |
} |
199 |
|
232 |
|
Lines 201-218
Link Here
|
201 |
return readInt(is, is.read()); |
234 |
return readInt(is, is.read()); |
202 |
} |
235 |
} |
203 |
|
236 |
|
204 |
public static int readInt(InputStream inputStream, int type) throws IOException { |
237 |
static int readInt(InputStream inputStream, int type) throws IOException { |
|
|
238 |
if (type <= 0x7f) |
239 |
return type; |
205 |
switch (type) { |
240 |
switch (type) { |
206 |
case T_BYTE: { |
241 |
case T_SHORT | 0x80: { |
207 |
return inputStream.read(); |
|
|
208 |
} |
209 |
case T_SHORT: { |
210 |
int ch1, ch2; |
242 |
int ch1, ch2; |
211 |
ch1 = inputStream.read(); |
243 |
ch1 = inputStream.read(); |
212 |
ch2 = inputStream.read(); |
244 |
ch2 = inputStream.read(); |
213 |
return (ch2 << 8) | ch1; |
245 |
return (ch2 << 8) | ch1; |
214 |
} |
246 |
} |
215 |
case T_INTEGER: { |
247 |
case T_INTEGER | 0x80: { |
216 |
int ch1, ch2, ch3, ch4; |
248 |
int ch1, ch2, ch3, ch4; |
217 |
ch1 = inputStream.read(); |
249 |
ch1 = inputStream.read(); |
218 |
ch2 = inputStream.read(); |
250 |
ch2 = inputStream.read(); |
Lines 225-238
Link Here
|
225 |
} |
257 |
} |
226 |
} |
258 |
} |
227 |
|
259 |
|
228 |
public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException { |
260 |
public static long readLong(InputStream inputStream) throws IOException { |
229 |
int type; |
261 |
int t = inputStream.read(); |
230 |
switch (type = inputStream.read()) { |
262 |
if (t <= 0x7f) |
231 |
case T_NULL: { |
263 |
return t; |
232 |
return null; |
264 |
switch (t) { |
233 |
} case T_STRING: { |
265 |
case T_INTEGER | 0x80: { |
|
|
266 |
int ch1, ch2, ch3, ch4; |
267 |
ch1 = inputStream.read(); |
268 |
ch2 = inputStream.read(); |
269 |
ch3 = inputStream.read(); |
270 |
ch4 = inputStream.read(); |
271 |
return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1; |
272 |
} |
273 |
case T_LONG | 0x80: { |
274 |
int ch1, ch2, ch3, ch4; |
275 |
long v; |
276 |
|
277 |
ch1 = inputStream.read(); |
278 |
ch2 = inputStream.read(); |
279 |
ch3 = inputStream.read(); |
280 |
ch4 = inputStream.read(); |
281 |
v = (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1; |
282 |
ch1 = inputStream.read(); |
283 |
ch2 = inputStream.read(); |
284 |
ch3 = inputStream.read(); |
285 |
ch4 = inputStream.read(); |
286 |
return v << 32 + ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1); |
287 |
} |
288 |
default: |
289 |
throw new IOException("Unknown int format: " + t); |
290 |
} |
291 |
} |
292 |
|
293 |
public static String readString(InputStream inputStream) throws IOException { |
234 |
int length = readInt(inputStream); |
294 |
int length = readInt(inputStream); |
235 |
if (length == 0) |
295 |
if (length == 0) |
|
|
296 |
return null; |
297 |
else if (--length == 0) |
236 |
return ""; |
298 |
return ""; |
237 |
StringBuffer sb = new StringBuffer(length); |
299 |
StringBuffer sb = new StringBuffer(length); |
238 |
do { |
300 |
do { |
Lines 249-258
Link Here
|
249 |
} while (--length > 0); |
311 |
} while (--length > 0); |
250 |
return new String(sb); |
312 |
return new String(sb); |
251 |
} |
313 |
} |
|
|
314 |
|
315 |
public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException { |
316 |
int type; |
317 |
switch (type = inputStream.read()) { |
318 |
case T_NULL: |
319 |
return null; |
320 |
case T_STRING: |
321 |
return readString(inputStream); |
252 |
case T_BYTE: |
322 |
case T_BYTE: |
253 |
case T_SHORT: |
323 |
case T_SHORT: |
254 |
case T_INTEGER: |
324 |
case T_INTEGER: |
255 |
return new Integer(readInt(inputStream, type)); |
325 |
return new Integer(readInt(inputStream)); |
|
|
326 |
case T_LONG: |
327 |
return new Long(readLong(inputStream)); |
256 |
case T_BOOLEAN: { |
328 |
case T_BOOLEAN: { |
257 |
return new Boolean(inputStream.read() == 1); |
329 |
return new Boolean(inputStream.read() == 1); |
258 |
} case T_MAP: { |
330 |
} case T_MAP: { |
Lines 296-302
Link Here
|
296 |
} |
368 |
} |
297 |
} case T_MOF_ATTRIBUTE: { |
369 |
} case T_MOF_ATTRIBUTE: { |
298 |
if (storable == null) Thread.dumpStack(); |
370 |
if (storable == null) Thread.dumpStack(); |
299 |
return new StorableClass.AttributeDescriptor(storable.getMdrStorage(), (String) read(inputStream), (String) read(inputStream), readInt(inputStream), readInt(inputStream), readInt(inputStream), readBoolean(inputStream), readBoolean(inputStream), readBoolean(inputStream)); |
371 |
return new StorableClass.AttributeDescriptor(storable.getMdrStorage(), (String) read(inputStream), (String) read(inputStream), readInt(inputStream), readInt(inputStream), readInt(inputStream), readBoolean(inputStream), readBoolean(inputStream), readBoolean(inputStream), readInt(inputStream)); |
300 |
} case T_MOF_REFERENCE: { |
372 |
} case T_MOF_REFERENCE: { |
301 |
if (storable == null) Thread.dumpStack(); |
373 |
if (storable == null) Thread.dumpStack(); |
302 |
return ((StorableClass) storable).new ReferenceDescriptor((String) read(inputStream, storable), (String) read(inputStream, storable)); |
374 |
return ((StorableClass) storable).new ReferenceDescriptor((String) read(inputStream, storable), (String) read(inputStream, storable)); |