KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > io > IOUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.base.io;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33
34 import java.util.logging.*;
35 import com.sun.logging.*;
36
37 import com.sun.ejb.spi.container.ContainerCallback;
38 import com.sun.ejb.spi.io.J2EEObjectStreamFactory;
39 import com.sun.ejb.spi.io.NonSerializableObjectHandler;
40
41 public class IOUtils {
42
43     private static Logger _ejbLogger =
44        LogDomains.getLogger(LogDomains.EJB_LOGGER);
45
46     private static J2EEObjectStreamFactory _streamFactory;
47
48     public static final void setJ2EEObjectStreamFactory(
49             J2EEObjectStreamFactory factory)
50     {
51         _streamFactory = factory;
52     }
53     
54     public static ObjectInputStream JavaDoc createObjectInputStream(
55         final InputStream JavaDoc is, final boolean resolveObject,
56         final ClassLoader JavaDoc loader)
57     throws Exception JavaDoc
58     {
59     return _streamFactory.createObjectInputStream(is, resolveObject, loader);
60     }
61
62     public static ObjectOutputStream JavaDoc createObjectOutputStream(
63         final OutputStream JavaDoc os, final boolean replaceObject,
64         final NonSerializableObjectHandler handler)
65     throws IOException JavaDoc
66     {
67     return _streamFactory.createObjectOutputStream(os, replaceObject,
68         handler);
69     }
70
71     public static final byte[] serializeObject(Object JavaDoc obj, boolean replaceObject)
72     throws java.io.NotSerializableException JavaDoc
73     {
74         byte[] data = null;
75         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
76         ObjectOutputStream JavaDoc oos = null;
77         try {
78             oos = _streamFactory.createObjectOutputStream(
79                 bos, replaceObject,
80                 new NonSerializableObjectHandler() {
81                     public Object JavaDoc handleNonSerializableObject(Object JavaDoc obj) {
82                         return obj;
83                     }
84                 });
85
86             oos.writeObject(obj);
87             oos.flush();
88             data = bos.toByteArray();
89         } catch (java.io.NotSerializableException JavaDoc notSerEx) {
90             throw notSerEx;
91         } catch (Exception JavaDoc th) {
92             _ejbLogger.log(Level.FINE, "Error during serialization", th);
93         } finally {
94             if (oos != null) {
95                 try { oos.close(); } catch (Exception JavaDoc ex) {}
96             }
97             try { bos.close(); } catch (Exception JavaDoc ex) {}
98         }
99
100     return data;
101     }
102
103     public static final Object JavaDoc deserializeObject(byte[] data, boolean resolveObject,
104             ClassLoader JavaDoc classLoader)
105     throws Exception JavaDoc
106     {
107         Object JavaDoc obj = null;
108     ByteArrayInputStream JavaDoc bis = null;
109     ObjectInputStream JavaDoc ois = null;
110         try {
111             bis = new ByteArrayInputStream JavaDoc(data);
112             ois = _streamFactory.createObjectInputStream(bis, resolveObject,
113             classLoader);
114             obj = ois.readObject();
115         } catch (Exception JavaDoc ex) {
116             _ejbLogger.log(Level.FINE, "Error during deserialization", ex);
117             throw ex;
118         } finally {
119             try { ois.close(); } catch (Exception JavaDoc ex) {}
120             try { bis.close(); } catch (Exception JavaDoc ex) {}
121     }
122         return obj;
123     }
124
125 }
126
Popular Tags