001    package railo.runtime.converter;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.ByteArrayOutputStream;
005    import java.io.IOException;
006    import java.io.InputStream;
007    import java.io.ObjectInputStream;
008    import java.io.ObjectOutputStream;
009    import java.io.OutputStream;
010    import java.io.Serializable;
011    import java.io.Writer;
012    
013    import railo.commons.io.IOUtil;
014    import railo.commons.io.res.Resource;
015    import railo.runtime.PageContext;
016    import railo.runtime.coder.Base64Coder;
017    import railo.runtime.coder.CoderException;
018    
019    
020    
021    /**
022     * 
023     */
024    public final class JavaConverter extends ConverterSupport implements BinaryConverter {
025    
026       
027            @Override
028            public void writeOut(PageContext pc, Object source, Writer writer) throws ConverterException, IOException {
029                    if(!(source instanceof Serializable))throw new ConverterException("Java Object is not of type Serializable");
030                writer.write(serialize((Serializable)source));
031                    writer.flush();
032            }
033    
034            @Override
035            public void writeOut(PageContext pc, Object source, OutputStream os) throws ConverterException, IOException {
036                    if(!(source instanceof Serializable))throw new ConverterException("Java Object is not of type Serializable");
037                serialize((Serializable)source, os);
038                os.flush();
039            }
040        
041        
042        /**
043         * serialize a Java Object of Type Serializable
044         * @param o
045         * @return serialized String
046         * @throws IOException
047         */
048        public static String serialize(Serializable o) throws IOException {
049            ByteArrayOutputStream baos = new ByteArrayOutputStream();
050            serialize(o, baos);
051            return Base64Coder.encode(baos.toByteArray());
052        }
053    
054        public static void serialize(Serializable o, railo.commons.io.res.Resource out) throws IOException {
055            serialize(o,out.getOutputStream());
056        }
057        
058        public static void serialize(Serializable o, OutputStream os) throws IOException {
059            ObjectOutputStream oos=null;
060            try {
061                    oos = new ObjectOutputStream(os);
062                    oos.writeObject(o);
063            }
064            finally {
065               IOUtil.closeEL(oos);
066               IOUtil.closeEL(os);
067            }
068        }
069        
070        /**
071         * unserialize a serialized Object
072         * @param str
073         * @return unserialized Object
074         * @throws IOException
075         * @throws ClassNotFoundException
076         * @throws CoderException 
077         */
078        public static Object deserialize(String str) throws IOException, ClassNotFoundException, CoderException {
079            ByteArrayInputStream bais = new ByteArrayInputStream(Base64Coder.decode(str));
080            return deserialize(bais);
081        }
082        
083        public static Object deserialize(InputStream is) throws IOException, ClassNotFoundException {
084            ObjectInputStream ois=null;
085            Object o=null;
086            try {
087                    ois = new ObjectInputStream(is);
088                    o=ois.readObject();
089            }
090            finally {
091                    IOUtil.closeEL(ois);
092            }
093            return o;
094        }
095    
096        public static Object deserialize(Resource res) throws IOException, ClassNotFoundException {
097            return deserialize(res.getInputStream()); 
098        }
099        
100        
101    }