001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.converter; 020 021import java.io.ByteArrayInputStream; 022import java.io.ByteArrayOutputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.ObjectInputStream; 026import java.io.ObjectOutputStream; 027import java.io.OutputStream; 028import java.io.Serializable; 029import java.io.Writer; 030 031import lucee.commons.io.IOUtil; 032import lucee.commons.io.res.Resource; 033import lucee.runtime.PageContext; 034import lucee.runtime.coder.Base64Coder; 035import lucee.runtime.coder.CoderException; 036 037 038 039/** 040 * 041 */ 042public final class JavaConverter extends ConverterSupport implements BinaryConverter { 043 044 045 @Override 046 public void writeOut(PageContext pc, Object source, Writer writer) throws ConverterException, IOException { 047 if(!(source instanceof Serializable))throw new ConverterException("Java Object is not of type Serializable"); 048 writer.write(serialize((Serializable)source)); 049 writer.flush(); 050 } 051 052 @Override 053 public void writeOut(PageContext pc, Object source, OutputStream os) throws ConverterException, IOException { 054 if(!(source instanceof Serializable))throw new ConverterException("Java Object is not of type Serializable"); 055 serialize((Serializable)source, os); 056 os.flush(); 057 } 058 059 060 /** 061 * serialize a Java Object of Type Serializable 062 * @param o 063 * @return serialized String 064 * @throws IOException 065 */ 066 public static String serialize(Serializable o) throws IOException { 067 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 068 serialize(o, baos); 069 return Base64Coder.encode(baos.toByteArray()); 070 } 071 public static byte[] serializeAsBinary(Serializable o) throws IOException { 072 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 073 serialize(o, baos); 074 return baos.toByteArray(); 075 } 076 077 public static void serialize(Serializable o, lucee.commons.io.res.Resource out) throws IOException { 078 serialize(o,out.getOutputStream()); 079 } 080 081 public static void serialize(Serializable o, OutputStream os) throws IOException { 082 ObjectOutputStream oos=null; 083 try { 084 oos = new ObjectOutputStream(os); 085 oos.writeObject(o); 086 } 087 finally { 088 IOUtil.closeEL(oos); 089 IOUtil.closeEL(os); 090 } 091 } 092 093 /** 094 * unserialize a serialized Object 095 * @param str 096 * @return unserialized Object 097 * @throws IOException 098 * @throws ClassNotFoundException 099 * @throws CoderException 100 */ 101 public static Object deserialize(String str) throws IOException, ClassNotFoundException, CoderException { 102 ByteArrayInputStream bais = new ByteArrayInputStream(Base64Coder.decode(str)); 103 return deserialize(bais); 104 } 105 106 public static Object deserialize(InputStream is) throws IOException, ClassNotFoundException { 107 ObjectInputStream ois=null; 108 Object o=null; 109 try { 110 ois = new ObjectInputStream(is); 111 o=ois.readObject(); 112 } 113 finally { 114 IOUtil.closeEL(ois); 115 } 116 return o; 117 } 118 119 public static Object deserialize(Resource res) throws IOException, ClassNotFoundException { 120 return deserialize(res.getInputStream()); 121 } 122 123 124}