Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/util/SerializationHelper.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activemq.util;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  import java.io.ObjectStreamClass;
27  import java.io.OutputStream;
28  import java.lang.reflect.Proxy;
29  
30  /**
31   * @version $Revision: 1.1.1.1 $
32   */
33  public class SerializationHelper {
34      
35      static final private ClassLoader ACTIVEMQ_CLASSLOADER = SerializationHelper.class.getClassLoader();
36  
37      /**
38       * Serialize an Object to a ByteArray
39       * @param object
40       * @return a byte[] array
41       * @throws IOException
42       */
43      public static byte[] writeObject(Object object) throws IOException {
44          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
45          ObjectOutputStream out = new ObjectOutputStream(buffer);
46          out.writeObject(object);
47          out.close();
48          return buffer.toByteArray();
49      }
50  
51      /**
52       * Read an Object from a byte array
53       * @param data
54       * @return
55       * @throws IOException
56       * @throws ClassNotFoundException
57       */
58      public static Object readObject(byte[] data) throws IOException, ClassNotFoundException {
59          ByteArrayInputStream buffer = new ByteArrayInputStream(data);
60          ObjectInputStreamExt in = new ObjectInputStreamExt(buffer);
61          Object answer = in.readObject();
62          in.close();
63          return answer;
64      }
65      
66      /**
67       * read an object from an InputStream
68       * @param in
69       * @return
70       * @throws IOException
71       * @throws ClassNotFoundException
72       */
73      public static Object readObject(InputStream in) throws IOException, ClassNotFoundException{
74          ObjectInputStreamExt objIn = new ObjectInputStreamExt(in);
75          Object result = objIn.readObject();
76          return result;
77          
78      }
79      
80      /**
81       * Write an Object to an OutputStream
82       * @param out
83       * @param obj
84       * @throws IOException
85       */
86      public static void writeObject(OutputStream out, Object obj) throws IOException{
87          ObjectOutputStream objOut = new ObjectOutputStream(out);
88          objOut.writeObject(obj);
89          objOut.flush();
90      }
91      
92      static public class ObjectInputStreamExt extends ObjectInputStream {
93  
94          public ObjectInputStreamExt(InputStream in) throws IOException {
95              super(in);
96          }
97  
98          protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
99              ClassLoader cl = Thread.currentThread().getContextClassLoader();
100             return load(classDesc.getName(), cl);
101         }
102 
103         protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
104             ClassLoader cl = Thread.currentThread().getContextClassLoader();
105             Class[] cinterfaces = new Class[interfaces.length];
106             for (int i = 0; i < interfaces.length; i++)
107                 cinterfaces[i] = load(interfaces[i], cl);
108 
109             try {
110                 return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces);
111             } catch (IllegalArgumentException e) {
112                 throw new ClassNotFoundException(null, e);
113             }
114         }
115 
116         private Class load(String className, ClassLoader cl) throws ClassNotFoundException {
117             try {
118                 return ClassLoading.loadClass(className, cl);
119             } catch ( ClassNotFoundException e ) {
120                 return ClassLoading.loadClass(className, ACTIVEMQ_CLASSLOADER);
121             }
122         }
123 
124      }
125 
126 }