1 | /*
|
---|
2 | * Copyright (c) 2012 Jan Kotek
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.mapdb;
|
---|
17 |
|
---|
18 | import java.io.*;
|
---|
19 | import java.lang.reflect.Array;
|
---|
20 | import java.math.BigDecimal;
|
---|
21 | import java.math.BigInteger;
|
---|
22 | import java.util.*;
|
---|
23 |
|
---|
24 | import static org.mapdb.SerializationHeader.*;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Serializer which uses 'header byte' to serialize/deserialize
|
---|
28 | * most of classes from 'java.lang' and 'java.util' packages.
|
---|
29 | *
|
---|
30 | * @author Jan Kotek
|
---|
31 | */
|
---|
32 | @SuppressWarnings({ "unchecked", "rawtypes" })
|
---|
33 | public class SerializerBase implements Serializer{
|
---|
34 |
|
---|
35 |
|
---|
36 | static final class knownSerializable{
|
---|
37 | static final Set get = new HashSet(Arrays.asList(
|
---|
38 | BTreeKeySerializer.STRING,
|
---|
39 | BTreeKeySerializer.ZERO_OR_POSITIVE_LONG,
|
---|
40 | BTreeKeySerializer.ZERO_OR_POSITIVE_INT,
|
---|
41 | Utils.COMPARABLE_COMPARATOR, Utils.COMPARABLE_COMPARATOR_WITH_NULLS,
|
---|
42 |
|
---|
43 | Serializer.STRING_SERIALIZER, Serializer.LONG_SERIALIZER, Serializer.INTEGER_SERIALIZER,
|
---|
44 | Serializer.EMPTY_SERIALIZER, Serializer.BASIC_SERIALIZER, Serializer.CRC32_CHECKSUM
|
---|
45 | ));
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static void assertSerializable(Object o){
|
---|
49 | if(o!=null && !(o instanceof Serializable)
|
---|
50 | && !knownSerializable.get.contains(o)){
|
---|
51 | throw new IllegalArgumentException("Not serializable: "+o.getClass());
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Utility class similar to ArrayList, but with fast identity search.
|
---|
57 | */
|
---|
58 | protected final static class FastArrayList<K> {
|
---|
59 |
|
---|
60 | private int size = 0;
|
---|
61 | private K[] elementData = (K[]) new Object[1];
|
---|
62 |
|
---|
63 | boolean forwardRefs = false;
|
---|
64 |
|
---|
65 | K get(int index) {
|
---|
66 | if (index >= size)
|
---|
67 | throw new IndexOutOfBoundsException();
|
---|
68 | return elementData[index];
|
---|
69 | }
|
---|
70 |
|
---|
71 | void add(K o) {
|
---|
72 | if (elementData.length == size) {
|
---|
73 | //grow array if necessary
|
---|
74 | elementData = Arrays.copyOf(elementData, elementData.length * 2);
|
---|
75 | }
|
---|
76 |
|
---|
77 | elementData[size] = o;
|
---|
78 | size++;
|
---|
79 | }
|
---|
80 |
|
---|
81 | int size() {
|
---|
82 | return size;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * This method is reason why ArrayList is not used.
|
---|
88 | * Search an item in list and returns its index.
|
---|
89 | * It uses identity rather than 'equalsTo'
|
---|
90 | * One could argue that TreeMap should be used instead,
|
---|
91 | * but we do not expect large object trees.
|
---|
92 | * This search is VERY FAST compared to Maps, it does not allocate
|
---|
93 | * new instances or uses method calls.
|
---|
94 | *
|
---|
95 | * @param obj to find in list
|
---|
96 | * @return index of object in list or -1 if not found
|
---|
97 | */
|
---|
98 | int identityIndexOf(Object obj) {
|
---|
99 | for (int i = 0; i < size; i++) {
|
---|
100 | if (obj == elementData[i]){
|
---|
101 | forwardRefs = true;
|
---|
102 | return i;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return -1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public void serialize(final DataOutput out, final Object obj) throws IOException {
|
---|
115 | serialize(out, obj, null);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | public void serialize(final DataOutput out, final Object obj, FastArrayList<Object> objectStack) throws IOException {
|
---|
120 |
|
---|
121 | /**try to find object on stack if it exists*/
|
---|
122 | if (objectStack != null) {
|
---|
123 | int indexInObjectStack = objectStack.identityIndexOf(obj);
|
---|
124 | if (indexInObjectStack != -1) {
|
---|
125 | //object was already serialized, just write reference to it and return
|
---|
126 | out.write(OBJECT_STACK);
|
---|
127 | Utils.packInt(out, indexInObjectStack);
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | //add this object to objectStack
|
---|
131 | objectStack.add(obj);
|
---|
132 | }
|
---|
133 |
|
---|
134 | final Class clazz = obj != null ? obj.getClass() : null;
|
---|
135 |
|
---|
136 | /** first try to serialize object without initializing object stack*/
|
---|
137 | if (obj == null) {
|
---|
138 | out.write(NULL);
|
---|
139 | return;
|
---|
140 | } else if (clazz == Boolean.class) {
|
---|
141 | if ((Boolean) obj)
|
---|
142 | out.write(BOOLEAN_TRUE);
|
---|
143 | else
|
---|
144 | out.write(BOOLEAN_FALSE);
|
---|
145 | return;
|
---|
146 | } else if (clazz == Integer.class) {
|
---|
147 | final int val = (Integer) obj;
|
---|
148 | writeInteger(out, val);
|
---|
149 | return;
|
---|
150 | } else if (clazz == Double.class) {
|
---|
151 | double v = (Double) obj;
|
---|
152 | if (v == -1d)
|
---|
153 | out.write(DOUBLE_MINUS_1);
|
---|
154 | else if (v == 0d)
|
---|
155 | out.write(DOUBLE_0);
|
---|
156 | else if (v == 1d)
|
---|
157 | out.write(DOUBLE_1);
|
---|
158 | else if (v >= 0 && v <= 255 && (int) v == v) {
|
---|
159 | out.write(DOUBLE_255);
|
---|
160 | out.write((int) v);
|
---|
161 | } else if (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE && (short) v == v) {
|
---|
162 | out.write(DOUBLE_SHORT);
|
---|
163 | out.writeShort((int) v);
|
---|
164 | } else {
|
---|
165 | out.write(DOUBLE_FULL);
|
---|
166 | out.writeDouble(v);
|
---|
167 | }
|
---|
168 | return;
|
---|
169 | } else if (clazz == Float.class) {
|
---|
170 | float v = (Float) obj;
|
---|
171 | if (v == -1f)
|
---|
172 | out.write(FLOAT_MINUS_1);
|
---|
173 | else if (v == 0f)
|
---|
174 | out.write(FLOAT_0);
|
---|
175 | else if (v == 1f)
|
---|
176 | out.write(FLOAT_1);
|
---|
177 | else if (v >= 0 && v <= 255 && (int) v == v) {
|
---|
178 | out.write(FLOAT_255);
|
---|
179 | out.write((int) v);
|
---|
180 | } else if (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE && (short) v == v) {
|
---|
181 | out.write(FLOAT_SHORT);
|
---|
182 | out.writeShort((int) v);
|
---|
183 |
|
---|
184 | } else {
|
---|
185 | out.write(FLOAT_FULL);
|
---|
186 | out.writeFloat(v);
|
---|
187 | }
|
---|
188 | return;
|
---|
189 | } else if (clazz == BigInteger.class) {
|
---|
190 | out.write(BIGINTEGER);
|
---|
191 | byte[] buf = ((BigInteger) obj).toByteArray();
|
---|
192 | Utils.packInt(out, buf.length);
|
---|
193 | out.write(buf);
|
---|
194 | return;
|
---|
195 | } else if (clazz == BigDecimal.class) {
|
---|
196 | out.write(BIGDECIMAL);
|
---|
197 | BigDecimal d = (BigDecimal) obj;
|
---|
198 | byte[] buf = d.unscaledValue().toByteArray();
|
---|
199 | Utils.packInt(out, buf.length);
|
---|
200 | out.write(buf);
|
---|
201 | Utils.packInt(out, d.scale());
|
---|
202 | return;
|
---|
203 | } else if (clazz == Long.class) {
|
---|
204 | final long val = (Long) obj;
|
---|
205 | writeLong(out, val);
|
---|
206 | return;
|
---|
207 | } else if (clazz == Short.class) {
|
---|
208 | short val = (Short) obj;
|
---|
209 | if (val == -1)
|
---|
210 | out.write(SHORT_MINUS_1);
|
---|
211 | else if (val == 0)
|
---|
212 | out.write(SHORT_0);
|
---|
213 | else if (val == 1)
|
---|
214 | out.write(SHORT_1);
|
---|
215 | else if (val > 0 && val < 255) {
|
---|
216 | out.write(SHORT_255);
|
---|
217 | out.write(val);
|
---|
218 | } else {
|
---|
219 | out.write(SHORT_FULL);
|
---|
220 | out.writeShort(val);
|
---|
221 | }
|
---|
222 | return;
|
---|
223 | } else if (clazz == Byte.class) {
|
---|
224 | byte val = (Byte) obj;
|
---|
225 | if (val == -1)
|
---|
226 | out.write(BYTE_MINUS_1);
|
---|
227 | else if (val == 0)
|
---|
228 | out.write(BYTE_0);
|
---|
229 | else if (val == 1)
|
---|
230 | out.write(BYTE_1);
|
---|
231 | else {
|
---|
232 | out.write(BYTE_FULL);
|
---|
233 | out.writeByte(val);
|
---|
234 | }
|
---|
235 | return;
|
---|
236 | } else if (clazz == Character.class) {
|
---|
237 | out.write(CHAR);
|
---|
238 | out.writeChar((Character) obj);
|
---|
239 | return;
|
---|
240 | } else if (clazz == String.class) {
|
---|
241 | String s = (String) obj;
|
---|
242 | if (s.length() == 0) {
|
---|
243 | out.write(STRING_EMPTY);
|
---|
244 | } else {
|
---|
245 | out.write(STRING);
|
---|
246 | serializeString(out, s);
|
---|
247 | }
|
---|
248 | return;
|
---|
249 | } else if (obj instanceof Class) {
|
---|
250 | out.write(CLASS);
|
---|
251 | serializeClass(out, (Class)obj);
|
---|
252 | return;
|
---|
253 | } else if (obj instanceof int[]) {
|
---|
254 | writeIntArray(out, (int[]) obj);
|
---|
255 | return;
|
---|
256 | } else if (obj instanceof long[]) {
|
---|
257 | writeLongArray(out, (long[]) obj);
|
---|
258 | return;
|
---|
259 | } else if (obj instanceof short[]) {
|
---|
260 | out.write(SHORT_ARRAY);
|
---|
261 | short[] a = (short[]) obj;
|
---|
262 | Utils.packInt(out, a.length);
|
---|
263 | for(short s:a) out.writeShort(s);
|
---|
264 | return;
|
---|
265 | } else if (obj instanceof boolean[]) {
|
---|
266 | out.write(BOOLEAN_ARRAY);
|
---|
267 | boolean[] a = (boolean[]) obj;
|
---|
268 | Utils.packInt(out, a.length);
|
---|
269 | for(boolean s:a) out.writeBoolean(s); //TODO pack 8 booleans to single byte
|
---|
270 | return;
|
---|
271 | } else if (obj instanceof double[]) {
|
---|
272 | out.write(DOUBLE_ARRAY);
|
---|
273 | double[] a = (double[]) obj;
|
---|
274 | Utils.packInt(out, a.length);
|
---|
275 | for(double s:a) out.writeDouble(s);
|
---|
276 | return;
|
---|
277 | } else if (obj instanceof float[]) {
|
---|
278 | out.write(FLOAT_ARRAY);
|
---|
279 | float[] a = (float[]) obj;
|
---|
280 | Utils.packInt(out, a.length);
|
---|
281 | for(float s:a) out.writeFloat(s);
|
---|
282 | return;
|
---|
283 | } else if (obj instanceof char[]) {
|
---|
284 | out.write(CHAR_ARRAY);
|
---|
285 | char[] a = (char[]) obj;
|
---|
286 | Utils.packInt(out, a.length);
|
---|
287 | for(char s:a) out.writeChar(s);
|
---|
288 | return;
|
---|
289 | } else if (obj instanceof byte[]) {
|
---|
290 | byte[] b = (byte[]) obj;
|
---|
291 | serializeByteArray(out, b);
|
---|
292 | return;
|
---|
293 | } else if (clazz == Date.class) {
|
---|
294 | out.write(DATE);
|
---|
295 | out.writeLong(((Date) obj).getTime());
|
---|
296 | return;
|
---|
297 | } else if (clazz == UUID.class) {
|
---|
298 | out.write(UUID);
|
---|
299 | out.writeLong(((UUID) obj).getMostSignificantBits());
|
---|
300 | out.writeLong(((UUID)obj).getLeastSignificantBits());
|
---|
301 | return;
|
---|
302 | } else if(clazz == BTreeKeySerializer.BasicKeySerializer.class){
|
---|
303 | out.write(B_TREE_BASIC_KEY_SERIALIZER);
|
---|
304 | if(((BTreeKeySerializer.BasicKeySerializer)obj).defaultSerializer!=this) throw new InternalError();
|
---|
305 | return;
|
---|
306 | } else if(clazz == CompressSerializerWrapper.class){
|
---|
307 | out.write(SERIALIZER_COMPRESSION_WRAPPER);
|
---|
308 | serialize(out, ((CompressSerializerWrapper)obj).serializer, objectStack);
|
---|
309 | return;
|
---|
310 |
|
---|
311 | } else if(obj == BTreeKeySerializer.ZERO_OR_POSITIVE_LONG){
|
---|
312 | out.write(B_TREE_SERIALIZER_POS_LONG);
|
---|
313 | return;
|
---|
314 | } else if(obj == BTreeKeySerializer.ZERO_OR_POSITIVE_INT){
|
---|
315 | out.write(B_TREE_SERIALIZER_POS_INT);
|
---|
316 | return;
|
---|
317 | } else if(obj == Serializer.STRING_SERIALIZER){
|
---|
318 | out.write(SerializationHeader.STRING_SERIALIZER);
|
---|
319 | return;
|
---|
320 | } else if(obj == Serializer.LONG_SERIALIZER){
|
---|
321 | out.write(SerializationHeader.LONG_SERIALIZER);
|
---|
322 | return;
|
---|
323 | } else if(obj == Serializer.INTEGER_SERIALIZER){
|
---|
324 | out.write(SerializationHeader.INTEGER_SERIALIZER);
|
---|
325 | return;
|
---|
326 | } else if(obj == Serializer.EMPTY_SERIALIZER){
|
---|
327 | out.write(SerializationHeader.EMPTY_SERIALIZER);
|
---|
328 | return;
|
---|
329 | } else if(obj == Serializer.CRC32_CHECKSUM){
|
---|
330 | out.write(SerializationHeader.CRC32_SERIALIZER);
|
---|
331 | return;
|
---|
332 | } else if(obj == BTreeKeySerializer.STRING){
|
---|
333 | out.write(B_TREE_SERIALIZER_STRING);
|
---|
334 | return;
|
---|
335 | } else if(obj == Utils.COMPARABLE_COMPARATOR){
|
---|
336 | out.write(COMPARABLE_COMPARATOR);
|
---|
337 | return;
|
---|
338 | } else if(obj == Utils.COMPARABLE_COMPARATOR_WITH_NULLS){
|
---|
339 | out.write(COMPARABLE_COMPARATOR_WITH_NULLS);
|
---|
340 | return;
|
---|
341 | } else if(obj == BASIC_SERIALIZER){
|
---|
342 | out.write(SerializationHeader.BASIC_SERIALIZER);
|
---|
343 | return;
|
---|
344 | } else if(obj == Fun.HI){
|
---|
345 | out.write(FUN_HI);
|
---|
346 | } else if(obj == this){
|
---|
347 | out.write(THIS_SERIALIZER);
|
---|
348 | return;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 |
|
---|
353 |
|
---|
354 | /** classes bellow need object stack, so initialize it if not alredy initialized*/
|
---|
355 | if (objectStack == null) {
|
---|
356 | objectStack = new FastArrayList<>();
|
---|
357 | objectStack.add(obj);
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | if (obj instanceof Object[]) {
|
---|
362 | Object[] b = (Object[]) obj;
|
---|
363 | boolean packableLongs = b.length <= 255;
|
---|
364 | boolean allNull = true;
|
---|
365 | if (packableLongs) {
|
---|
366 | //check if it contains packable longs
|
---|
367 | for (Object o : b) {
|
---|
368 | if(o!=null){
|
---|
369 | allNull=false;
|
---|
370 | if (o.getClass() != Long.class || ((Long) o < 0 && (Long) o != Long.MAX_VALUE)) {
|
---|
371 | packableLongs = false;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | if(!packableLongs && !allNull)
|
---|
376 | break;
|
---|
377 | }
|
---|
378 | }else{
|
---|
379 | //check for all null
|
---|
380 | for (Object o : b) {
|
---|
381 | if(o!=null){
|
---|
382 | allNull=false;
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 | if(allNull){
|
---|
388 | out.write(ARRAY_OBJECT_ALL_NULL);
|
---|
389 | Utils.packInt(out, b.length);
|
---|
390 |
|
---|
391 | // Write classfor components
|
---|
392 | Class<?> componentType = obj.getClass().getComponentType();
|
---|
393 | serializeClass(out, componentType);
|
---|
394 |
|
---|
395 | }else if (packableLongs) {
|
---|
396 | //packable Longs is special case, it is often used in JDBM to reference fields
|
---|
397 | out.write(ARRAY_OBJECT_PACKED_LONG);
|
---|
398 | out.write(b.length);
|
---|
399 | for (Object o : b) {
|
---|
400 | if (o == null)
|
---|
401 | Utils.packLong(out, 0);
|
---|
402 | else
|
---|
403 | Utils.packLong(out, (Long) o + 1);
|
---|
404 | }
|
---|
405 |
|
---|
406 | } else {
|
---|
407 | out.write(ARRAY_OBJECT);
|
---|
408 | Utils.packInt(out, b.length);
|
---|
409 |
|
---|
410 | // Write classfor components
|
---|
411 | Class<?> componentType = obj.getClass().getComponentType();
|
---|
412 | serializeClass(out, componentType);
|
---|
413 |
|
---|
414 | for (Object o : b)
|
---|
415 | serialize(out, o, objectStack);
|
---|
416 |
|
---|
417 | }
|
---|
418 |
|
---|
419 | } else if (clazz == ArrayList.class) {
|
---|
420 | ArrayList l = (ArrayList) obj;
|
---|
421 | boolean packableLongs = l.size() < 255;
|
---|
422 | if (packableLongs) {
|
---|
423 | //packable Longs is special case, it is often used in JDBM to reference fields
|
---|
424 | for (Object o : l) {
|
---|
425 | if (o != null && (o.getClass() != Long.class || ((Long) o < 0 && (Long) o != Long.MAX_VALUE))) {
|
---|
426 | packableLongs = false;
|
---|
427 | break;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 | if (packableLongs) {
|
---|
432 | out.write(ARRAYLIST_PACKED_LONG);
|
---|
433 | out.write(l.size());
|
---|
434 | for (Object o : l) {
|
---|
435 | if (o == null)
|
---|
436 | Utils.packLong(out, 0);
|
---|
437 | else
|
---|
438 | Utils.packLong(out, (Long) o + 1);
|
---|
439 | }
|
---|
440 | } else {
|
---|
441 | serializeCollection(ARRAYLIST, out, obj, objectStack);
|
---|
442 | }
|
---|
443 |
|
---|
444 | } else if (clazz == java.util.LinkedList.class) {
|
---|
445 | serializeCollection(LINKEDLIST, out, obj, objectStack);
|
---|
446 | } else if (clazz == Vector.class) {
|
---|
447 | serializeCollection(VECTOR, out, obj, objectStack);
|
---|
448 | } else if (clazz == TreeSet.class) {
|
---|
449 | TreeSet l = (TreeSet) obj;
|
---|
450 | out.write(TREESET);
|
---|
451 | Utils.packInt(out, l.size());
|
---|
452 | serialize(out, l.comparator(), objectStack);
|
---|
453 | for (Object o : l)
|
---|
454 | serialize(out, o, objectStack);
|
---|
455 | } else if (clazz == HashSet.class) {
|
---|
456 | serializeCollection(HASHSET, out, obj, objectStack);
|
---|
457 | } else if (clazz == LinkedHashSet.class) {
|
---|
458 | serializeCollection(LINKEDHASHSET, out, obj, objectStack);
|
---|
459 | } else if (clazz == TreeMap.class) {
|
---|
460 | TreeMap l = (TreeMap) obj;
|
---|
461 | out.write(TREEMAP);
|
---|
462 | Utils.packInt(out, l.size());
|
---|
463 | serialize(out, l.comparator(), objectStack);
|
---|
464 | for (Object o : l.keySet()) {
|
---|
465 | serialize(out, o, objectStack);
|
---|
466 | serialize(out, l.get(o), objectStack);
|
---|
467 | }
|
---|
468 | } else if (clazz == HashMap.class) {
|
---|
469 | serializeMap(HASHMAP, out, obj, objectStack);
|
---|
470 | } else if (clazz == IdentityHashMap.class) {
|
---|
471 | serializeMap(IDENTITYHASHMAP, out, obj, objectStack);
|
---|
472 | } else if (clazz == LinkedHashMap.class) {
|
---|
473 | serializeMap(LINKEDHASHMAP, out, obj, objectStack);
|
---|
474 | } else if (clazz == Hashtable.class) {
|
---|
475 | serializeMap(HASHTABLE, out, obj, objectStack);
|
---|
476 | } else if (clazz == Properties.class) {
|
---|
477 | serializeMap(PROPERTIES, out, obj, objectStack);
|
---|
478 | } else if (clazz == Locale.class){
|
---|
479 | out.write(LOCALE);
|
---|
480 | Locale l = (Locale) obj;
|
---|
481 | out.writeUTF(l.getLanguage());
|
---|
482 | out.writeUTF(l.getCountry());
|
---|
483 | out.writeUTF(l.getVariant());
|
---|
484 | } else if (clazz == Fun.Tuple2.class){
|
---|
485 | out.write(TUPLE2);
|
---|
486 | Fun.Tuple2 t = (Fun.Tuple2) obj;
|
---|
487 | serialize(out, t.a, objectStack);
|
---|
488 | serialize(out, t.b, objectStack);
|
---|
489 | } else if (clazz == Fun.Tuple2.class){
|
---|
490 | out.write(TUPLE3);
|
---|
491 | Fun.Tuple3 t = (Fun.Tuple3) obj;
|
---|
492 | serialize(out, t.a, objectStack);
|
---|
493 | serialize(out, t.b, objectStack);
|
---|
494 | serialize(out, t.c, objectStack);
|
---|
495 | } else if (clazz == Fun.Tuple4.class){
|
---|
496 | out.write(TUPLE4);
|
---|
497 | Fun.Tuple4 t = (Fun.Tuple4) obj;
|
---|
498 | serialize(out, t.a, objectStack);
|
---|
499 | serialize(out, t.b, objectStack);
|
---|
500 | serialize(out, t.c, objectStack);
|
---|
501 | serialize(out, t.d, objectStack);
|
---|
502 | } else {
|
---|
503 | serializeUnknownObject(out, obj, objectStack);
|
---|
504 | }
|
---|
505 |
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | protected void serializeClass(DataOutput out, Class clazz) throws IOException {
|
---|
510 | //TODO override in SerializerPojo
|
---|
511 | out.writeUTF(clazz.getName());
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | static void serializeString(DataOutput out, String obj) throws IOException {
|
---|
516 | final int len = obj.length();
|
---|
517 | Utils.packInt(out, len);
|
---|
518 | for (int i = 0; i < len; i++) {
|
---|
519 | int c = (int) obj.charAt(i); //TODO investigate if c could be negative here
|
---|
520 | Utils.packInt(out, c);
|
---|
521 | }
|
---|
522 |
|
---|
523 | }
|
---|
524 |
|
---|
525 | private void serializeMap(int header, DataOutput out, Object obj, FastArrayList<Object> objectStack) throws IOException {
|
---|
526 | Map l = (Map) obj;
|
---|
527 | out.write(header);
|
---|
528 | Utils.packInt(out, l.size());
|
---|
529 | for (Object o : l.keySet()) {
|
---|
530 | serialize(out, o, objectStack);
|
---|
531 | serialize(out, l.get(o), objectStack);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | private void serializeCollection(int header, DataOutput out, Object obj, FastArrayList<Object> objectStack) throws IOException {
|
---|
536 | Collection l = (Collection) obj;
|
---|
537 | out.write(header);
|
---|
538 | Utils.packInt(out, l.size());
|
---|
539 |
|
---|
540 | for (Object o : l)
|
---|
541 | serialize(out, o, objectStack);
|
---|
542 |
|
---|
543 | }
|
---|
544 |
|
---|
545 | private void serializeByteArray(DataOutput out, byte[] b) throws IOException {
|
---|
546 | boolean allEqual = b.length>0;
|
---|
547 | //check if all values in byte[] are equal
|
---|
548 | for(int i=1;i<b.length;i++){
|
---|
549 | if(b[i-1]!=b[i]){
|
---|
550 | allEqual=false;
|
---|
551 | break;
|
---|
552 | }
|
---|
553 | }
|
---|
554 | if(allEqual){
|
---|
555 | out.write(ARRAY_BYTE_ALL_EQUAL);
|
---|
556 | Utils.packInt(out, b.length);
|
---|
557 | out.write(b[0]);
|
---|
558 | }else{
|
---|
559 | out.write(ARRAY_BYTE);
|
---|
560 | Utils.packInt(out, b.length);
|
---|
561 | out.write(b);
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | private void writeLongArray(DataOutput da, long[] obj) throws IOException {
|
---|
567 | long max = Long.MIN_VALUE;
|
---|
568 | long min = Long.MAX_VALUE;
|
---|
569 | for (long i : obj) {
|
---|
570 | max = Math.max(max, i);
|
---|
571 | min = Math.min(min, i);
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (0 <= min && max <= 255) {
|
---|
575 | da.write(ARRAY_LONG_B);
|
---|
576 | Utils.packInt(da, obj.length);
|
---|
577 | for (long l : obj)
|
---|
578 | da.write((int) l);
|
---|
579 | } else if (0 <= min) {
|
---|
580 | da.write(ARRAY_LONG_PACKED);
|
---|
581 | Utils.packInt(da, obj.length);
|
---|
582 | for (long l : obj)
|
---|
583 | Utils.packLong(da, l);
|
---|
584 | } else if (Short.MIN_VALUE <= min && max <= Short.MAX_VALUE) {
|
---|
585 | da.write(ARRAY_LONG_S);
|
---|
586 | Utils.packInt(da, obj.length);
|
---|
587 | for (long l : obj)
|
---|
588 | da.writeShort((short) l);
|
---|
589 | } else if (Integer.MIN_VALUE <= min && max <= Integer.MAX_VALUE) {
|
---|
590 | da.write(ARRAY_LONG_I);
|
---|
591 | Utils.packInt(da, obj.length);
|
---|
592 | for (long l : obj)
|
---|
593 | da.writeInt((int) l);
|
---|
594 | } else {
|
---|
595 | da.write(ARRAY_LONG_L);
|
---|
596 | Utils.packInt(da, obj.length);
|
---|
597 | for (long l : obj)
|
---|
598 | da.writeLong(l);
|
---|
599 | }
|
---|
600 |
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | private void writeIntArray(DataOutput da, int[] obj) throws IOException {
|
---|
605 | int max = Integer.MIN_VALUE;
|
---|
606 | int min = Integer.MAX_VALUE;
|
---|
607 | for (int i : obj) {
|
---|
608 | max = Math.max(max, i);
|
---|
609 | min = Math.min(min, i);
|
---|
610 | }
|
---|
611 |
|
---|
612 | boolean fitsInByte = 0 <= min && max <= 255;
|
---|
613 | boolean fitsInShort = Short.MIN_VALUE >= min && max <= Short.MAX_VALUE;
|
---|
614 |
|
---|
615 |
|
---|
616 | if (obj.length <= 255 && fitsInByte) {
|
---|
617 | da.write(ARRAY_INT_B_255);
|
---|
618 | da.write(obj.length);
|
---|
619 | for (int i : obj)
|
---|
620 | da.write(i);
|
---|
621 | } else if (fitsInByte) {
|
---|
622 | da.write(ARRAY_INT_B_INT);
|
---|
623 | Utils.packInt(da, obj.length);
|
---|
624 | for (int i : obj)
|
---|
625 | da.write(i);
|
---|
626 | } else if (0 <= min) {
|
---|
627 | da.write(ARRAY_INT_PACKED);
|
---|
628 | Utils.packInt(da, obj.length);
|
---|
629 | for (int l : obj)
|
---|
630 | Utils.packInt(da, l);
|
---|
631 | } else if (fitsInShort) {
|
---|
632 | da.write(ARRAY_INT_S);
|
---|
633 | Utils.packInt(da, obj.length);
|
---|
634 | for (int i : obj)
|
---|
635 | da.writeShort(i);
|
---|
636 | } else {
|
---|
637 | da.write(ARRAY_INT_I);
|
---|
638 | Utils.packInt(da, obj.length);
|
---|
639 | for (int i : obj)
|
---|
640 | da.writeInt(i);
|
---|
641 | }
|
---|
642 |
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | private void writeInteger(DataOutput da, final int val) throws IOException {
|
---|
647 | if (val == -1)
|
---|
648 | da.write(INTEGER_MINUS_1);
|
---|
649 | else if (val == 0)
|
---|
650 | da.write(INTEGER_0);
|
---|
651 | else if (val == 1)
|
---|
652 | da.write(INTEGER_1);
|
---|
653 | else if (val == 2)
|
---|
654 | da.write(INTEGER_2);
|
---|
655 | else if (val == 3)
|
---|
656 | da.write(INTEGER_3);
|
---|
657 | else if (val == 4)
|
---|
658 | da.write(INTEGER_4);
|
---|
659 | else if (val == 5)
|
---|
660 | da.write(INTEGER_5);
|
---|
661 | else if (val == 6)
|
---|
662 | da.write(INTEGER_6);
|
---|
663 | else if (val == 7)
|
---|
664 | da.write(INTEGER_7);
|
---|
665 | else if (val == 8)
|
---|
666 | da.write(INTEGER_8);
|
---|
667 | else if (val == Integer.MIN_VALUE)
|
---|
668 | da.write(INTEGER_MINUS_MAX);
|
---|
669 | else if (val > 0 && val < 255) {
|
---|
670 | da.write(INTEGER_255);
|
---|
671 | da.write(val);
|
---|
672 | } else if (val < 0) {
|
---|
673 | da.write(INTEGER_PACK_NEG);
|
---|
674 | Utils.packInt(da, -val);
|
---|
675 | } else {
|
---|
676 | da.write(INTEGER_PACK);
|
---|
677 | Utils.packInt(da, val);
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | private void writeLong(DataOutput da, final long val) throws IOException {
|
---|
682 | if (val == -1)
|
---|
683 | da.write(LONG_MINUS_1);
|
---|
684 | else if (val == 0)
|
---|
685 | da.write(LONG_0);
|
---|
686 | else if (val == 1)
|
---|
687 | da.write(LONG_1);
|
---|
688 | else if (val == 2)
|
---|
689 | da.write(LONG_2);
|
---|
690 | else if (val == 3)
|
---|
691 | da.write(LONG_3);
|
---|
692 | else if (val == 4)
|
---|
693 | da.write(LONG_4);
|
---|
694 | else if (val == 5)
|
---|
695 | da.write(LONG_5);
|
---|
696 | else if (val == 6)
|
---|
697 | da.write(LONG_6);
|
---|
698 | else if (val == 7)
|
---|
699 | da.write(LONG_7);
|
---|
700 | else if (val == 8)
|
---|
701 | da.write(LONG_8);
|
---|
702 | else if (val == Long.MIN_VALUE)
|
---|
703 | da.write(LONG_MINUS_MAX);
|
---|
704 | else if (val > 0 && val < 255) {
|
---|
705 | da.write(LONG_255);
|
---|
706 | da.write((int) val);
|
---|
707 | } else if (val < 0) {
|
---|
708 | da.write(LONG_PACK_NEG);
|
---|
709 | Utils.packLong(da, -val);
|
---|
710 | } else {
|
---|
711 | da.write(LONG_PACK);
|
---|
712 | Utils.packLong(da, val);
|
---|
713 | }
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 |
|
---|
718 | static String deserializeString(DataInput buf) throws IOException {
|
---|
719 | int len = Utils.unpackInt(buf);
|
---|
720 | char[] b = new char[len];
|
---|
721 | for (int i = 0; i < len; i++)
|
---|
722 | b[i] = (char) Utils.unpackInt(buf);
|
---|
723 |
|
---|
724 | return new String(b);
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | @Override
|
---|
729 | public Object deserialize(DataInput is, int capacity) throws IOException {
|
---|
730 | if(capacity==0) return null;
|
---|
731 | return deserialize(is, null);
|
---|
732 | }
|
---|
733 |
|
---|
734 | public Object deserialize(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
735 |
|
---|
736 | Object ret = null;
|
---|
737 |
|
---|
738 | final int head = is.readUnsignedByte();
|
---|
739 |
|
---|
740 | /** first try to deserialize object without allocating object stack*/
|
---|
741 | switch (head) {
|
---|
742 | case NULL:
|
---|
743 | break;
|
---|
744 | case BOOLEAN_TRUE:
|
---|
745 | ret = Boolean.TRUE;
|
---|
746 | break;
|
---|
747 | case BOOLEAN_FALSE:
|
---|
748 | ret = Boolean.FALSE;
|
---|
749 | break;
|
---|
750 | case INTEGER_MINUS_1:
|
---|
751 | ret = -1;
|
---|
752 | break;
|
---|
753 | case INTEGER_0:
|
---|
754 | ret = 0;
|
---|
755 | break;
|
---|
756 | case INTEGER_1:
|
---|
757 | ret = 1;
|
---|
758 | break;
|
---|
759 | case INTEGER_2:
|
---|
760 | ret = 2;
|
---|
761 | break;
|
---|
762 | case INTEGER_3:
|
---|
763 | ret = 3;
|
---|
764 | break;
|
---|
765 | case INTEGER_4:
|
---|
766 | ret = 4;
|
---|
767 | break;
|
---|
768 | case INTEGER_5:
|
---|
769 | ret = 5;
|
---|
770 | break;
|
---|
771 | case INTEGER_6:
|
---|
772 | ret = 6;
|
---|
773 | break;
|
---|
774 | case INTEGER_7:
|
---|
775 | ret = 7;
|
---|
776 | break;
|
---|
777 | case INTEGER_8:
|
---|
778 | ret = 8;
|
---|
779 | break;
|
---|
780 | case INTEGER_MINUS_MAX:
|
---|
781 | ret = Integer.MIN_VALUE;
|
---|
782 | break;
|
---|
783 | case INTEGER_255:
|
---|
784 | ret = is.readUnsignedByte();
|
---|
785 | break;
|
---|
786 | case INTEGER_PACK_NEG:
|
---|
787 | ret = -Utils.unpackInt(is);
|
---|
788 | break;
|
---|
789 | case INTEGER_PACK:
|
---|
790 | ret = Utils.unpackInt(is);
|
---|
791 | break;
|
---|
792 | case LONG_MINUS_1:
|
---|
793 | ret = (long) -1;
|
---|
794 | break;
|
---|
795 | case LONG_0:
|
---|
796 | ret = (long) 0;
|
---|
797 | break;
|
---|
798 | case LONG_1:
|
---|
799 | ret = (long) 1;
|
---|
800 | break;
|
---|
801 | case LONG_2:
|
---|
802 | ret = (long) 2;
|
---|
803 | break;
|
---|
804 | case LONG_3:
|
---|
805 | ret = (long) 3;
|
---|
806 | break;
|
---|
807 | case LONG_4:
|
---|
808 | ret = (long) 4;
|
---|
809 | break;
|
---|
810 | case LONG_5:
|
---|
811 | ret = (long) 5;
|
---|
812 | break;
|
---|
813 | case LONG_6:
|
---|
814 | ret = (long) 6;
|
---|
815 | break;
|
---|
816 | case LONG_7:
|
---|
817 | ret = (long) 7;
|
---|
818 | break;
|
---|
819 | case LONG_8:
|
---|
820 | ret = (long) 8;
|
---|
821 | break;
|
---|
822 | case LONG_255:
|
---|
823 | ret = (long) is.readUnsignedByte();
|
---|
824 | break;
|
---|
825 | case LONG_PACK_NEG:
|
---|
826 | ret = -Utils.unpackLong(is);
|
---|
827 | break;
|
---|
828 | case LONG_PACK:
|
---|
829 | ret = Utils.unpackLong(is);
|
---|
830 | break;
|
---|
831 | case LONG_MINUS_MAX:
|
---|
832 | ret = Long.MIN_VALUE;
|
---|
833 | break;
|
---|
834 | case SHORT_MINUS_1:
|
---|
835 | ret = (short) -1;
|
---|
836 | break;
|
---|
837 | case SHORT_0:
|
---|
838 | ret = (short) 0;
|
---|
839 | break;
|
---|
840 | case SHORT_1:
|
---|
841 | ret = (short) 1;
|
---|
842 | break;
|
---|
843 | case SHORT_255:
|
---|
844 | ret = (short) is.readUnsignedByte();
|
---|
845 | break;
|
---|
846 | case SHORT_FULL:
|
---|
847 | ret = is.readShort();
|
---|
848 | break;
|
---|
849 | case BYTE_MINUS_1:
|
---|
850 | ret = (byte) -1;
|
---|
851 | break;
|
---|
852 | case BYTE_0:
|
---|
853 | ret = (byte) 0;
|
---|
854 | break;
|
---|
855 | case BYTE_1:
|
---|
856 | ret = (byte) 1;
|
---|
857 | break;
|
---|
858 | case BYTE_FULL:
|
---|
859 | ret = is.readByte();
|
---|
860 | break;
|
---|
861 | case SHORT_ARRAY:
|
---|
862 | int size = Utils.unpackInt(is);
|
---|
863 | ret = new short[size];
|
---|
864 | for(int i=0;i<size;i++) ((short[])ret)[i] = is.readShort();
|
---|
865 | break;
|
---|
866 | case BOOLEAN_ARRAY:
|
---|
867 | size = Utils.unpackInt(is);
|
---|
868 | ret = new boolean[size];
|
---|
869 | for(int i=0;i<size;i++) ((boolean[])ret)[i] = is.readBoolean();
|
---|
870 | break;
|
---|
871 | case DOUBLE_ARRAY:
|
---|
872 | size = Utils.unpackInt(is);
|
---|
873 | ret = new double[size];
|
---|
874 | for(int i=0;i<size;i++) ((double[])ret)[i] = is.readDouble();
|
---|
875 | break;
|
---|
876 | case FLOAT_ARRAY:
|
---|
877 | size = Utils.unpackInt(is);
|
---|
878 | ret = new float[size];
|
---|
879 | for(int i=0;i<size;i++) ((float[])ret)[i] = is.readFloat();
|
---|
880 | break;
|
---|
881 | case CHAR_ARRAY:
|
---|
882 | size = Utils.unpackInt(is);
|
---|
883 | ret = new char[size];
|
---|
884 | for(int i=0;i<size;i++) ((char[])ret)[i] = is.readChar();
|
---|
885 | break;
|
---|
886 | case CHAR:
|
---|
887 | ret = is.readChar();
|
---|
888 | break;
|
---|
889 | case FLOAT_MINUS_1:
|
---|
890 | ret = (float) -1;
|
---|
891 | break;
|
---|
892 | case FLOAT_0:
|
---|
893 | ret = (float) 0;
|
---|
894 | break;
|
---|
895 | case FLOAT_1:
|
---|
896 | ret = (float) 1;
|
---|
897 | break;
|
---|
898 | case FLOAT_255:
|
---|
899 | ret = (float) is.readUnsignedByte();
|
---|
900 | break;
|
---|
901 | case FLOAT_SHORT:
|
---|
902 | ret = (float) is.readShort();
|
---|
903 | break;
|
---|
904 | case FLOAT_FULL:
|
---|
905 | ret = is.readFloat();
|
---|
906 | break;
|
---|
907 | case DOUBLE_MINUS_1:
|
---|
908 | ret = (double) -1;
|
---|
909 | break;
|
---|
910 | case DOUBLE_0:
|
---|
911 | ret = (double) 0;
|
---|
912 | break;
|
---|
913 | case DOUBLE_1:
|
---|
914 | ret = (double) 1;
|
---|
915 | break;
|
---|
916 | case DOUBLE_255:
|
---|
917 | ret = (double) is.readUnsignedByte();
|
---|
918 | break;
|
---|
919 | case DOUBLE_SHORT:
|
---|
920 | ret = (double) is.readShort();
|
---|
921 | break;
|
---|
922 | case DOUBLE_FULL:
|
---|
923 | ret = is.readDouble();
|
---|
924 | break;
|
---|
925 | case BIGINTEGER:
|
---|
926 | ret = new BigInteger(deserializeArrayByte(is));
|
---|
927 | break;
|
---|
928 | case BIGDECIMAL:
|
---|
929 | ret = new BigDecimal(new BigInteger(deserializeArrayByte(is)), Utils.unpackInt(is));
|
---|
930 | break;
|
---|
931 | case STRING:
|
---|
932 | ret = deserializeString(is);
|
---|
933 | break;
|
---|
934 | case STRING_EMPTY:
|
---|
935 | ret = Utils.EMPTY_STRING;
|
---|
936 | break;
|
---|
937 | case CLASS:
|
---|
938 | ret = deserializeClass(is);
|
---|
939 | break;
|
---|
940 | case DATE:
|
---|
941 | ret = new Date(is.readLong());
|
---|
942 | break;
|
---|
943 | case UUID:
|
---|
944 | ret = new UUID(is.readLong(), is.readLong());
|
---|
945 | break;
|
---|
946 | case ARRAY_INT_B_255:
|
---|
947 | ret = deserializeArrayIntB255(is);
|
---|
948 | break;
|
---|
949 | case ARRAY_INT_B_INT:
|
---|
950 | ret = deserializeArrayIntBInt(is);
|
---|
951 | break;
|
---|
952 | case ARRAY_INT_S:
|
---|
953 | ret = deserializeArrayIntSInt(is);
|
---|
954 | break;
|
---|
955 | case ARRAY_INT_I:
|
---|
956 | ret = deserializeArrayIntIInt(is);
|
---|
957 | break;
|
---|
958 | case ARRAY_INT_PACKED:
|
---|
959 | ret = deserializeArrayIntPack(is);
|
---|
960 | break;
|
---|
961 | case ARRAY_LONG_B:
|
---|
962 | ret = deserializeArrayLongB(is);
|
---|
963 | break;
|
---|
964 | case ARRAY_LONG_S:
|
---|
965 | ret = deserializeArrayLongS(is);
|
---|
966 | break;
|
---|
967 | case ARRAY_LONG_I:
|
---|
968 | ret = deserializeArrayLongI(is);
|
---|
969 | break;
|
---|
970 | case ARRAY_LONG_L:
|
---|
971 | ret = deserializeArrayLongL(is);
|
---|
972 | break;
|
---|
973 | case ARRAY_LONG_PACKED:
|
---|
974 | ret = deserializeArrayLongPack(is);
|
---|
975 | break;
|
---|
976 | case ARRAYLIST_PACKED_LONG:
|
---|
977 | ret = deserializeArrayListPackedLong(is);
|
---|
978 | break;
|
---|
979 | case ARRAY_BYTE_ALL_EQUAL:
|
---|
980 | byte[] b = new byte[Utils.unpackInt(is)];
|
---|
981 | Arrays.fill(b, is.readByte());
|
---|
982 | ret = b;
|
---|
983 | break;
|
---|
984 | case ARRAY_BYTE:
|
---|
985 | ret = deserializeArrayByte(is);
|
---|
986 | break;
|
---|
987 | case LOCALE :
|
---|
988 | ret = new Locale(is.readUTF(),is.readUTF(),is.readUTF());
|
---|
989 | break;
|
---|
990 | case COMPARABLE_COMPARATOR:
|
---|
991 | ret = Utils.COMPARABLE_COMPARATOR;
|
---|
992 | break;
|
---|
993 | case SerializationHeader.LONG_SERIALIZER:
|
---|
994 | ret = LONG_SERIALIZER;
|
---|
995 | break;
|
---|
996 | case SerializationHeader.INTEGER_SERIALIZER:
|
---|
997 | ret = INTEGER_SERIALIZER;
|
---|
998 | break;
|
---|
999 | case SerializationHeader.EMPTY_SERIALIZER:
|
---|
1000 | ret = EMPTY_SERIALIZER;
|
---|
1001 | break;
|
---|
1002 | case SerializationHeader.CRC32_SERIALIZER:
|
---|
1003 | ret = Serializer.CRC32_CHECKSUM;
|
---|
1004 | break;
|
---|
1005 | case B_TREE_SERIALIZER_POS_LONG:
|
---|
1006 | ret = BTreeKeySerializer.ZERO_OR_POSITIVE_LONG;
|
---|
1007 | break;
|
---|
1008 | case B_TREE_SERIALIZER_POS_INT:
|
---|
1009 | ret = BTreeKeySerializer.ZERO_OR_POSITIVE_INT;
|
---|
1010 | break;
|
---|
1011 | case B_TREE_SERIALIZER_STRING:
|
---|
1012 | ret = BTreeKeySerializer.STRING;
|
---|
1013 | break;
|
---|
1014 | case COMPARABLE_COMPARATOR_WITH_NULLS:
|
---|
1015 | ret = Utils.COMPARABLE_COMPARATOR_WITH_NULLS;
|
---|
1016 | break;
|
---|
1017 | case B_TREE_BASIC_KEY_SERIALIZER:
|
---|
1018 | ret = new BTreeKeySerializer.BasicKeySerializer(this);
|
---|
1019 | break;
|
---|
1020 | case SerializationHeader.BASIC_SERIALIZER:
|
---|
1021 | ret = BASIC_SERIALIZER;
|
---|
1022 | break;
|
---|
1023 | case SerializationHeader.STRING_SERIALIZER:
|
---|
1024 | ret = Serializer.STRING_SERIALIZER;
|
---|
1025 | break;
|
---|
1026 | case TUPLE2:
|
---|
1027 | ret = new Fun.Tuple2(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1028 | break;
|
---|
1029 | case TUPLE3:
|
---|
1030 | ret = new Fun.Tuple3(deserialize(is, objectStack), deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1031 | break;
|
---|
1032 | case TUPLE4:
|
---|
1033 | ret = new Fun.Tuple4(deserialize(is, objectStack), deserialize(is, objectStack), deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1034 | break;
|
---|
1035 | case FUN_HI:
|
---|
1036 | ret = Fun.HI;
|
---|
1037 | break;
|
---|
1038 | case THIS_SERIALIZER:
|
---|
1039 | ret = this;
|
---|
1040 | break;
|
---|
1041 | case JAVA_SERIALIZATION:
|
---|
1042 | throw new InternalError("Wrong header, data were probably serialized with java.lang.ObjectOutputStream, not with JDBM serialization");
|
---|
1043 | case ARRAY_OBJECT_PACKED_LONG:
|
---|
1044 | ret = deserializeArrayObjectPackedLong(is);
|
---|
1045 | break;
|
---|
1046 | case ARRAY_OBJECT_ALL_NULL:
|
---|
1047 | ret = deserializeArrayObjectAllNull(is);
|
---|
1048 | break;
|
---|
1049 | case ARRAY_OBJECT_NO_REFS:
|
---|
1050 | ret = deserializeArrayObjectNoRefs(is);
|
---|
1051 | break;
|
---|
1052 |
|
---|
1053 | case -1:
|
---|
1054 | throw new EOFException();
|
---|
1055 |
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | if (ret != null || head == NULL) {
|
---|
1059 | if (objectStack != null)
|
---|
1060 | objectStack.add(ret);
|
---|
1061 | return ret;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /** something else which needs object stack initialized*/
|
---|
1065 |
|
---|
1066 | if (objectStack == null)
|
---|
1067 | objectStack = new FastArrayList<>();
|
---|
1068 | int oldObjectStackSize = objectStack.size();
|
---|
1069 |
|
---|
1070 | switch (head) {
|
---|
1071 | case OBJECT_STACK:
|
---|
1072 | ret = objectStack.get(Utils.unpackInt(is));
|
---|
1073 | break;
|
---|
1074 | case ARRAYLIST:
|
---|
1075 | ret = deserializeArrayList(is, objectStack);
|
---|
1076 | break;
|
---|
1077 | case ARRAY_OBJECT:
|
---|
1078 | ret = deserializeArrayObject(is, objectStack);
|
---|
1079 | break;
|
---|
1080 | case LINKEDLIST:
|
---|
1081 | ret = deserializeLinkedList(is, objectStack);
|
---|
1082 | break;
|
---|
1083 | case TREESET:
|
---|
1084 | ret = deserializeTreeSet(is, objectStack);
|
---|
1085 | break;
|
---|
1086 | case HASHSET:
|
---|
1087 | ret = deserializeHashSet(is, objectStack);
|
---|
1088 | break;
|
---|
1089 | case LINKEDHASHSET:
|
---|
1090 | ret = deserializeLinkedHashSet(is, objectStack);
|
---|
1091 | break;
|
---|
1092 | case VECTOR:
|
---|
1093 | ret = deserializeVector(is, objectStack);
|
---|
1094 | break;
|
---|
1095 | case TREEMAP:
|
---|
1096 | ret = deserializeTreeMap(is, objectStack);
|
---|
1097 | break;
|
---|
1098 | case HASHMAP:
|
---|
1099 | ret = deserializeHashMap(is, objectStack);
|
---|
1100 | break;
|
---|
1101 | case IDENTITYHASHMAP:
|
---|
1102 | ret = deserializeIdentityHashMap(is, objectStack);
|
---|
1103 | break;
|
---|
1104 | case LINKEDHASHMAP:
|
---|
1105 | ret = deserializeLinkedHashMap(is, objectStack);
|
---|
1106 | break;
|
---|
1107 | case HASHTABLE:
|
---|
1108 | ret = deserializeHashtable(is, objectStack);
|
---|
1109 | break;
|
---|
1110 | case PROPERTIES:
|
---|
1111 | ret = deserializeProperties(is, objectStack);
|
---|
1112 | break;
|
---|
1113 | case SERIALIZER_COMPRESSION_WRAPPER:
|
---|
1114 | ret = CompressLZF.CompressionWrapper((Serializer<?>) deserialize(is, objectStack));
|
---|
1115 | break;
|
---|
1116 | default:
|
---|
1117 | ret = deserializeUnknownHeader(is, head, objectStack);
|
---|
1118 | break;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | if (head != OBJECT_STACK && objectStack.size() == oldObjectStackSize) {
|
---|
1122 | //check if object was not already added to stack as part of collection
|
---|
1123 | objectStack.add(ret);
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | return ret;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | private byte[] deserializeArrayByte(DataInput is) throws IOException {
|
---|
1131 | byte[] bb = new byte[Utils.unpackInt(is)];
|
---|
1132 | is.readFully(bb);
|
---|
1133 | return bb;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 |
|
---|
1137 | protected Class deserializeClass(DataInput is) throws IOException {
|
---|
1138 | //TODO override 'deserializeClass' in SerializerPojo
|
---|
1139 | try {
|
---|
1140 | return Class.forName(is.readUTF());
|
---|
1141 | } catch (ClassNotFoundException e) {
|
---|
1142 | throw new RuntimeException(e);
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 |
|
---|
1147 |
|
---|
1148 |
|
---|
1149 | private long[] deserializeArrayLongL(DataInput is) throws IOException {
|
---|
1150 | int size = Utils.unpackInt(is);
|
---|
1151 | long[] ret = new long[size];
|
---|
1152 | for (int i = 0; i < size; i++)
|
---|
1153 | ret[i] = is.readLong();
|
---|
1154 | return ret;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 |
|
---|
1158 | private long[] deserializeArrayLongI(DataInput is) throws IOException {
|
---|
1159 | int size = Utils.unpackInt(is);
|
---|
1160 | long[] ret = new long[size];
|
---|
1161 | for (int i = 0; i < size; i++)
|
---|
1162 | ret[i] = is.readInt();
|
---|
1163 | return ret;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 |
|
---|
1167 | private long[] deserializeArrayLongS(DataInput is) throws IOException {
|
---|
1168 | int size = Utils.unpackInt(is);
|
---|
1169 | long[] ret = new long[size];
|
---|
1170 | for (int i = 0; i < size; i++)
|
---|
1171 | ret[i] = is.readShort();
|
---|
1172 | return ret;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 |
|
---|
1176 | private long[] deserializeArrayLongB(DataInput is) throws IOException {
|
---|
1177 | int size = Utils.unpackInt(is);
|
---|
1178 | long[] ret = new long[size];
|
---|
1179 | for (int i = 0; i < size; i++) {
|
---|
1180 | ret[i] = is.readUnsignedByte();
|
---|
1181 | if (ret[i] < 0)
|
---|
1182 | throw new EOFException();
|
---|
1183 | }
|
---|
1184 | return ret;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 |
|
---|
1188 | private int[] deserializeArrayIntIInt(DataInput is) throws IOException {
|
---|
1189 | int size = Utils.unpackInt(is);
|
---|
1190 | int[] ret = new int[size];
|
---|
1191 | for (int i = 0; i < size; i++)
|
---|
1192 | ret[i] = is.readInt();
|
---|
1193 | return ret;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | private int[] deserializeArrayIntSInt(DataInput is) throws IOException {
|
---|
1198 | int size = Utils.unpackInt(is);
|
---|
1199 | int[] ret = new int[size];
|
---|
1200 | for (int i = 0; i < size; i++)
|
---|
1201 | ret[i] = is.readShort();
|
---|
1202 | return ret;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 |
|
---|
1206 | private int[] deserializeArrayIntBInt(DataInput is) throws IOException {
|
---|
1207 | int size = Utils.unpackInt(is);
|
---|
1208 | int[] ret = new int[size];
|
---|
1209 | for (int i = 0; i < size; i++) {
|
---|
1210 | ret[i] = is.readUnsignedByte();
|
---|
1211 | if (ret[i] < 0)
|
---|
1212 | throw new EOFException();
|
---|
1213 | }
|
---|
1214 | return ret;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | private int[] deserializeArrayIntPack(DataInput is) throws IOException {
|
---|
1219 | int size = Utils.unpackInt(is);
|
---|
1220 | if (size < 0)
|
---|
1221 | throw new EOFException();
|
---|
1222 |
|
---|
1223 | int[] ret = new int[size];
|
---|
1224 | for (int i = 0; i < size; i++) {
|
---|
1225 | ret[i] = Utils.unpackInt(is);
|
---|
1226 | }
|
---|
1227 | return ret;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | private long[] deserializeArrayLongPack(DataInput is) throws IOException {
|
---|
1231 | int size = Utils.unpackInt(is);
|
---|
1232 | if (size < 0)
|
---|
1233 | throw new EOFException();
|
---|
1234 |
|
---|
1235 | long[] ret = new long[size];
|
---|
1236 | for (int i = 0; i < size; i++) {
|
---|
1237 | ret[i] = Utils.unpackLong(is);
|
---|
1238 | }
|
---|
1239 | return ret;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | private int[] deserializeArrayIntB255(DataInput is) throws IOException {
|
---|
1243 | int size = is.readUnsignedByte();
|
---|
1244 | if (size < 0)
|
---|
1245 | throw new EOFException();
|
---|
1246 |
|
---|
1247 | int[] ret = new int[size];
|
---|
1248 | for (int i = 0; i < size; i++) {
|
---|
1249 | ret[i] = is.readUnsignedByte();
|
---|
1250 | if (ret[i] < 0)
|
---|
1251 | throw new EOFException();
|
---|
1252 | }
|
---|
1253 | return ret;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 |
|
---|
1257 | private Object[] deserializeArrayObject(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1258 | int size = Utils.unpackInt(is);
|
---|
1259 | Class clazz = deserializeClass(is);
|
---|
1260 | Object[] s = (Object[]) Array.newInstance(clazz, size);
|
---|
1261 | objectStack.add(s);
|
---|
1262 | for (int i = 0; i < size; i++){
|
---|
1263 | s[i] = deserialize(is, objectStack);
|
---|
1264 | }
|
---|
1265 | return s;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | private Object[] deserializeArrayObjectNoRefs(DataInput is) throws IOException {
|
---|
1269 | int size = Utils.unpackInt(is);
|
---|
1270 | Class clazz = deserializeClass(is);
|
---|
1271 | Object[] s = (Object[]) Array.newInstance(clazz, size);
|
---|
1272 | for (int i = 0; i < size; i++){
|
---|
1273 | s[i] = deserialize(is, null);
|
---|
1274 | }
|
---|
1275 | return s;
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 |
|
---|
1279 | private Object[] deserializeArrayObjectAllNull(DataInput is) throws IOException {
|
---|
1280 | int size = Utils.unpackInt(is);
|
---|
1281 | Class clazz = deserializeClass(is);
|
---|
1282 | Object[] s = (Object[]) Array.newInstance(clazz, size);
|
---|
1283 | return s;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | private Object[] deserializeArrayObjectPackedLong(DataInput is) throws IOException {
|
---|
1288 | int size = is.readUnsignedByte();
|
---|
1289 | Object[] s = new Object[size];
|
---|
1290 | for (int i = 0; i < size; i++) {
|
---|
1291 | long l = Utils.unpackLong(is);
|
---|
1292 | if (l == 0)
|
---|
1293 | s[i] = null;
|
---|
1294 | else
|
---|
1295 | s[i] = l - 1;
|
---|
1296 | }
|
---|
1297 | return s;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 |
|
---|
1301 | private ArrayList<Object> deserializeArrayList(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1302 | int size = Utils.unpackInt(is);
|
---|
1303 | ArrayList<Object> s = new ArrayList<Object>(size);
|
---|
1304 | objectStack.add(s);
|
---|
1305 | for (int i = 0; i < size; i++) {
|
---|
1306 | s.add(deserialize(is, objectStack));
|
---|
1307 | }
|
---|
1308 | return s;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | private ArrayList<Object> deserializeArrayListPackedLong(DataInput is) throws IOException {
|
---|
1312 | int size = is.readUnsignedByte();
|
---|
1313 | if (size < 0)
|
---|
1314 | throw new EOFException();
|
---|
1315 |
|
---|
1316 | ArrayList<Object> s = new ArrayList<Object>(size);
|
---|
1317 | for (int i = 0; i < size; i++) {
|
---|
1318 | long l = Utils.unpackLong(is);
|
---|
1319 | if (l == 0)
|
---|
1320 | s.add(null);
|
---|
1321 | else
|
---|
1322 | s.add(l - 1);
|
---|
1323 | }
|
---|
1324 | return s;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 |
|
---|
1328 | private java.util.LinkedList deserializeLinkedList(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1329 | int size = Utils.unpackInt(is);
|
---|
1330 | java.util.LinkedList s = new java.util.LinkedList();
|
---|
1331 | objectStack.add(s);
|
---|
1332 | for (int i = 0; i < size; i++)
|
---|
1333 | s.add(deserialize(is, objectStack));
|
---|
1334 | return s;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 |
|
---|
1338 | private Vector<Object> deserializeVector(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1339 | int size = Utils.unpackInt(is);
|
---|
1340 | Vector<Object> s = new Vector<Object>(size);
|
---|
1341 | objectStack.add(s);
|
---|
1342 | for (int i = 0; i < size; i++)
|
---|
1343 | s.add(deserialize(is, objectStack));
|
---|
1344 | return s;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 |
|
---|
1348 | private HashSet<Object> deserializeHashSet(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1349 | int size = Utils.unpackInt(is);
|
---|
1350 | HashSet<Object> s = new HashSet<Object>(size);
|
---|
1351 | objectStack.add(s);
|
---|
1352 | for (int i = 0; i < size; i++)
|
---|
1353 | s.add(deserialize(is, objectStack));
|
---|
1354 | return s;
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 |
|
---|
1358 | private LinkedHashSet<Object> deserializeLinkedHashSet(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1359 | int size = Utils.unpackInt(is);
|
---|
1360 | LinkedHashSet<Object> s = new LinkedHashSet<Object>(size);
|
---|
1361 | objectStack.add(s);
|
---|
1362 | for (int i = 0; i < size; i++)
|
---|
1363 | s.add(deserialize(is, objectStack));
|
---|
1364 | return s;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 |
|
---|
1368 | private TreeSet<Object> deserializeTreeSet(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1369 | int size = Utils.unpackInt(is);
|
---|
1370 | TreeSet<Object> s = new TreeSet<Object>();
|
---|
1371 | objectStack.add(s);
|
---|
1372 | Comparator comparator = (Comparator) deserialize(is, objectStack);
|
---|
1373 | if (comparator != null)
|
---|
1374 | s = new TreeSet<Object>(comparator);
|
---|
1375 |
|
---|
1376 | for (int i = 0; i < size; i++)
|
---|
1377 | s.add(deserialize(is, objectStack));
|
---|
1378 | return s;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 |
|
---|
1382 | private TreeMap<Object, Object> deserializeTreeMap(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1383 | int size = Utils.unpackInt(is);
|
---|
1384 |
|
---|
1385 | TreeMap<Object, Object> s = new TreeMap<Object, Object>();
|
---|
1386 | objectStack.add(s);
|
---|
1387 | Comparator comparator = (Comparator) deserialize(is, objectStack);
|
---|
1388 | if (comparator != null)
|
---|
1389 | s = new TreeMap<Object, Object>(comparator);
|
---|
1390 | for (int i = 0; i < size; i++)
|
---|
1391 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1392 | return s;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 |
|
---|
1396 | private HashMap<Object, Object> deserializeHashMap(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1397 | int size = Utils.unpackInt(is);
|
---|
1398 |
|
---|
1399 | HashMap<Object, Object> s = new HashMap<Object, Object>(size);
|
---|
1400 | objectStack.add(s);
|
---|
1401 | for (int i = 0; i < size; i++)
|
---|
1402 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1403 | return s;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | private IdentityHashMap<Object, Object> deserializeIdentityHashMap(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1407 | int size = Utils.unpackInt(is);
|
---|
1408 |
|
---|
1409 | IdentityHashMap<Object, Object> s = new IdentityHashMap<Object, Object>(size);
|
---|
1410 | objectStack.add(s);
|
---|
1411 | for (int i = 0; i < size; i++)
|
---|
1412 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1413 | return s;
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | private LinkedHashMap<Object, Object> deserializeLinkedHashMap(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1417 | int size = Utils.unpackInt(is);
|
---|
1418 |
|
---|
1419 | LinkedHashMap<Object, Object> s = new LinkedHashMap<Object, Object>(size);
|
---|
1420 | objectStack.add(s);
|
---|
1421 | for (int i = 0; i < size; i++)
|
---|
1422 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1423 | return s;
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 |
|
---|
1427 | private Hashtable<Object, Object> deserializeHashtable(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1428 | int size = Utils.unpackInt(is);
|
---|
1429 |
|
---|
1430 | Hashtable<Object, Object> s = new Hashtable<Object, Object>(size);
|
---|
1431 | objectStack.add(s);
|
---|
1432 | for (int i = 0; i < size; i++)
|
---|
1433 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1434 | return s;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 |
|
---|
1438 | private Properties deserializeProperties(DataInput is, FastArrayList<Object> objectStack) throws IOException {
|
---|
1439 | int size = Utils.unpackInt(is);
|
---|
1440 |
|
---|
1441 | Properties s = new Properties();
|
---|
1442 | objectStack.add(s);
|
---|
1443 | for (int i = 0; i < size; i++)
|
---|
1444 | s.put(deserialize(is, objectStack), deserialize(is, objectStack));
|
---|
1445 | return s;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | /** override this method to extend SerializerBase functionality*/
|
---|
1449 | protected void serializeUnknownObject(DataOutput out, Object obj, FastArrayList<Object> objectStack) throws IOException {
|
---|
1450 | throw new InternalError("Could not deserialize unknown object: "+obj.getClass().getName());
|
---|
1451 | }
|
---|
1452 | /** override this method to extend SerializerBase functionality*/
|
---|
1453 | protected Object deserializeUnknownHeader(DataInput is, int head, FastArrayList<Object> objectStack) throws IOException {
|
---|
1454 | throw new InternalError("Unknown serialization header: " + head);
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 |
|
---|
1458 | }
|
---|