KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilObject


1 /*
2  * $Id: UtilObject.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30
31 /**
32  * UtilObject
33  *
34  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
35  * @version $Rev: 5462 $
36  * @since 3.1
37  */

38 public class UtilObject {
39
40     public static final String JavaDoc module = UtilObject.class.getName();
41
42     /** Serialize an object to a byte array */
43     public static byte[] getBytes(Object JavaDoc obj) {
44         ByteArrayOutputStream JavaDoc bos = null;
45         ObjectOutputStream JavaDoc oos = null;
46         byte[] data = null;
47         try {
48             bos = new ByteArrayOutputStream JavaDoc();
49             oos = new ObjectOutputStream JavaDoc(bos);
50             oos.writeObject(obj);
51             data = bos.toByteArray();
52         } catch (IOException JavaDoc e) {
53             Debug.logError(e, module);
54         } finally {
55             try {
56                 if (oos != null) {
57                     oos.flush();
58                     oos.close();
59                 }
60                 if (bos != null) {
61                     bos.close();
62                 }
63             } catch (IOException JavaDoc e) {
64                 Debug.logError(e, module);
65             }
66         }
67
68         return data;
69     }
70     
71     public static long getByteCount(Object JavaDoc obj) {
72         OutputStreamByteCount osbc = null;
73         ObjectOutputStream JavaDoc oos = null;
74         try {
75             osbc = new OutputStreamByteCount();
76             oos = new ObjectOutputStream JavaDoc(osbc);
77             oos.writeObject(obj);
78         } catch (IOException JavaDoc e) {
79             Debug.logError(e, module);
80         } finally {
81             try {
82                 if (oos != null) {
83                     oos.flush();
84                     oos.close();
85                 }
86                 if (osbc != null) {
87                     osbc.close();
88                 }
89             } catch (IOException JavaDoc e) {
90                 Debug.logError(e, module);
91             }
92         }
93         if (osbc != null) {
94             return osbc.getByteCount();
95         } else {
96             return 0;
97         }
98     }
99     
100     /** Deserialize a byte array back to an object */
101     public static Object JavaDoc getObject(byte[] bytes) {
102         ByteArrayInputStream JavaDoc bis = null;
103         ObjectInputStream ois = null;
104         Object JavaDoc obj = null;
105
106         try {
107             bis = new ByteArrayInputStream JavaDoc(bytes);
108             ois = new ObjectInputStream(bis, Thread.currentThread().getContextClassLoader());
109             obj = ois.readObject();
110         } catch (ClassNotFoundException JavaDoc e) {
111             Debug.logError(e, module);
112         } catch (IOException JavaDoc e) {
113             Debug.logError(e, module);
114         } finally {
115             try {
116                 if (ois != null) {
117                     ois.close();
118                 }
119                 if (bis != null) {
120                     bis.close();
121                 }
122             } catch (IOException JavaDoc e) {
123                 Debug.logError(e, module);
124             }
125         }
126
127         return obj;
128     }
129
130     public static boolean equalsHelper(Object JavaDoc o1, Object JavaDoc o2) {
131         if (o1 == o2) {
132             // handles same-reference, or null
133
return true;
134         } else if (o1 == null || o2 == null) {
135             // either o1 or o2 is null, but not both
136
return false;
137         } else {
138             return o1.equals(o2);
139         }
140     }
141
142     public static int compareToHelper(Comparable JavaDoc o1, Object JavaDoc o2) {
143         if (o1 == o2) {
144             // handles same-reference, or null
145
return 0;
146         } else if (o1 == null) {
147             return -1;
148         } else if (o2 == null) {
149             // either o1 or o2 is null, but not both
150
return 1;
151         } else {
152             return o1.compareTo(o2);
153         }
154     }
155
156     public static int doHashCode(Object JavaDoc o1) {
157         if (o1 == null) return 0;
158         return o1.hashCode();
159     }
160 }
161
Popular Tags