1 28 package org.objectweb.jonas.common; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.io.OptionalDataException ; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 39 43 public class JNDIUtils { 44 45 48 private JNDIUtils() { } 49 50 55 public static byte[] getBytesFromObject(Object obj) { 56 return getBytesFromObject(obj, null); 57 } 58 59 65 public static byte[] getBytesFromObject(Object obj, Logger logger) { 66 67 if (obj == null) { 68 return null; 69 } 70 71 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 72 73 ObjectOutputStream oos = null; 74 byte[] bytes = null; 75 76 try { 77 oos = new ObjectOutputStream (baos); 78 oos.writeObject(obj); 79 bytes = baos.toByteArray(); 80 } catch (Exception e) { 81 return null; 83 } finally { 84 try { 85 oos.close(); 86 baos.close(); 87 } catch (Exception e) { 88 if (logger != null) { 89 logger.log(BasicLevel.DEBUG, "Cannot close output streams : '" + e.getMessage() + "'"); 90 } 91 } 92 } 93 return bytes; 94 } 95 96 102 public static Object getObjectFromBytes(byte[] bytes) { 103 return getObjectFromBytes(bytes, null); 104 } 105 106 113 public static Object getObjectFromBytes(byte[] bytes, Logger logger) { 114 ByteArrayInputStream bis = null; 116 ObjectInputStream ois = null; 117 Object obj = null; 118 119 if (bytes == null) { 120 return null; 121 } 122 123 bis = new ByteArrayInputStream (bytes); 124 try { 125 ois = new ObjectInputStream (bis); 126 obj = ois.readObject(); 127 128 } catch (ClassNotFoundException cfe) { 129 if (logger != null) { 130 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + cfe.getMessage()); 131 } 132 } catch (OptionalDataException ode) { 133 if (logger != null) { 134 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ode.getMessage()); 135 } 136 } catch (IOException ioe) { 137 if (logger != null) { 138 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ioe.getMessage()); 139 } 140 } finally { 141 try { 142 bis.close(); 143 ois.close(); 144 } catch (Exception e) { 145 if (logger != null) { 146 logger.log(BasicLevel.DEBUG, "Cannot close input stream : " + e.getMessage()); 147 } 148 } 149 } 150 return obj; 151 } 152 153 } 154 | Popular Tags |