aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core-plugins/com/jme3/export/binary/BinaryOutputCapsule.java
blob: 4466db52a5cc1691a5e8271a2ee383556b0d7af5 (plain)
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
/*
 * Copyright (c) 2009-2010 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme3.export.binary;

import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.util.IntMap;
import com.jme3.util.IntMap.Entry;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Map;

/**
 * @author Joshua Slack
 */
final class BinaryOutputCapsule implements OutputCapsule {

    public static final int NULL_OBJECT = -1;
    public static final int DEFAULT_OBJECT = -2;

    public static byte[] NULL_BYTES = new byte[] { (byte) -1 };
    public static byte[] DEFAULT_BYTES = new byte[] { (byte) -2 };

    protected ByteArrayOutputStream baos;
    protected byte[] bytes;
    protected BinaryExporter exporter;
    protected BinaryClassObject cObj;

    public BinaryOutputCapsule(BinaryExporter exporter, BinaryClassObject bco) {
        this.baos = new ByteArrayOutputStream();
        this.exporter = exporter;
        this.cObj = bco;
    }

    public void write(byte value, String name, byte defVal) throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BYTE);
        write(value);
    }

    public void write(byte[] value, String name, byte[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BYTE_1D);
        write(value);
    }

    public void write(byte[][] value, String name, byte[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BYTE_2D);
        write(value);
    }

    public void write(int value, String name, int defVal) throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.INT);
        write(value);
    }

    public void write(int[] value, String name, int[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.INT_1D);
        write(value);
    }

    public void write(int[][] value, String name, int[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.INT_2D);
        write(value);
    }

    public void write(float value, String name, float defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.FLOAT);
        write(value);
    }

    public void write(float[] value, String name, float[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.FLOAT_1D);
        write(value);
    }

    public void write(float[][] value, String name, float[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.FLOAT_2D);
        write(value);
    }

    public void write(double value, String name, double defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.DOUBLE);
        write(value);
    }

    public void write(double[] value, String name, double[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.DOUBLE_1D);
        write(value);
    }

    public void write(double[][] value, String name, double[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.DOUBLE_2D);
        write(value);
    }

    public void write(long value, String name, long defVal) throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.LONG);
        write(value);
    }

    public void write(long[] value, String name, long[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.LONG_1D);
        write(value);
    }

    public void write(long[][] value, String name, long[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.LONG_2D);
        write(value);
    }

    public void write(short value, String name, short defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.SHORT);
        write(value);
    }

    public void write(short[] value, String name, short[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.SHORT_1D);
        write(value);
    }

    public void write(short[][] value, String name, short[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.SHORT_2D);
        write(value);
    }

    public void write(boolean value, String name, boolean defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BOOLEAN);
        write(value);
    }

    public void write(boolean[] value, String name, boolean[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BOOLEAN_1D);
        write(value);
    }

    public void write(boolean[][] value, String name, boolean[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BOOLEAN_2D);
        write(value);
    }

    public void write(String value, String name, String defVal)
            throws IOException {
        if (value == null ? defVal == null : value.equals(defVal))
            return;
        writeAlias(name, BinaryClassField.STRING);
        write(value);
    }

    public void write(String[] value, String name, String[] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.STRING_1D);
        write(value);
    }

    public void write(String[][] value, String name, String[][] defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.STRING_2D);
        write(value);
    }

    public void write(BitSet value, String name, BitSet defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BITSET);
        write(value);
    }

    public void write(Savable object, String name, Savable defVal)
            throws IOException {
        if (object == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE);
        write(object);
    }

    public void write(Savable[] objects, String name, Savable[] defVal)
            throws IOException {
        if (objects == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_1D);
        write(objects);
    }

    public void write(Savable[][] objects, String name, Savable[][] defVal)
            throws IOException {
        if (objects == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_2D);
        write(objects);
    }

    public void write(FloatBuffer value, String name, FloatBuffer defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.FLOATBUFFER);
        write(value);
    }

    public void write(IntBuffer value, String name, IntBuffer defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.INTBUFFER);
        write(value);
    }

    public void write(ByteBuffer value, String name, ByteBuffer defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.BYTEBUFFER);
        write(value);
    }

    public void write(ShortBuffer value, String name, ShortBuffer defVal)
            throws IOException {
        if (value == defVal)
            return;
        writeAlias(name, BinaryClassField.SHORTBUFFER);
        write(value);
    }

    public void writeFloatBufferArrayList(ArrayList<FloatBuffer> array,
            String name, ArrayList<FloatBuffer> defVal) throws IOException {
        if (array == defVal)
            return;
        writeAlias(name, BinaryClassField.FLOATBUFFER_ARRAYLIST);
        writeFloatBufferArrayList(array);
    }

    public void writeByteBufferArrayList(ArrayList<ByteBuffer> array,
            String name, ArrayList<ByteBuffer> defVal) throws IOException {
        if (array == defVal)
            return;
        writeAlias(name, BinaryClassField.BYTEBUFFER_ARRAYLIST);
        writeByteBufferArrayList(array);
    }

    public void writeSavableArrayList(ArrayList array, String name,
            ArrayList defVal) throws IOException {
        if (array == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_ARRAYLIST);
        writeSavableArrayList(array);
    }

    public void writeSavableArrayListArray(ArrayList[] array, String name,
            ArrayList[] defVal) throws IOException {
        if (array == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_ARRAYLIST_1D);
        writeSavableArrayListArray(array);
    }

    public void writeSavableArrayListArray2D(ArrayList[][] array, String name,
            ArrayList[][] defVal) throws IOException {
        if (array == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_ARRAYLIST_2D);
        writeSavableArrayListArray2D(array);
    }

    public void writeSavableMap(Map<? extends Savable, ? extends Savable> map,
            String name, Map<? extends Savable, ? extends Savable> defVal)
            throws IOException {
        if (map == defVal)
            return;
        writeAlias(name, BinaryClassField.SAVABLE_MAP);
        writeSavableMap(map);
    }

    public void writeStringSavableMap(Map<String, ? extends Savable> map,
            String name, Map<String, ? extends Savable> defVal)
            throws IOException {
        if (map == defVal)
            return;
        writeAlias(name, BinaryClassField.STRING_SAVABLE_MAP);
        writeStringSavableMap(map);
    }

    public void writeIntSavableMap(IntMap<? extends Savable> map,
            String name, IntMap<? extends Savable> defVal)
            throws IOException {
        if (map == defVal)
            return;
        writeAlias(name, BinaryClassField.INT_SAVABLE_MAP);
        writeIntSavableMap(map);
    }

    protected void writeAlias(String name, byte fieldType) throws IOException {
        if (cObj.nameFields.get(name) == null)
            generateAlias(name, fieldType);

        byte alias = cObj.nameFields.get(name).alias;
        write(alias);
    }

    // XXX: The generation of aliases is limited to 256 possible values.
    // If we run into classes with more than 256 fields, we need to expand this.
    // But I mean, come on...
    protected void generateAlias(String name, byte type) {
        byte alias = (byte) cObj.nameFields.size();
        cObj.nameFields.put(name, new BinaryClassField(name, alias, type));
    }

    @Override
    public boolean equals(Object arg0) {
        if (!(arg0 instanceof BinaryOutputCapsule))
            return false;

        byte[] other = ((BinaryOutputCapsule) arg0).bytes;
        if (bytes.length != other.length)
            return false;
        return Arrays.equals(bytes, other);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + Arrays.hashCode(this.bytes);
        return hash;
    }

    public void finish() {
        // renamed to finish as 'finalize' in java.lang.Object should not be
        // overridden like this
        // - finalize should not be called directly but is called by garbage
        // collection!!!
        bytes = baos.toByteArray();
        baos = null;
    }

    // byte primitive

    protected void write(byte value) throws IOException {
        baos.write(value);
    }

    protected void writeForBuffer(byte value) throws IOException {
        baos.write(value);
    }
    
    protected void write(byte[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        baos.write(value);
    }

    protected void write(byte[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // int primitive

    protected void write(int value) throws IOException {
        baos.write(deflate(ByteUtils.convertToBytes(value)));
    }

    protected void writeForBuffer(int value) throws IOException {
        byte[] byteArray = new byte[4];
        byteArray[0] = (byte) value;
        byteArray[1] = (byte) (value >> 8);
        byteArray[2] = (byte) (value >> 16);
        byteArray[3] = (byte) (value >> 24);
        baos.write(byteArray);
    }

    protected void write(int[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(int[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // float primitive

    protected void write(float value) throws IOException {
        baos.write(ByteUtils.convertToBytes(value));
    }

    protected void writeForBuffer(float value) throws IOException {
        int integer = Float.floatToIntBits(value);
        writeForBuffer(integer);
    }

    protected void write(float[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(float[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // double primitive

    protected void write(double value) throws IOException {
        baos.write(ByteUtils.convertToBytes(value));
    }

    protected void write(double[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(double[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // long primitive

    protected void write(long value) throws IOException {
        baos.write(deflate(ByteUtils.convertToBytes(value)));
    }

    protected void write(long[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(long[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // short primitive

    protected void write(short value) throws IOException {
        baos.write(ByteUtils.convertToBytes(value));
    }

    protected void writeForBuffer(short value) throws IOException {
        byte[] byteArray = new byte[2];
        byteArray[0] = (byte) value;
        byteArray[1] = (byte) (value >> 8);
        baos.write(byteArray);
    }

    protected void write(short[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(short[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // boolean primitive

    protected void write(boolean value) throws IOException {
        baos.write(ByteUtils.convertToBytes(value));
    }

    protected void write(boolean[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(boolean[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // String

    protected void write(String value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        // write our output as UTF-8. Java misspells UTF-8 as UTF8 for official use in java.lang
        byte[] bytes = value.getBytes("UTF8");
        write(bytes.length);
        baos.write(bytes);
    }

    protected void write(String[] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    protected void write(String[][] value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.length);
        for (int x = 0; x < value.length; x++)
            write(value[x]);
    }

    // BitSet

    protected void write(BitSet value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        write(value.size());
        // TODO: MAKE THIS SMALLER
        for (int x = 0, max = value.size(); x < max; x++)
            write(value.get(x));
    }

    // DEFLATOR for int and long

    protected static byte[] deflate(byte[] bytes) {
        int size = bytes.length;
        if (size == 4) {
            int possibleMagic = ByteUtils.convertIntFromBytes(bytes);
            if (possibleMagic == NULL_OBJECT)
                return NULL_BYTES;
            else if (possibleMagic == DEFAULT_OBJECT)
                return DEFAULT_BYTES;
        }
        for (int x = 0; x < bytes.length; x++) {
            if (bytes[x] != 0)
                break;
            size--;
        }
        if (size == 0)
            return new byte[1];

        byte[] rVal = new byte[1 + size];
        rVal[0] = (byte) size;
        for (int x = 1; x < rVal.length; x++)
            rVal[x] = bytes[bytes.length - size - 1 + x];

        return rVal;
    }

    // BinarySavable

    protected void write(Savable object) throws IOException {
        if (object == null) {
            write(NULL_OBJECT);
            return;
        }
        int id = exporter.processBinarySavable(object);
        write(id);
    }

    // BinarySavable array

    protected void write(Savable[] objects) throws IOException {
        if (objects == null) {
            write(NULL_OBJECT);
            return;
        }
        write(objects.length);
        for (int x = 0; x < objects.length; x++) {
            write(objects[x]);
        }
    }

    protected void write(Savable[][] objects) throws IOException {
        if (objects == null) {
            write(NULL_OBJECT);
            return;
        }
        write(objects.length);
        for (int x = 0; x < objects.length; x++) {
            write(objects[x]);
        }
    }

    // ArrayList<BinarySavable>

    protected void writeSavableArrayList(ArrayList array) throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());
        for (Object bs : array) {
            write((Savable) bs);
        }
    }

    protected void writeSavableArrayListArray(ArrayList[] array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.length);
        for (ArrayList bs : array) {
            writeSavableArrayList(bs);
        }
    }

    protected void writeSavableArrayListArray2D(ArrayList[][] array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.length);
        for (ArrayList[] bs : array) {
            writeSavableArrayListArray(bs);
        }
    }

    // Map<BinarySavable, BinarySavable>

    protected void writeSavableMap(
            Map<? extends Savable, ? extends Savable> array) throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());
        for (Savable key : array.keySet()) {
            write(new Savable[] { key, array.get(key) });
        }
    }

    protected void writeStringSavableMap(Map<String, ? extends Savable> array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());

        // write String array for keys
        String[] keys = array.keySet().toArray(new String[] {});
        write(keys);

        // write Savable array for values
        Savable[] values = array.values().toArray(new Savable[] {});
        write(values);
    }

    protected void writeIntSavableMap(IntMap<? extends Savable> array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());

        int[] keys = new int[array.size()];
        Savable[] values = new Savable[keys.length];
        int i = 0;
        for (Entry<? extends Savable> entry : array){
            keys[i] = entry.getKey();
            values[i] = entry.getValue();
            i++;
        }

        // write String array for keys
        write(keys);

        // write Savable array for values
        write(values);
    }

    // ArrayList<FloatBuffer>

    protected void writeFloatBufferArrayList(ArrayList<FloatBuffer> array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());
        for (FloatBuffer buf : array) {
            write(buf);
        }
    }

    // ArrayList<FloatBuffer>

    protected void writeByteBufferArrayList(ArrayList<ByteBuffer> array)
            throws IOException {
        if (array == null) {
            write(NULL_OBJECT);
            return;
        }
        write(array.size());
        for (ByteBuffer buf : array) {
            write(buf);
        }
    }

    // NIO BUFFERS
    // float buffer

    protected void write(FloatBuffer value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        value.rewind();
        int length = value.limit();
        write(length);
        for (int x = 0; x < length; x++) {
            writeForBuffer(value.get());
        }
        value.rewind();
    }

    // int buffer

    protected void write(IntBuffer value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        value.rewind();
        int length = value.limit();
        write(length);

        for (int x = 0; x < length; x++) {
            writeForBuffer(value.get());
        }
        value.rewind();
    }

    // byte buffer

    protected void write(ByteBuffer value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        value.rewind();
        int length = value.limit();
        write(length);
        for (int x = 0; x < length; x++) {
            writeForBuffer(value.get());
        }
        value.rewind();
    }

    // short buffer

    protected void write(ShortBuffer value) throws IOException {
        if (value == null) {
            write(NULL_OBJECT);
            return;
        }
        value.rewind();
        int length = value.limit();
        write(length);
        for (int x = 0; x < length; x++) {
            writeForBuffer(value.get());
        }
        value.rewind();
    }

    public void write(Enum value, String name, Enum defVal) throws IOException {
        if (value == defVal)
            return;
        if (value == null) {
            return;
        } else {
            write(value.name(), name, null);
        }
    }
}