1 3 package jodd.util; 4 5 import jodd.io.FastByteArrayOutputStream; 6 import jodd.io.StreamUtil; 7 8 import java.io.*; 9 import java.util.Arrays ; 10 11 14 public class ObjectUtil { 15 16 22 public static boolean equals(Object obj1, Object obj2) { 23 return (obj1 != null) ? (obj1.equals(obj2)) : (obj2 == null); 24 } 25 26 27 31 public static boolean equalsEx(Object obj1, Object obj2) { 32 if (obj1 == null) { 33 return (obj2 == null); 34 } 35 if (obj2 == null) { 36 return false; 37 } 38 if (obj1.getClass().isArray()) { 39 if (obj2.getClass().isArray() == false) { 40 return false; 41 } 42 return Arrays.equals((Object [])obj1, (Object [])obj2); 43 } else { 44 return obj1.equals(obj2); 45 } 46 } 47 48 53 public static boolean equalsType(Object object, Object thiz) { 54 return (object != null) && (object.getClass().equals(thiz.getClass())); 55 } 56 57 59 62 public static Object clone(Object source) throws CloneNotSupportedException { 63 if (source == null) { 64 return null; 65 } 66 try { 67 return ReflectUtil.invokeDeclared(source, "clone", new Class []{}, new Object [] {}); 68 } catch (Exception ex) { 69 throw new CloneNotSupportedException ("Can't invoke clone() on object due to: " + ex.getMessage()); 70 } 71 } 72 73 74 76 79 public static Object cloneViaSerialization(Serializable obj) throws IOException, ClassNotFoundException { 80 FastByteArrayOutputStream bytes = new FastByteArrayOutputStream(); 81 ObjectOutputStream out = null; 82 try { 83 out = new ObjectOutputStream(bytes); 84 out.writeObject(obj); 85 } finally { 86 StreamUtil.close(out); 87 } 88 89 ObjectInputStream in = null; 90 Object objCopy = null; 91 try { 92 in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); 93 objCopy = in.readObject(); 94 } finally { 95 StreamUtil.close(in); 96 } 97 return objCopy; 98 } 99 100 101 103 104 107 public static void writeObject(String dest, Object object) throws IOException { 108 writeObject(new File(dest), object); 109 } 110 111 114 public static void writeObject(File dest, Object object) throws IOException { 115 BufferedOutputStream bos = null; 116 ObjectOutputStream oos = null; 117 try { 118 bos = new BufferedOutputStream(new FileOutputStream(dest)); 119 oos = new ObjectOutputStream(bos); 120 oos.writeObject(object); 121 } finally { 122 StreamUtil.close(oos); 123 StreamUtil.close(bos); 124 } 125 } 126 127 130 public static Object readObject(String source) throws IOException, ClassNotFoundException { 131 return readObject(new File(source)); 132 } 133 134 137 public static Object readObject(File source) throws IOException, ClassNotFoundException { 138 Object result = null; 139 BufferedInputStream bis = null; 140 ObjectInputStream ois = null; 141 try { 142 bis = new BufferedInputStream(new FileInputStream(source)); 143 ois = new ObjectInputStream(bis); 144 result = ois.readObject(); 145 } finally { 146 StreamUtil.close(ois); 147 StreamUtil.close(bis); 148 } 149 return result; 150 } 151 152 153 155 158 public static byte[] objectToByteArray(Object obj) throws IOException { 159 FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); 160 ObjectOutputStream oos = null; 161 try { 162 oos = new ObjectOutputStream(bos); 163 oos.writeObject(obj); 164 oos.flush(); 165 } finally { 166 StreamUtil.close(oos); 167 } 168 return bos.toByteArray(); 169 } 170 171 174 public static Object byteArrayToObject(byte[] data) throws IOException, ClassNotFoundException { 175 Object retObj = null; 176 ByteArrayInputStream bais = new ByteArrayInputStream(data); 177 ObjectInputStream ois = null; 178 try { 179 ois = new ObjectInputStream(bais); 180 retObj = ois.readObject(); 181 } finally { 182 StreamUtil.close(ois); 183 } 184 return retObj; 185 } 186 } 187 | Popular Tags |