1 23 24 package com.sun.ejb.base.io; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 34 import java.util.logging.*; 35 import com.sun.logging.*; 36 37 import com.sun.ejb.spi.container.ContainerCallback; 38 import com.sun.ejb.spi.io.J2EEObjectStreamFactory; 39 import com.sun.ejb.spi.io.NonSerializableObjectHandler; 40 41 public class IOUtils { 42 43 private static Logger _ejbLogger = 44 LogDomains.getLogger(LogDomains.EJB_LOGGER); 45 46 private static J2EEObjectStreamFactory _streamFactory; 47 48 public static final void setJ2EEObjectStreamFactory( 49 J2EEObjectStreamFactory factory) 50 { 51 _streamFactory = factory; 52 } 53 54 public static ObjectInputStream createObjectInputStream( 55 final InputStream is, final boolean resolveObject, 56 final ClassLoader loader) 57 throws Exception 58 { 59 return _streamFactory.createObjectInputStream(is, resolveObject, loader); 60 } 61 62 public static ObjectOutputStream createObjectOutputStream( 63 final OutputStream os, final boolean replaceObject, 64 final NonSerializableObjectHandler handler) 65 throws IOException 66 { 67 return _streamFactory.createObjectOutputStream(os, replaceObject, 68 handler); 69 } 70 71 public static final byte[] serializeObject(Object obj, boolean replaceObject) 72 throws java.io.NotSerializableException 73 { 74 byte[] data = null; 75 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 76 ObjectOutputStream oos = null; 77 try { 78 oos = _streamFactory.createObjectOutputStream( 79 bos, replaceObject, 80 new NonSerializableObjectHandler() { 81 public Object handleNonSerializableObject(Object obj) { 82 return obj; 83 } 84 }); 85 86 oos.writeObject(obj); 87 oos.flush(); 88 data = bos.toByteArray(); 89 } catch (java.io.NotSerializableException notSerEx) { 90 throw notSerEx; 91 } catch (Exception th) { 92 _ejbLogger.log(Level.FINE, "Error during serialization", th); 93 } finally { 94 if (oos != null) { 95 try { oos.close(); } catch (Exception ex) {} 96 } 97 try { bos.close(); } catch (Exception ex) {} 98 } 99 100 return data; 101 } 102 103 public static final Object deserializeObject(byte[] data, boolean resolveObject, 104 ClassLoader classLoader) 105 throws Exception 106 { 107 Object obj = null; 108 ByteArrayInputStream bis = null; 109 ObjectInputStream ois = null; 110 try { 111 bis = new ByteArrayInputStream (data); 112 ois = _streamFactory.createObjectInputStream(bis, resolveObject, 113 classLoader); 114 obj = ois.readObject(); 115 } catch (Exception ex) { 116 _ejbLogger.log(Level.FINE, "Error during deserialization", ex); 117 throw ex; 118 } finally { 119 try { ois.close(); } catch (Exception ex) {} 120 try { bis.close(); } catch (Exception ex) {} 121 } 122 return obj; 123 } 124 125 } 126 | Popular Tags |