1 package org.apache.turbine.util; 2 3 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.ObjectInputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.io.Serializable ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.List ; 30 31 38 public abstract class ObjectUtils 39 { 40 48 public static Object isNull(Object o, Object dflt) 49 { 50 return org.apache.commons.lang.ObjectUtils.defaultIfNull(o,dflt); 51 } 52 53 61 public static void addOnce(List l, Object o) 62 { 63 if (!l.contains(o)) 64 { 65 l.add(o); 66 } 67 } 68 69 78 public static byte[] serializeHashtable(Hashtable hash) 79 throws Exception 80 { 81 Hashtable saveData = new Hashtable (hash.size()); 82 String key = null; 83 Object value = null; 84 byte[] byteArray = null; 85 86 Enumeration keys = hash.keys(); 87 88 while (keys.hasMoreElements()) 89 { 90 key = (String ) keys.nextElement(); 91 value = hash.get(key); 92 if (value instanceof Serializable ) 93 { 94 saveData.put (key, value); 95 } 96 } 97 98 ByteArrayOutputStream baos = null; 99 BufferedOutputStream bos = null; 100 ObjectOutputStream out = null; 101 try 102 { 103 baos = new ByteArrayOutputStream (); 105 bos = new BufferedOutputStream (baos); 106 out = new ObjectOutputStream (bos); 107 108 out.writeObject(saveData); 109 out.flush(); 110 bos.flush(); 111 112 byteArray = baos.toByteArray(); 113 } 114 finally 115 { 116 if (out != null) 117 { 118 out.close(); 119 } 120 if (bos != null) 121 { 122 bos.close(); 123 } 124 if (baos != null) 125 { 126 baos.close(); 127 } 128 } 129 return byteArray; 130 } 131 132 139 public static Object deserialize(byte[] objectData) 140 { 141 Object object = null; 142 143 if (objectData != null) 144 { 145 ObjectInputStream in = null; 147 ByteArrayInputStream bin = new ByteArrayInputStream (objectData); 148 BufferedInputStream bufin = new BufferedInputStream (bin); 149 150 try 151 { 152 in = new ObjectInputStream (bufin); 153 154 object = in.readObject(); 157 } 158 catch (Exception e) 159 { 160 } 161 finally 162 { 163 try 164 { 165 if (in != null) 166 { 167 in.close(); 168 } 169 if (bufin != null) 170 { 171 bufin.close(); 172 } 173 if (bin != null) 174 { 175 bin.close(); 176 } 177 } 178 catch (IOException e) 179 { 180 } 181 } 182 } 183 return object; 184 } 185 186 196 public static boolean equals(Object o1, Object o2) 197 { 198 return org.apache.commons.lang.ObjectUtils.equals(o1,o2); 199 } 200 201 211 public static final void safeAddToHashtable(Hashtable hash, Object key, 212 Object value) 213 throws NullPointerException 214 { 215 if (value == null) 216 { 217 hash.put(key, ""); 218 } 219 else 220 { 221 hash.put(key, value); 222 } 223 } 224 } 225 | Popular Tags |