1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2010 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist.util.proxy;
17
18import java.io.IOException;
19import java.io.ObjectOutputStream;
20import java.io.ObjectStreamClass;
21import java.io.OutputStream;
22
23/**
24 * An input stream class which knows how to serialize proxies created via {@link ProxyFactory}. It must
25 * be used when serialising proxies created from a proxy factory configured with
26 * {@link ProxyFactory#useWriteReplace} set to false. Subsequent deserialization of the serialized data
27 * must employ a {@link ProxyObjectInputStream}
28 *
29 * @author Andrew Dinn
30 */
31public class ProxyObjectOutputStream extends ObjectOutputStream
32{
33    /**
34     * create an output stream which can be used to serialize an object graph which includes proxies created
35     * using class ProxyFactory
36     * @param out
37     * @throws IOException whenever ObjectOutputStream would also do so
38     * @throws SecurityException whenever ObjectOutputStream would also do so
39     * @throws NullPointerException if out is null
40     */
41    public ProxyObjectOutputStream(OutputStream out) throws IOException
42    {
43        super(out);
44    }
45
46    protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
47        Class cl = desc.forClass();
48        if (ProxyFactory.isProxyClass(cl)) {
49            writeBoolean(true);
50            Class superClass = cl.getSuperclass();
51            Class[] interfaces = cl.getInterfaces();
52            byte[] signature = ProxyFactory.getFilterSignature(cl);
53            String name = superClass.getName();
54            writeObject(name);
55            // we don't write the marker interface ProxyObject
56            writeInt(interfaces.length - 1);
57            for (int i = 0; i < interfaces.length; i++) {
58                Class interfaze = interfaces[i];
59                if (interfaze != ProxyObject.class) {
60                    name = interfaces[i].getName();
61                    writeObject(name);
62                }
63            }
64            writeInt(signature.length);
65            write(signature);
66        } else {
67            writeBoolean(false);
68            super.writeClassDescriptor(desc);
69        }
70    }
71}
72