summaryrefslogtreecommitdiff
path: root/external/ikvm/openjdk/java/io/InteropObjectInputStream.java
blob: de08fe3af607f5fc83825b123a83a0537a10d205 (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
/*
 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2009 Jeroen Frijters
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package java.io;

import cli.System.Runtime.Serialization.SerializationException;
import cli.System.Runtime.Serialization.SerializationInfo;
import java.util.concurrent.atomic.AtomicBoolean;

@ikvm.lang.Internal
public final class InteropObjectInputStream extends ObjectInputStream
{
    private ObjectDataInputStream dis;
    private CallbackContext curContext;
    
    public static void readObject(Object obj, SerializationInfo info)
    {
        try
        {
            new InteropObjectInputStream(obj, info);
        }
        catch (Exception x)
        {
            ikvm.runtime.Util.throwException(new SerializationException(x.getMessage(), x));
        }
    }
    
    private InteropObjectInputStream(Object obj, SerializationInfo info) throws IOException, ClassNotFoundException
    {
        dis = new ObjectDataInputStream(info);
        if (obj instanceof ObjectStreamClass)
        {
            switch (readByte())
            {
                case TC_PROXYCLASSDESC:
                    readProxyDesc((ObjectStreamClass)obj);
                    break;
                case TC_CLASSDESC:
                    readNonProxyDesc((ObjectStreamClass)obj);
                    break;
                default:
                    throw new StreamCorruptedException();
            }
        }
        else
        {
            readOrdinaryObject(obj);
        }
    }
    
    private ObjectStreamClass readClassDesc() throws IOException, ClassNotFoundException
    {
        return (ObjectStreamClass)readObject();
    }
    
    private void readProxyDesc(ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        int numIfaces = readInt();
        Class[] ifaces = new Class[numIfaces];
        for (int i = 0; i < numIfaces; i++)
        {
            ifaces[i] = (Class)readObject();
        }
        Class cl = java.lang.reflect.Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), ifaces);
        desc.initProxy(cl, null, readClassDesc());
    }
    
    private void readNonProxyDesc(ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        Class cl = (Class)readObject();
        ObjectStreamClass readDesc = new ObjectStreamClass();
        readDesc.readNonProxy(this);
        desc.initNonProxy(readDesc, cl, null, readClassDesc());
    }
    
    private void readOrdinaryObject(Object obj) throws IOException, ClassNotFoundException
    {
        ObjectStreamClass desc = readClassDesc();
        desc.checkDeserialize();

        if (obj instanceof InteropObjectOutputStream.DynamicProxy)
        {
            InteropObjectOutputStream.DynamicProxy pp = (InteropObjectOutputStream.DynamicProxy)obj;
            try
            {
                obj = desc.newInstance();
                pp.obj = obj;
            }
            catch (Exception ex)
            {
                throw (IOException) new InvalidClassException(
                    desc.forClass().getName(),
                    "unable to create instance").initCause(ex);
            }
        }
        
        if (desc.isExternalizable())
        {
            readExternalData((Externalizable)obj, desc);
        }
        else
        {
            readSerialData(obj, desc);
        }
    }
    
    private void readExternalData(Externalizable obj, ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        CallbackContext oldContext = curContext;
        curContext = null;
        try {
            obj.readExternal(this);
            skipCustomData();
        } finally {
            curContext = oldContext;
        }
    }

    private void readSerialData(Object obj, ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
        for (int i = 0; i < slots.length; i++)
        {
            ObjectStreamClass slotDesc = slots[i].desc;

            if (slots[i].hasData)
            {
                if (slotDesc.hasReadObjectMethod())
                {
                    CallbackContext oldContext = curContext;
                    try
                    {
                        curContext = new CallbackContext(obj, slotDesc);
                        slotDesc.invokeReadObject(obj, this);
                    }
                    finally
                    {
                        curContext.setUsed();
                        curContext = oldContext;
                    }
                }
                else
                {
                    defaultReadFields(obj, slotDesc);
                }
                if (slotDesc.hasWriteObjectData())
                {
                    skipCustomData();
                }
            }
            else if (slotDesc.hasReadObjectNoDataMethod())
            {
                slotDesc.invokeReadObjectNoData(obj);
            }
        }
    }
    
    private void skipCustomData() throws IOException
    {
        dis.skipMarker();
    }
    
    private void defaultReadFields(Object obj, ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        byte[] primVals = new byte[desc.getPrimDataSize()];
        readFully(primVals);
        desc.setPrimFieldValues(obj, primVals);

        Object[] objVals = new Object[desc.getNumObjFields()];
        for (int i = 0; i < objVals.length; i++)
        {
            objVals[i] = readObject();
        }
        desc.setObjFieldValues(obj, objVals);
    }
    
    @Override
    public void defaultReadObject() throws IOException, ClassNotFoundException
    {
        if (curContext == null) {
            throw new NotActiveException("not in call to readObject");
        }
        Object curObj = curContext.getObj();
        ObjectStreamClass curDesc = curContext.getDesc();
        defaultReadFields(curObj, curDesc);
    }

    @Override
    protected Object readObjectOverride() throws IOException
    {
        return dis.readObject();
    }

    @Override
    public Object readUnshared()
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException
    {
        if (curContext == null) {
            throw new NotActiveException("not in call to readObject");
        }
        Object curObj = curContext.getObj();
        ObjectStreamClass curDesc = curContext.getDesc();
        GetFieldImpl getField = new GetFieldImpl(curDesc);
        getField.readFields();
        return getField;
    }

    @Override
    public void registerValidation(ObjectInputValidation obj, int prio)
    {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public int read() throws IOException
    {
        return dis.read();
    }

    @Override
    public int read(byte[] buf, int off, int len) throws IOException
    {
        return dis.read(buf, off, len);
    }

    @Override
    public int available() throws IOException
    {
        return dis.available();
    }

    @Override
    public void close() throws IOException
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean readBoolean() throws IOException
    {
        return dis.readBoolean();
    }

    @Override
    public byte readByte() throws IOException
    {
        return dis.readByte();
    }

    @Override
    public int readUnsignedByte() throws IOException
    {
        return dis.readUnsignedByte();
    }

    @Override
    public char readChar() throws IOException
    {
        return dis.readChar();
    }

    @Override
    public short readShort() throws IOException
    {
        return dis.readShort();
    }

    @Override
    public int readUnsignedShort() throws IOException
    {
        return dis.readUnsignedShort();
    }

    @Override
    public int readInt() throws IOException
    {
        return dis.readInt();
    }

    @Override
    public long readLong() throws IOException
    {
        return dis.readLong();
    }

    @Override
    public float readFloat() throws IOException
    {
        return dis.readFloat();
    }

    @Override
    public double readDouble() throws IOException
    {
        return dis.readDouble();
    }

    @Override
    public void readFully(byte[] buf) throws IOException
    {
        readFully(buf, 0, buf.length);
    }

    @Override
    public void readFully(byte[] buf, int off, int len) throws IOException
    {
        dis.readFully(buf, off, len);
    }

    @Override
    public int skipBytes(int len) throws IOException
    {
        return dis.skipBytes(len);
    }

    @Deprecated
    @Override
    public String readLine() throws IOException
    {
        return dis.readLine();
    }

    @Override
    public String readUTF() throws IOException
    {
        return dis.readUTF();
    }
    
    // private API used by ObjectStreamClass
    @Override    
    String readTypeString() throws IOException
    {
        return (String)readObjectOverride();
    }

    private final class GetFieldImpl extends GetField {

        /** class descriptor describing serializable fields */
        private final ObjectStreamClass desc;
        /** primitive field values */
        private final byte[] primVals;
        /** object field values */
        private final Object[] objVals;
        /** object field value handles */
        private final int[] objHandles;

        /**
         * Creates GetFieldImpl object for reading fields defined in given
         * class descriptor.
         */
        GetFieldImpl(ObjectStreamClass desc) {
            this.desc = desc;
            primVals = new byte[desc.getPrimDataSize()];
            objVals = new Object[desc.getNumObjFields()];
            objHandles = new int[objVals.length];
        }

        public ObjectStreamClass getObjectStreamClass() {
            return desc;
        }

        public boolean defaulted(String name) throws IOException {
            return (getFieldOffset(name, null) < 0);
        }

        public boolean get(String name, boolean val) throws IOException {
            int off = getFieldOffset(name, Boolean.TYPE);
            return (off >= 0) ? Bits.getBoolean(primVals, off) : val;
        }

        public byte get(String name, byte val) throws IOException {
            int off = getFieldOffset(name, Byte.TYPE);
            return (off >= 0) ? primVals[off] : val;
        }

        public char get(String name, char val) throws IOException {
            int off = getFieldOffset(name, Character.TYPE);
            return (off >= 0) ? Bits.getChar(primVals, off) : val;
        }

        public short get(String name, short val) throws IOException {
            int off = getFieldOffset(name, Short.TYPE);
            return (off >= 0) ? Bits.getShort(primVals, off) : val;
        }

        public int get(String name, int val) throws IOException {
            int off = getFieldOffset(name, Integer.TYPE);
            return (off >= 0) ? Bits.getInt(primVals, off) : val;
        }

        public float get(String name, float val) throws IOException {
            int off = getFieldOffset(name, Float.TYPE);
            return (off >= 0) ? Bits.getFloat(primVals, off) : val;
        }

        public long get(String name, long val) throws IOException {
            int off = getFieldOffset(name, Long.TYPE);
            return (off >= 0) ? Bits.getLong(primVals, off) : val;
        }

        public double get(String name, double val) throws IOException {
            int off = getFieldOffset(name, Double.TYPE);
            return (off >= 0) ? Bits.getDouble(primVals, off) : val;
        }

        public Object get(String name, Object val) throws IOException {
            int off = getFieldOffset(name, Object.class);
            if (off >= 0) {
                return objVals[off];
            } else {
                return val;
            }
        }

        /**
         * Reads primitive and object field values from stream.
         */
        void readFields() throws IOException {
            readFully(primVals, 0, primVals.length);

            for (int i = 0; i < objVals.length; i++) {
                objVals[i] = readObjectOverride();
            }
        }

        /**
         * Returns offset of field with given name and type.  A specified type
         * of null matches all types, Object.class matches all non-primitive
         * types, and any other non-null type matches assignable types only.
         * If no matching field is found in the (incoming) class
         * descriptor but a matching field is present in the associated local
         * class descriptor, returns -1.  Throws IllegalArgumentException if
         * neither incoming nor local class descriptor contains a match.
         */
        private int getFieldOffset(String name, Class type) {
            ObjectStreamField field = desc.getField(name, type);
            if (field != null) {
                return field.getOffset();
            } else if (desc.getLocalDesc().getField(name, type) != null) {
                return -1;
            } else {
                throw new IllegalArgumentException("no such field " + name +
                                                   " with type " + type);
            }
        }
    }

    private final static class CallbackContext {
        private final Object obj;
        private final ObjectStreamClass desc;
        private final AtomicBoolean used = new AtomicBoolean();

        public CallbackContext(Object obj, ObjectStreamClass desc) {
            this.obj = obj;
            this.desc = desc;
        }

        public Object getObj() throws NotActiveException {
            checkAndSetUsed();
            return obj;
        }

        public ObjectStreamClass getDesc() {
            return desc;
        }

        private void checkAndSetUsed() throws NotActiveException {
            if (!used.compareAndSet(false, true)) {
                 throw new NotActiveException(
                      "not in readObject invocation or fields already read");
            }
        }

        public void setUsed() {
            used.set(true);
        }
    }

    private static final class ObjectDataInputStream extends DataInputStream
    {
        private final static class Inp extends FilterInputStream
        {
            private static final byte MARKER = 0;
            private static final byte OBJECTS = 10;
            private static final byte BYTES = 20;
            private final SerializationInfo info;
            private byte[] buf;
            private int pos;
            private int blockEnd = -1;
            private int objCount;
            private int objId;

            Inp(SerializationInfo info) throws IOException
            {
                super(null);
                this.info = info;
                buf = (byte[])info.GetValue("$data", ikvm.runtime.Util.getInstanceTypeFromClass(cli.System.Object.class));
                modeSwitch();
            }
            
            private void modeSwitch() throws IOException
            {
                if (pos == buf.length)
                {
                    // next read will report error
                    blockEnd = -1;
                    objCount = -1;
                }
                else if (buf[pos] == OBJECTS)
                {
                    pos++;
                    objCount = readPacked();
                    blockEnd = -1;
                }
                else if (buf[pos] == BYTES)
                {
                    pos++;
                    switchToData();
                }
                else if (buf[pos] == MARKER)
                {
                }
                else
                {
                    throw new StreamCorruptedException();
                }
            }
            
            private int packedLength(int val)
            {
                if (val < 0)
                {
                    // we only use packed integers for lengths or counts, so they can never be negative
                    throw new Error();
                }
                else if (val < 128)
                {
                    return 1;
                }
                else if (val < 16129)
                {
                    return 2;
                }
                else
                {
                    return 5;
                }
            }
            
            private int readPacked()
            {
                int b1 = buf[pos++] & 0xFF;
                if (b1 < 128)
                {
                    return b1;
                }
                else if (b1 != 128)
                {
                    return ((b1 & 127) << 7) + (buf[pos++] & 0xFF);
                }
                else
                {
                    int v = 0;
                    v |= (buf[pos++] & 0xFF) << 24;
                    v |= (buf[pos++] & 0xFF) << 16;
                    v |= (buf[pos++] & 0xFF) <<  8;
                    v |= (buf[pos++] & 0xFF) <<  0;
                    return v;
                }
            }
            
            private void switchToData() throws IOException
            {
                if (blockEnd != -1 || objCount != 0 || buf[pos] == MARKER)
                {
                    throw new StreamCorruptedException();
                }
                int len = readPacked();
                blockEnd = pos + len;
                if (blockEnd > buf.length)
                {
                    throw new StreamCorruptedException();
                }
            }

            public int read() throws IOException
            {
                if (blockEnd - pos < 1)
                {
                    switchToData();
                }
                return buf[pos++] & 0xFF;
            }
            
            public int read(byte b[], int off, int len) throws IOException
            {
                if (len == 0)
                {
                    return 0;
                }
                if (blockEnd - pos < len)
                {
                    switchToData();
                }
                System.arraycopy(buf, pos, b, off, len);
                pos += len;
                return len;
            }
            
            public long skip(long n) throws IOException
            {
                if (n == 0)
                {
                    return 0;
                }
                if (blockEnd - pos < n)
                {
                    switchToData();
                }
                pos += (int)n;
                return n;
            }
            
            public int available() throws IOException
            {
                return 0;
            }
            
            public void close() throws IOException
            {
            }
            
            public void mark(int readlimit)
            {
            }
            
            public void reset() throws IOException
            {
            }
            
            public boolean markSupported()
            {
                return false;
            }
            
            Object readObject() throws IOException
            {
                if (objCount <= 0)
                {
                    if (pos != blockEnd || buf[pos] == MARKER)
                    {
                        throw new StreamCorruptedException();
                    }
                    blockEnd = -1;
                    objCount = readPacked();
                }
                objCount--;
                return info.GetValue("$" + (objId++), ikvm.runtime.Util.getInstanceTypeFromClass(cli.System.Object.class));
            }
            
            void skipMarker() throws IOException
            {
                for (;;)
                {
                    if (objCount > 0)
                    {
                        objId += objCount;
                        objCount = 0;
                        if (buf[pos] == MARKER)
                        {
                            pos++;
                            modeSwitch();
                            break;
                        }
                        switchToData();
                    }
                    if (blockEnd != -1)
                    {
                        pos = blockEnd;
                        blockEnd = -1;
                    }
                    if (pos == buf.length)
                    {
                        throw new StreamCorruptedException();
                    }
                    if (buf[pos] == MARKER)
                    {
                        pos++;
                        modeSwitch();
                        break;
                    }
                    blockEnd = -1;
                    objCount = readPacked();
                }
            }
        }

        ObjectDataInputStream(SerializationInfo info) throws IOException
        {
            super(new Inp(info));
        }
        
        public Object readObject() throws IOException
        {
            return ((Inp)in).readObject();
        }
        
        public void skipMarker() throws IOException
        {
            ((Inp)in).skipMarker();
        }
    }
}