-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathFSTObjectInput.java
More file actions
1500 lines (1331 loc) · 58 KB
/
FSTObjectInput.java
File metadata and controls
1500 lines (1331 loc) · 58 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 Ruediger Moeller.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nustaq.serialization;
import org.nustaq.logging.FSTLogger;
import org.nustaq.serialization.coders.Unknown;
import org.nustaq.serialization.minbin.MBObject;
import org.nustaq.serialization.util.FSTUtil;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: Möller
* Date: 04.11.12
* Time: 11:53
*/
/**
* replacement of ObjectInputStream
*/
public class FSTObjectInput implements ObjectInput {
private static final FSTLogger logger = FSTLogger.getLogger(FSTObjectInput.class);
public static boolean REGISTER_ENUMS_READ = false; // do not register enums on read. Flag is saver in case things brake somewhere
public static ByteArrayInputStream emptyStream = new ByteArrayInputStream(new byte[0]);
protected FSTDecoder codec;
protected FSTObjectRegistry objects;
protected Stack<String> debugStack;
protected int curDepth;
protected ArrayList<CallbackEntry> callbacks;
// FSTConfiguration conf;
// mirrored from conf
protected boolean ignoreAnnotations;
protected FSTClazzInfoRegistry clInfoRegistry;
// done
protected ConditionalCallback conditionalCallback;
protected int readExternalReadAHead = 8000;
protected VersionConflictListener versionConflictListener;
protected FSTConfiguration conf;
// copied values from conf
protected boolean isCrossPlatform;
public FSTConfiguration getConf() {
return conf;
}
@Override
public void readFully(byte[] b) throws IOException {
readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
getCodec().readPlainBytes(b, off, len);
}
@Override
public int skipBytes(int n) throws IOException {
getCodec().skip(n);
return n;
}
@Override
public boolean readBoolean() throws IOException {
return getCodec().readFByte() == 0 ? false : true;
}
@Override
public byte readByte() throws IOException {
return getCodec().readFByte();
}
@Override
public int readUnsignedByte() throws IOException {
return ((int) getCodec().readFByte()+256) & 0xff;
}
@Override
public short readShort() throws IOException {
return getCodec().readFShort();
}
@Override
public int readUnsignedShort() throws IOException {
return ((int)readShort()+65536) & 0xffff;
}
@Override
public char readChar() throws IOException {
return getCodec().readFChar();
}
@Override
public int readInt() throws IOException {
return getCodec().readFInt();
}
@Override
public long readLong() throws IOException {
return getCodec().readFLong();
}
@Override
public float readFloat() throws IOException {
return getCodec().readFFloat();
}
@Override
public double readDouble() throws IOException {
return getCodec().readFDouble();
}
@Override
public String readLine() throws IOException {
throw new RuntimeException("not implemented");
}
@Override
public String readUTF() throws IOException {
return getCodec().readStringUTF();
}
public FSTDecoder getCodec() {
return codec;
}
protected void setCodec(FSTDecoder codec) {
this.codec = codec;
}
public boolean isClosed() {
return closed;
}
protected static class CallbackEntry {
ObjectInputValidation cb;
int prio;
CallbackEntry(ObjectInputValidation cb, int prio) {
this.cb = cb;
this.prio = prio;
}
}
public static interface ConditionalCallback {
public boolean shouldSkip(Object halfDecoded, int streamPosition, Field field);
}
public FSTObjectInput() throws IOException {
this(emptyStream, FSTConfiguration.getDefaultConfiguration());
}
public FSTObjectInput(FSTConfiguration conf) {
this(emptyStream, conf);
}
/**
* Creates a FSTObjectInput that uses the specified
* underlying InputStream.
*
* @param in the specified input stream
*/
public FSTObjectInput(InputStream in) throws IOException {
this(in, FSTConfiguration.getDefaultConfiguration());
}
/**
* Creates a FSTObjectInput that uses the specified
* underlying InputStream.
*
* Don't create a FSTConfiguration with each stream, just create one global static configuration and reuseit.
* FSTConfiguration is threadsafe.
*
* @param in the specified input stream
*/
public FSTObjectInput(InputStream in, FSTConfiguration conf) {
setCodec(conf.createStreamDecoder());
getCodec().setInputStream(in);
isCrossPlatform = conf.isCrossPlatform();
initRegistries(conf);
this.conf = conf;
}
public Class getClassForName(String name) throws ClassNotFoundException {
return getCodec().classForName(name);
}
protected void initRegistries(FSTConfiguration conf) {
ignoreAnnotations = conf.getCLInfoRegistry().isIgnoreAnnotations();
clInfoRegistry = conf.getCLInfoRegistry();
objects = (FSTObjectRegistry) conf.getCachedObject(FSTObjectRegistry.class);
if (objects == null) {
objects = new FSTObjectRegistry(conf);
} else {
objects.clearForRead(conf);
}
}
public ConditionalCallback getConditionalCallback() {
return conditionalCallback;
}
public void setConditionalCallback(ConditionalCallback conditionalCallback) {
this.conditionalCallback = conditionalCallback;
}
public int getReadExternalReadAHead() {
return readExternalReadAHead;
}
/**
* since the stock readXX methods on InputStream are final, i can't ensure sufficient readAhead on the inputStream
* before calling readExternal. Default value is 16000 bytes. If you make use of the externalizable interfac
* and write larger Objects a) cast the ObjectInput in readExternal to FSTObjectInput and call ensureReadAhead on this
* in your readExternal method b) set a sufficient maximum using this method before serializing.
* @param readExternalReadAHead
*/
public void setReadExternalReadAHead(int readExternalReadAHead) {
this.readExternalReadAHead = readExternalReadAHead;
}
@Override
public Object readObject() throws ClassNotFoundException, IOException {
try {
return readObject((Class[]) null);
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public int read() throws IOException {
return getCodec().readIntByte();
}
@Override
public int read(byte[] b) throws IOException {
getCodec().readPlainBytes(b, 0, b.length);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
getCodec().readPlainBytes(b, off, len);
return b.length;
}
@Override
public long skip(long n) throws IOException {
getCodec().skip((int) n);
return n;
}
@Override
public int available() throws IOException {
return getCodec().available();
}
protected void processValidation() throws InvalidObjectException {
if (callbacks == null) {
return;
}
Collections.sort(callbacks, new Comparator<CallbackEntry>() {
@Override
public int compare(CallbackEntry o1, CallbackEntry o2) {
return o2.prio - o1.prio;
}
});
for (int i = 0; i < callbacks.size(); i++) {
CallbackEntry callbackEntry = callbacks.get(i);
try {
callbackEntry.cb.validateObject();
} catch (Exception ex) {
FSTUtil.<RuntimeException>rethrow(ex);
}
}
}
public Object readObject(Class... possibles) throws Exception {
curDepth++;
if ( isCrossPlatform ) {
return readObjectInternal(null); // not supported cross platform
}
try {
if (possibles != null && possibles.length > 1 ) {
for (int i = 0; i < possibles.length; i++) {
Class possible = possibles[i];
getCodec().registerClass(possible);
}
}
Object res = readObjectInternal(possibles);
processValidation();
return res;
} catch (Throwable th) {
FSTUtil.<RuntimeException>rethrow(th);
} finally {
curDepth--;
}
return null;
}
protected FSTClazzInfo.FSTFieldInfo infoCache;
public Object readObjectInternal(Class... expected) throws ClassNotFoundException, IOException, IllegalAccessException, InstantiationException {
try {
FSTClazzInfo.FSTFieldInfo info = infoCache;
infoCache = null;
if (info == null )
info = new FSTClazzInfo.FSTFieldInfo(expected, null, ignoreAnnotations);
else
info.possibleClasses = expected;
Object res = readObjectWithHeader(info);
infoCache = info;
return res;
} catch (Throwable t) {
FSTUtil.<RuntimeException>rethrow(t);
}
return null;
}
public Object readObjectWithHeader(FSTClazzInfo.FSTFieldInfo referencee) throws Exception {
FSTClazzInfo clzSerInfo;
Class c;
final int readPos = getCodec().getInputPos();
byte code = getCodec().readObjectHeaderTag(); // NOTICE: THIS ADVANCES THE INPUT STREAM...
if (code == FSTObjectOutput.OBJECT ) {
// class name
clzSerInfo = readClass();
c = clzSerInfo.getClazz();
if ( c.isArray() )
return readArrayNoHeader(referencee,readPos,c);
// fall through
} else if ( code == FSTObjectOutput.TYPED ) {
c = referencee.getType();
clzSerInfo = getClazzInfo(c, referencee);
} else if ( code >= 1 ) {
try {
c = referencee.getPossibleClasses()[code - 1];
clzSerInfo = getClazzInfo(c, referencee);
} catch (Throwable th) {
clzSerInfo = null; c = null;
FSTUtil.<RuntimeException>rethrow(th);
}
} else {
Object res = instantiateSpecialTag(referencee, readPos, code);
return res;
}
try {
FSTObjectSerializer ser = clzSerInfo.getSer();
if (ser != null) {
Object res = instantiateAndReadWithSer(c, ser, clzSerInfo, referencee, readPos);
getCodec().readArrayEnd(clzSerInfo);
return res;
} else {
Object res = instantiateAndReadNoSer(c, clzSerInfo, referencee, readPos);
return res;
}
} catch (Exception e) {
FSTUtil.<RuntimeException>rethrow(e);
}
return null;
}
protected Object instantiateSpecialTag(FSTClazzInfo.FSTFieldInfo referencee, int readPos, byte code) throws Exception {
if ( code == FSTObjectOutput.STRING ) { // faster than switch, note: currently string tag not used by all codecs ..
String res = getCodec().readStringUTF();
objects.registerObjectForRead(res, readPos);
return res;
} else if ( code == FSTObjectOutput.BIG_INT ) {
return instantiateBigInt();
} else if ( code == FSTObjectOutput.NULL ) {
return null;
} else
{
switch (code) {
// case FSTObjectOutput.BIG_INT: { return instantiateBigInt(); }
case FSTObjectOutput.BIG_LONG: { return Long.valueOf(getCodec().readFLong()); }
case FSTObjectOutput.BIG_BOOLEAN_FALSE: { return Boolean.FALSE; }
case FSTObjectOutput.BIG_BOOLEAN_TRUE: { return Boolean.TRUE; }
case FSTObjectOutput.ONE_OF: { return referencee.getOneOf()[getCodec().readFByte()]; }
// case FSTObjectOutput.NULL: { return null; }
case FSTObjectOutput.DIRECT_ARRAY_OBJECT: {
Object directObject = getCodec().getDirectObject();
objects.registerObjectForRead(directObject,readPos);
return directObject;
}
case FSTObjectOutput.DIRECT_OBJECT: {
Object directObject = getCodec().getDirectObject();
if (directObject.getClass() == byte[].class) { // fixme. special for minibin, move it there
if ( referencee != null && referencee.getType() == boolean[].class )
{
byte[] ba = (byte[]) directObject;
boolean res[] = new boolean[ba.length];
for (int i = 0; i < res.length; i++) {
res[i] = ba[i] != 0;
}
directObject = res;
}
}
objects.registerObjectForRead(directObject,readPos);
return directObject;
}
// case FSTObjectOutput.STRING: return getCodec().readStringUTF();
case FSTObjectOutput.HANDLE: {
Object res = instantiateHandle(referencee);
getCodec().readObjectEnd();
return res;
}
case FSTObjectOutput.ARRAY: {
Object res = instantiateArray(referencee, readPos);
return res;
}
case FSTObjectOutput.ENUM: { return instantiateEnum(referencee, readPos); }
}
throw new RuntimeException("unknown object tag "+code);
}
}
protected FSTClazzInfo getClazzInfo(Class c, FSTClazzInfo.FSTFieldInfo referencee) {
FSTClazzInfo clzSerInfo;
FSTClazzInfo lastInfo = referencee.lastInfo;
if ( lastInfo != null && lastInfo.clazz == c && lastInfo.conf == conf) {
clzSerInfo = lastInfo;
} else {
clzSerInfo = clInfoRegistry.getCLInfo(c, conf);
referencee.lastInfo = clzSerInfo;
}
return clzSerInfo;
}
protected Object instantiateHandle(FSTClazzInfo.FSTFieldInfo referencee) throws IOException {
int handle = getCodec().readFInt();
Object res = objects.getReadRegisteredObject(handle);
if (res == null) {
throw new IOException("unable to ressolve handle " + handle + " " + referencee.getDesc() + " " + getCodec().getInputPos() );
}
return res;
}
protected Object instantiateArray(FSTClazzInfo.FSTFieldInfo referencee, int readPos) throws Exception {
Object res = readArray(referencee, readPos); // NEED TO PASS ALONG THE POS FOR THE ARRAY
/*
registerObjectForRead alerady gets called by readArray (and with the proper pos now). that said, I'm unclear
on the intent of the if ( ! referencee.isFlat() ) so I wanted to comment on that
if ( ! referencee.isFlat() ) {
objects.registerObjectForRead(res, readPos);
}
*/
return res;
}
protected Object instantiateEnum(FSTClazzInfo.FSTFieldInfo referencee, int readPos) throws IOException, ClassNotFoundException {
FSTClazzInfo clzSerInfo;
Class c;
clzSerInfo = readClass();
c = clzSerInfo.getClazz();
int ordinal = getCodec().readFInt();
Object[] enumConstants = clzSerInfo.getEnumConstants();
if ( enumConstants == null ) {
// pseudo enum of anonymous classes tom style ?
return null;
}
Object res = enumConstants[ordinal];
if ( REGISTER_ENUMS_READ ) {
if ( ! referencee.isFlat() ) { // should be unnecessary
objects.registerObjectForRead(res, readPos);
}
}
return res;
}
protected Object instantiateBigInt() throws IOException {
int val = getCodec().readFInt();
return Integer.valueOf(val);
}
protected Object instantiateAndReadWithSer(Class c, FSTObjectSerializer ser, FSTClazzInfo clzSerInfo, FSTClazzInfo.FSTFieldInfo referencee, int readPos) throws Exception {
boolean serInstance = false;
Object newObj = ser.instantiate(c, this, clzSerInfo, referencee, readPos);
if (newObj == null) {
newObj = clzSerInfo.newInstance(getCodec().isMapBased());
} else
serInstance = true;
if (newObj == null) {
throw new IOException(referencee.getDesc() + ":Failed to instantiate '" + c.getName() + "'. Register a custom serializer implementing instantiate or define empty constructor..");
}
if ( newObj == FSTObjectSerializer.REALLY_NULL ) {
newObj = null;
} else {
if (newObj.getClass() != c && ser == null ) {
// for advanced trickery (e.g. returning non-serializable from FSTSerializer)
// this hurts. so in case of FSTSerializers incoming clzInfo will refer to the
// original class, not the one actually instantiated
c = newObj.getClass();
clzSerInfo = clInfoRegistry.getCLInfo(c, conf);
}
if ( ! referencee.isFlat() && ! clzSerInfo.isFlat() && !ser.alwaysCopy()) {
objects.registerObjectForRead(newObj, readPos);
}
if ( !serInstance )
ser.readObject(this, newObj, clzSerInfo, referencee);
}
getCodec().consumeEndMarker(); //=> bug when writing objects unlimited
return newObj;
}
protected Object instantiateAndReadNoSer(Class c, FSTClazzInfo clzSerInfo, FSTClazzInfo.FSTFieldInfo referencee, int readPos) throws Exception {
Object newObj;
newObj = clzSerInfo.newInstance(getCodec().isMapBased());
if (newObj == null) {
throw new IOException(referencee.getDesc() + ":Failed to instantiate '" + c.getName() + "'. Register a custom serializer implementing instantiate or define empty constructor.");
}
//fixme: code below improves unshared decoding perf, however disables to run mixed mode (clients can decide)
//actually would need 2 flags for encode/decode
//tested with json mixed mode does not work anyway ...
final boolean needsRefLookup = conf.shareReferences && !referencee.isFlat() && !clzSerInfo.isFlat();
// previously :
// final boolean needsRefLookup = !referencee.isFlat() && !clzSerInfo.isFlat();
if (needsRefLookup) {
objects.registerObjectForRead(newObj, readPos);
}
if ( clzSerInfo.isExternalizable() )
{
int tmp = readPos;
getCodec().ensureReadAhead(readExternalReadAHead);
((Externalizable)newObj).readExternal(this);
getCodec().readExternalEnd();
if ( clzSerInfo.getReadResolveMethod() != null ) {
final Object prevNew = newObj;
newObj = handleReadRessolve(clzSerInfo, newObj);
if ( newObj != prevNew && needsRefLookup ) {
objects.replace(prevNew, newObj, tmp);
}
}
} else if (clzSerInfo.useCompatibleMode())
{
Object replaced = readObjectCompatible(referencee, clzSerInfo, newObj);
if (replaced != null && replaced != newObj) {
objects.replace(newObj, replaced, readPos);
newObj = replaced;
}
} else {
FSTClazzInfo.FSTFieldInfo[] fieldInfo = clzSerInfo.getFieldInfo();
readObjectFields(referencee, clzSerInfo, fieldInfo, newObj,0,0);
}
return newObj;
}
protected Object readObjectCompatible(FSTClazzInfo.FSTFieldInfo referencee, FSTClazzInfo serializationInfo, Object newObj) throws Exception {
Class cl = serializationInfo.getClazz();
readObjectCompatibleRecursive(referencee, newObj, serializationInfo, cl);
if (newObj != null &&
serializationInfo.getReadResolveMethod() != null) {
newObj = handleReadRessolve(serializationInfo, newObj);
}
return newObj;
}
protected Object handleReadRessolve(FSTClazzInfo serializationInfo, Object newObj) throws IllegalAccessException {
Object rep = null;
try {
rep = serializationInfo.getReadResolveMethod().invoke(newObj);
} catch (InvocationTargetException e) {
FSTUtil.<RuntimeException>rethrow(e);
}
newObj = rep;//FIXME: support this in call
return newObj;
}
protected void readObjectCompatibleRecursive(FSTClazzInfo.FSTFieldInfo referencee, Object toRead, FSTClazzInfo serializationInfo, Class cl) throws Exception {
FSTClazzInfo.FSTCompatibilityInfo fstCompatibilityInfo = serializationInfo.getCompInfo().get(cl);
if (!Serializable.class.isAssignableFrom(cl)) {
return; // ok here, as compatible mode will never be triggered for "forceSerializable"
}
readObjectCompatibleRecursive(referencee, toRead, serializationInfo, cl.getSuperclass());
if (fstCompatibilityInfo != null && fstCompatibilityInfo.getReadMethod() != null) {
try {
int tag = readByte(); // expect 55
if ( tag == 66 ) {
// no write method defined, but read method defined ...
// expect defaultReadObject
getCodec().moveTo(getCodec().getInputPos() - 1); // need to push back tag, cause defaultWriteObject on writer side does not write tag
// input.pos--;
}
ObjectInputStream objectInputStream = getObjectInputStream(cl, serializationInfo, referencee, toRead);
fstCompatibilityInfo.getReadMethod().invoke(toRead, objectInputStream);
fakeWrapper.pop();
} catch (Exception e) {
FSTUtil.<RuntimeException>rethrow(e);
}
} else {
if (fstCompatibilityInfo != null) {
int tag = readByte();
if ( tag == 55 )
{
// came from writeMethod, but no readMethod defined => assume defaultWriteObject
tag = readByte(); // consume tag of defaultwriteobject (99)
if ( tag == 77 ) // came from putfield
{
HashMap<String, Object> fieldMap = (HashMap<String, Object>) FSTObjectInput.this.readObjectInternal(HashMap.class);
final FSTClazzInfo.FSTFieldInfo[] fieldArray = fstCompatibilityInfo.getFieldArray();
for (int i = 0; i < fieldArray.length; i++) {
FSTClazzInfo.FSTFieldInfo fstFieldInfo = fieldArray[i];
final Object val = fieldMap.get(fstFieldInfo.getName());
if ( val != null ) {
fstFieldInfo.setObjectValue(toRead,val);
}
}
return;
}
}
readObjectFields(referencee, serializationInfo, fstCompatibilityInfo.getFieldArray(), toRead,0,0);
}
}
}
public void defaultReadObject(FSTClazzInfo.FSTFieldInfo referencee, FSTClazzInfo serializationInfo, Object newObj)
{
try {
readObjectFields(referencee,serializationInfo,serializationInfo.getFieldInfo(),newObj,0,-1); // -1 flag to indicate no object end should be called
} catch (Exception e) {
FSTUtil.<RuntimeException>rethrow(e);
}
}
protected void readObjectFields(FSTClazzInfo.FSTFieldInfo referencee, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, Object newObj, int startIndex, int version) throws Exception {
if ( getCodec().isMapBased() ) {
readFieldsMapBased(referencee, serializationInfo, newObj);
if ( version >= 0 /*&& newObj instanceof Unknown == false*/ ) {
getCodec().readObjectEnd();
}
return;
}
if ( version < 0 )
version = 0;
int booleanMask = 0;
int boolcount = 8;
final int length = fieldInfo.length;
int conditional = 0;
for (int i = startIndex; i < length; i++) {
try {
FSTClazzInfo.FSTFieldInfo subInfo = fieldInfo[i];
if (subInfo.getVersion() > version ) {
int nextVersion = getCodec().readVersionTag();
if ( nextVersion == 0 ) // old object read
{
oldVersionRead(newObj);
return;
}
if ( nextVersion != subInfo.getVersion() ) {
throw new RuntimeException("read version tag "+nextVersion+" fieldInfo has "+subInfo.getVersion());
}
readObjectFields(referencee,serializationInfo,fieldInfo,newObj,i,nextVersion);
return;
}
if (subInfo.isPrimitive()) {
int integralType = subInfo.getIntegralType();
if (integralType == FSTClazzInfo.FSTFieldInfo.BOOL) {
if (boolcount == 8) {
booleanMask = ((int) getCodec().readFByte() + 256) &0xff;
boolcount = 0;
}
boolean val = (booleanMask & 128) != 0;
booleanMask = booleanMask << 1;
boolcount++;
subInfo.setBooleanValue(newObj, val);
} else {
switch (integralType) {
case FSTClazzInfo.FSTFieldInfo.BYTE: subInfo.setByteValue(newObj, getCodec().readFByte()); break;
case FSTClazzInfo.FSTFieldInfo.CHAR: subInfo.setCharValue(newObj, getCodec().readFChar()); break;
case FSTClazzInfo.FSTFieldInfo.SHORT: subInfo.setShortValue(newObj, getCodec().readFShort()); break;
case FSTClazzInfo.FSTFieldInfo.INT: subInfo.setIntValue(newObj, getCodec().readFInt()); break;
case FSTClazzInfo.FSTFieldInfo.LONG: subInfo.setLongValue(newObj, getCodec().readFLong()); break;
case FSTClazzInfo.FSTFieldInfo.FLOAT: subInfo.setFloatValue(newObj, getCodec().readFFloat()); break;
case FSTClazzInfo.FSTFieldInfo.DOUBLE: subInfo.setDoubleValue(newObj, getCodec().readFDouble()); break;
}
}
} else {
if ( subInfo.isConditional() ) {
if ( conditional == 0 ) {
conditional = getCodec().readPlainInt();
if ( skipConditional(newObj, conditional, subInfo) ) {
getCodec().moveTo(conditional);
continue;
}
}
}
// object
Object subObject = readObjectWithHeader(subInfo);
subInfo.setObjectValue(newObj, subObject);
}
} catch (IllegalAccessException ex) {
throw new IOException(ex);
}
}
int debug = getCodec().readVersionTag();// just consume '0'
}
public VersionConflictListener getVersionConflictListener() {
return versionConflictListener;
}
/**
* see @Version annotation
* @param versionConflictListener
*/
public void setVersionConflictListener(VersionConflictListener versionConflictListener) {
this.versionConflictListener = versionConflictListener;
}
protected void oldVersionRead(Object newObj) {
if ( versionConflictListener != null )
versionConflictListener.onOldVersionRead(newObj);
}
protected void readFieldsMapBased(FSTClazzInfo.FSTFieldInfo referencee, FSTClazzInfo serializationInfo, Object newObj) throws Exception {
String name;
int len = getCodec().getObjectHeaderLen(); // check if len is known in advance
if ( len < 0 )
len = Integer.MAX_VALUE;
int count = 0;
boolean isUnknown = newObj.getClass() == Unknown.class; // json
boolean inArray = isUnknown && getCodec().inArray(); // json externalized/custom serialized
getCodec().startFieldReading(newObj);
// fixme: break up this loop into separate impls.
while( count < len ) {
if ( inArray ) {
// unknwon json object written by externalize or custom serializer
Object o = readObjectWithHeader(null);
if ( o != null && getCodec().isEndMarker(o.toString()) )
return;
((Unknown)newObj).add(o);
continue;
}
name= getCodec().readStringUTF();
//int debug = getCodec().getInputPos();
if ( len == Integer.MAX_VALUE && getCodec().isEndMarker(name) )
return;
count++;
if (isUnknown) {
FSTClazzInfo.FSTFieldInfo fakeField = new FSTClazzInfo.FSTFieldInfo(null, null, true);
fakeField.fakeName = name;
Object toSet = readObjectWithHeader(fakeField);
((Unknown)newObj).set(name, toSet);
} else
if ( newObj.getClass() == MBObject.class ) {
Object toSet = readObjectWithHeader(null);
((MBObject)newObj).put(name,toSet);
} else {
FSTClazzInfo.FSTFieldInfo fieldInfo = serializationInfo.getFieldInfo(name, null);
if (fieldInfo == null) {
logger.log(FSTLogger.Level.WARN, "warning: unknown field: " + name + " on class " + serializationInfo.getClazz().getName(), null);
} else {
if (fieldInfo.isPrimitive()) {
// direct primitive field
switch (fieldInfo.getIntegralType()) {
case FSTClazzInfo.FSTFieldInfo.BOOL:
fieldInfo.setBooleanValue(newObj, getCodec().readFByte() == 0 ? false : true);
break;
case FSTClazzInfo.FSTFieldInfo.BYTE:
fieldInfo.setByteValue(newObj, getCodec().readFByte());
break;
case FSTClazzInfo.FSTFieldInfo.CHAR:
fieldInfo.setCharValue(newObj, getCodec().readFChar());
break;
case FSTClazzInfo.FSTFieldInfo.SHORT:
fieldInfo.setShortValue(newObj, getCodec().readFShort());
break;
case FSTClazzInfo.FSTFieldInfo.INT:
fieldInfo.setIntValue(newObj, getCodec().readFInt());
break;
case FSTClazzInfo.FSTFieldInfo.LONG:
fieldInfo.setLongValue(newObj, getCodec().readFLong());
break;
case FSTClazzInfo.FSTFieldInfo.FLOAT:
fieldInfo.setFloatValue(newObj, getCodec().readFFloat());
break;
case FSTClazzInfo.FSTFieldInfo.DOUBLE:
fieldInfo.setDoubleValue(newObj, getCodec().readFDouble());
break;
default:
throw new RuntimeException("unkown primitive type " + fieldInfo);
}
} else {
Object toSet = readObjectWithHeader(fieldInfo);
toSet = getCodec().coerceElement(fieldInfo.getType(), toSet);
fieldInfo.setObjectValue(newObj, toSet);
}
}
}
}
getCodec().endFieldReading(newObj);
}
protected boolean skipConditional(Object newObj, int conditional, FSTClazzInfo.FSTFieldInfo subInfo) {
if ( conditionalCallback != null ) {
return conditionalCallback.shouldSkip(newObj,conditional,subInfo.getField());
}
return false;
}
protected void readCompatibleObjectFields(FSTClazzInfo.FSTFieldInfo referencee, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, Map res) throws Exception {
int booleanMask = 0;
int boolcount = 8;
for (int i = 0; i < fieldInfo.length; i++) {
try {
FSTClazzInfo.FSTFieldInfo subInfo = fieldInfo[i];
if (subInfo.isIntegral() && !subInfo.isArray()) {
final Class subInfoType = subInfo.getType();
if (subInfoType == boolean.class) {
if (boolcount == 8) {
booleanMask = ((int) getCodec().readFByte() + 256) &0xff;
boolcount = 0;
}
boolean val = (booleanMask & 128) != 0;
booleanMask = booleanMask << 1;
boolcount++;
res.put(subInfo.getName(), val);
}
if (subInfoType == byte.class) {
res.put(subInfo.getName(), getCodec().readFByte());
} else if (subInfoType == char.class) {
res.put(subInfo.getName(), getCodec().readFChar());
} else if (subInfoType == short.class) {
res.put(subInfo.getName(), getCodec().readFShort());
} else if (subInfoType == int.class) {
res.put(subInfo.getName(), getCodec().readFInt());
} else if (subInfoType == double.class) {
res.put(subInfo.getName(), getCodec().readFDouble());
} else if (subInfoType == float.class) {
res.put(subInfo.getName(), getCodec().readFFloat());
} else if (subInfoType == long.class) {
res.put(subInfo.getName(), getCodec().readFLong());
}
} else {
// object
Object subObject = readObjectWithHeader(subInfo);
res.put(subInfo.getName(), subObject);
}
} catch (IllegalAccessException ex) {
throw new IOException(ex);
}
}
}
public String readStringUTF() throws IOException {
return getCodec().readStringUTF();
}
/**
* len < 127 !!!!!
* @return
* @throws IOException
*/
public String readStringAsc() throws IOException {
return getCodec().readStringAsc();
}
protected Object readArray(FSTClazzInfo.FSTFieldInfo referencee, int pos) throws Exception {
Object classOrArray = getCodec().readArrayHeader();
if (pos < 0)
pos = getCodec().getInputPos();
if ( classOrArray instanceof Class == false )
return classOrArray;
if ( classOrArray == null )
return null;
Object o = readArrayNoHeader(referencee, pos, (Class) classOrArray);
getCodec().readArrayEnd(null);
return o;
}
protected Object readArrayNoHeader(FSTClazzInfo.FSTFieldInfo referencee, int pos, Class arrCl) throws Exception {
final int len = getCodec().readFInt();
if (len == -1) {
return null;
}
Class arrType = arrCl.getComponentType();
if (!arrType.isArray()) {
Object array = Array.newInstance(arrType, len);
if ( ! referencee.isFlat() )
objects.registerObjectForRead(array, pos );
if (arrType.isPrimitive()) {
return getCodec().readFPrimitiveArray(array, arrType, len);
} else { // Object Array
Object arr[] = (Object[]) array;
for (int i = 0; i < len; i++) {
Object value = readObjectWithHeader(referencee);
value = getCodec().coerceElement(arrType, value);
arr[i] = value;
}
getCodec().readObjectEnd();
}
return array;
} else { // multidim array
Object array[] = (Object[]) Array.newInstance(arrType, len);
if ( ! referencee.isFlat() ) {
objects.registerObjectForRead(array, pos);
}
FSTClazzInfo.FSTFieldInfo ref1 = new FSTClazzInfo.FSTFieldInfo(referencee.getPossibleClasses(), null, clInfoRegistry.isIgnoreAnnotations());
for (int i = 0; i < len; i++) {
Object subArray = readArray(ref1, -1);
array[i] = subArray;
}
return array;
}
}
public void registerObject(Object o, int streamPosition, FSTClazzInfo info, FSTClazzInfo.FSTFieldInfo referencee) {
if ( ! objects.disabled && !referencee.isFlat() && (info == null || ! info.isFlat() ) ) {
objects.registerObjectForRead(o, streamPosition);
}
}
public FSTClazzInfo readClass() throws IOException, ClassNotFoundException {
return getCodec().readClass();
}
protected void resetAndClearRefs() {
try {
reset();
objects.clearForRead(conf);
} catch (IOException e) {
FSTUtil.<RuntimeException>rethrow(e);
}
}
public void reset() throws IOException {
getCodec().reset();
}
public void resetForReuse(InputStream in) throws IOException {
if ( closed ) {
throw new RuntimeException("can't reuse closed stream");
}
getCodec().reset();
getCodec().setInputStream(in);
objects.clearForRead(conf);
callbacks = null; //fix memory leak on reuse from default FstConfiguration
}
public void resetForReuseCopyArray(byte bytes[], int off, int len) throws IOException {
if ( closed ) {
throw new RuntimeException("can't reuse closed stream");
}
getCodec().reset();
objects.clearForRead(conf);
getCodec().resetToCopyOf(bytes, off, len);
callbacks = null; //fix memory leak on reuse from default FstConfiguration
}
public void resetForReuseUseArray(byte bytes[]) throws IOException {
resetForReuseUseArray(bytes, bytes.length);
}
public void resetForReuseUseArray(byte bytes[], int len) throws IOException {
if ( closed ) {
throw new RuntimeException("can't reuse closed stream");
}
objects.clearForRead(conf);
getCodec().resetWith(bytes, len);
callbacks = null; //fix memory leak on reuse from default FstConfiguration
}
public final int readFInt() throws IOException {
return getCodec().readFInt();
}
protected boolean closed = false;
@Override
public void close() throws IOException {
closed = true;
resetAndClearRefs();
conf.returnObject(objects);
objects = null;