KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > ObjectUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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 JavaDoc;
10
11 /**
12  * Various object utilities.
13  */

14 public class ObjectUtil {
15
16     /**
17      * Safely compares two objects just like <code>equals()</code> would, except
18      * it allows any of the 2 objects to be <code>null</code>.
19      *
20      * @return <code>true</code> if arguments are equal, otherwise <code>false</code>
21      */

22     public static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
23         return (obj1 != null) ? (obj1.equals(obj2)) : (obj2 == null);
24     }
25
26
27     /**
28      * Compares two objects or two object arrays. Useful for {@link Object#equals(Object)}.
29      * @see #equals(Object, Object)
30      */

31     public static boolean equalsEx(Object JavaDoc obj1, Object JavaDoc 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 JavaDoc[])obj1, (Object JavaDoc[])obj2);
43         } else {
44             return obj1.equals(obj2);
45         }
46     }
47
48     /**
49      * Non-symetric utility for comparing types of two objects. Useful for {@link Object#equals(Object)}.
50      * @param object <code>equals()</code> argument
51      * @param thiz current class that overrides <code>equals()</code>
52      */

53     public static boolean equalsType(Object JavaDoc object, Object JavaDoc thiz) {
54         return (object != null) && (object.getClass().equals(thiz.getClass()));
55     }
56
57     // ---------------------------------------------------------------- clone
58

59     /**
60      * Clone an object by invoking it's <code>clone()</code> method, even if it is not overriden.
61      */

62     public static Object JavaDoc clone(Object JavaDoc source) throws CloneNotSupportedException JavaDoc {
63         if (source == null) {
64             return null;
65         }
66         try {
67             return ReflectUtil.invokeDeclared(source, "clone", new Class JavaDoc[]{}, new Object JavaDoc[] {});
68         } catch (Exception JavaDoc ex) {
69             throw new CloneNotSupportedException JavaDoc("Can't invoke clone() on object due to: " + ex.getMessage());
70         }
71     }
72
73
74     // ---------------------------------------------------------------- clone
75

76     /**
77      * Create object copy using serialization mechanism.
78      */

79     public static Object JavaDoc cloneViaSerialization(Serializable obj) throws IOException, ClassNotFoundException JavaDoc {
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 JavaDoc 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     // ---------------------------------------------------------------- serialization to file
102

103
104     /**
105      * @see #writeObject(java.io.File, Object)
106      */

107     public static void writeObject(String JavaDoc dest, Object JavaDoc object) throws IOException {
108         writeObject(new File(dest), object);
109     }
110
111     /**
112      * Writes serializable object to a file. Existing file will be overwritten.
113      */

114     public static void writeObject(File dest, Object JavaDoc 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     /**
128      * @see #readObject(java.io.File)
129      */

130     public static Object JavaDoc readObject(String JavaDoc source) throws IOException, ClassNotFoundException JavaDoc {
131         return readObject(new File(source));
132     }
133
134     /**
135      * Reads serialized object from the file.
136      */

137     public static Object JavaDoc readObject(File source) throws IOException, ClassNotFoundException JavaDoc {
138         Object JavaDoc 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     // ---------------------------------------------------------------- serialization to byte array
154

155     /**
156      * Serialize an object to byte array.
157      */

158     public static byte[] objectToByteArray(Object JavaDoc 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     /**
172      * Deserialize an object from byte array.
173      */

174     public static Object JavaDoc byteArrayToObject(byte[] data) throws IOException, ClassNotFoundException JavaDoc {
175         Object JavaDoc 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