KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > license > LicenseUtils


1 package org.sapia.util.license;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9
10 /**
11  * A utility class.
12  *
13  * @author Yanick Duchesne
14  *
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class LicenseUtils {
22   
23   /**
24    * Deserialized an object from the given bytes.
25    * <p>
26    * <b>Note:</b> the given stream is closed by this method.
27    * @param bytes a byte array.
28    * @return an <code>Object</code>.
29    * @throws IOException if an IO problem occurs.
30    */

31   public static Object JavaDoc fromBytes(InputStream JavaDoc bytes) throws IOException JavaDoc{
32     ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bytes);
33     try{
34      return ois.readObject();
35     }catch(ClassNotFoundException JavaDoc e){
36       throw new IOException JavaDoc("Class not found - " + e.getMessage());
37     }finally{
38       if(ois != null)
39         ois.close();
40     }
41   }
42   
43   /**
44    * Serializes the given object to an array of bytes and returns the latter.
45    *
46    * @param serializable an <code>Object</code>.
47    * @return an array of bytes.
48    * @throws IOException if an IO problem occurs.
49    */

50   public static byte[] toBytes(Object JavaDoc serializable) throws IOException JavaDoc{
51     ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
52     toBytes(serializable, bos);
53     return bos.toByteArray();
54   }
55   
56   /**
57    * Serializes the given object to the given stream.
58    * <p>
59    * <b>NOTE:</b>
60    * This method closesd the given stream prior to returning.
61    *
62    * @param serializable an <code>Object</code>.
63    * @param os an <code>OutputStream</code>.
64    * @return an array of bytes.
65    * @throws IOException if an IO problem occurs.
66    */

67   public static void toBytes(Object JavaDoc serializable, OutputStream JavaDoc os) throws IOException JavaDoc{
68     ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(os);
69     try{
70       oos.writeObject(serializable);
71     }finally{
72       oos.flush();
73       oos.close();
74     }
75   }
76 }
77
Popular Tags