2156e3dbecdff5c606f612de3888655b35dcb8df
[IRC.git] / Robust / src / Tests / ssJava / mp3decoder / JavaLayerUtils.java
1 /*\r
2  * 11/19/04             1.0 moved to LGPL.\r
3  * 12/12/99             Initial version.        mdm@techie.com\r
4  *-----------------------------------------------------------------------\r
5  *   This program is free software; you can redistribute it and/or modify\r
6  *   it under the terms of the GNU Library General Public License as published\r
7  *   by the Free Software Foundation; either version 2 of the License, or\r
8  *   (at your option) any later version.\r
9  *\r
10  *   This program is distributed in the hope that it will be useful,\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  *   GNU Library General Public License for more details.\r
14  *\r
15  *   You should have received a copy of the GNU Library General Public\r
16  *   License along with this program; if not, write to the Free Software\r
17  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
18  *----------------------------------------------------------------------\r
19  */\r
20 \r
21 \r
22 import java.io.IOException;\r
23 import java.io.InputStream;\r
24 import java.io.InvalidClassException;\r
25 import java.io.InvalidObjectException;\r
26 import java.io.ObjectInputStream;\r
27 import java.io.ObjectOutputStream;\r
28 import java.io.OutputStream;\r
29 import java.lang.reflect.Array;\r
30 \r
31 /**\r
32  * The JavaLayerUtils class is not strictly part of the JavaLayer API.\r
33  * It serves to provide useful methods and system-wide hooks.\r
34  * \r
35  * @author MDM\r
36  */\r
37 public class JavaLayerUtils\r
38 {\r
39         static private JavaLayerHook    hook = null;\r
40         \r
41         /**\r
42          * Deserializes the object contained in the given input stream.\r
43          * @param in    The input stream to deserialize an object from.\r
44          * @param cls   The expected class of the deserialized object. \r
45          */\r
46         static public Object deserialize(InputStream in, Class cls)\r
47                 throws IOException\r
48         {\r
49                 if (cls==null)\r
50                         throw new NullPointerException("cls");\r
51                 \r
52                 Object obj = deserialize(in, cls);\r
53                 if (!cls.isInstance(obj))\r
54                 {\r
55                         throw new InvalidObjectException("type of deserialized instance not of required class.");\r
56                 }\r
57                 \r
58                 return obj;\r
59         }\r
60         \r
61         /**\r
62          * Deserializes an object from the given <code>InputStream</code>.\r
63          * The deserialization is delegated to an <code>\r
64          * ObjectInputStream</code> instance. \r
65          * \r
66          * @param in    The <code>InputStream</code> to deserialize an object\r
67          *                              from.\r
68          * \r
69          * @return The object deserialized from the stream. \r
70          * @exception IOException is thrown if there was a problem reading\r
71          *              the underlying stream, or an object could not be deserialized\r
72          *              from the stream.\r
73          * \r
74          * @see java.io.ObjectInputStream\r
75          */\r
76         static public Object deserialize(InputStream in)\r
77                 throws IOException\r
78         {\r
79                 if (in==null)\r
80                         throw new NullPointerException("in");\r
81                 \r
82                 ObjectInputStream objIn = new ObjectInputStream(in);\r
83                 \r
84                 Object obj;\r
85                 \r
86                 try\r
87                 {\r
88                         obj = objIn.readObject();\r
89                 }\r
90                 catch (ClassNotFoundException ex)\r
91                 {\r
92                         throw new InvalidClassException(ex.toString());\r
93                 }\r
94                 \r
95                 return obj;\r
96         }\r
97 \r
98         /**\r
99          * Deserializes an array from a given <code>InputStream</code>.\r
100          * \r
101          * @param in            The <code>InputStream</code> to \r
102          *                                      deserialize an object from.\r
103          *                              \r
104          * @param elemType      The class denoting the type of the array\r
105          *                                      elements.\r
106          * @param length        The expected length of the array, or -1 if\r
107          *                                      any length is expected.\r
108          */\r
109         static public Object deserializeArray(InputStream in, Class elemType, int length)\r
110                 throws IOException\r
111         {\r
112                 if (elemType==null)\r
113                         throw new NullPointerException("elemType");\r
114                 \r
115                 if (length<-1)\r
116                         throw new IllegalArgumentException("length");\r
117                 \r
118                 Object obj = deserialize(in);\r
119                 \r
120                 Class cls = obj.getClass();\r
121                 \r
122                 \r
123                 if (!cls.isArray())\r
124                         throw new InvalidObjectException("object is not an array");\r
125                 \r
126                 Class arrayElemType = cls.getComponentType();\r
127                 if (arrayElemType!=elemType)\r
128                         throw new InvalidObjectException("unexpected array component type");\r
129                                 \r
130                 if (length != -1)\r
131                 {\r
132                         int arrayLength = Array.getLength(obj);\r
133                         if (arrayLength!=length)\r
134                                 throw new InvalidObjectException("array length mismatch");\r
135                 }\r
136                 \r
137                 return obj;\r
138         }\r
139 \r
140         static public Object deserializeArrayResource(String name, Class elemType, int length)\r
141                 throws IOException\r
142         {               \r
143                 InputStream str = getResourceAsStream(name);\r
144                 if (str==null)\r
145                         throw new IOException("unable to load resource '"+name+"'");\r
146                 \r
147                 Object obj = deserializeArray(str, elemType, length);\r
148                 \r
149                 return obj;\r
150         }       \r
151         \r
152         static public void serialize(OutputStream out, Object obj)\r
153                 throws IOException\r
154         {\r
155                 if (out==null)\r
156                         throw new NullPointerException("out");\r
157                 \r
158                 if (obj==null)\r
159                         throw new NullPointerException("obj");\r
160                 \r
161                 ObjectOutputStream objOut = new ObjectOutputStream(out);\r
162                 objOut.writeObject(obj);\r
163                                 \r
164         }\r
165 \r
166         /**\r
167          * Sets the system-wide JavaLayer hook.\r
168          */\r
169         static synchronized public void setHook(JavaLayerHook hook0)            \r
170         {\r
171                 hook = hook0;\r
172         }\r
173         \r
174         static synchronized public JavaLayerHook getHook()\r
175         {\r
176                 return hook;    \r
177         }\r
178         \r
179         /**\r
180          * Retrieves an InputStream for a named resource. \r
181          * \r
182          * @param name  The name of the resource. This must be a simple\r
183          *                              name, and not a qualified package name.\r
184          * \r
185          * @return              The InputStream for the named resource, or null if\r
186          *                              the resource has not been found. If a hook has been \r
187          *                              provided, its getResourceAsStream() method is called\r
188          *                              to retrieve the resource. \r
189          */\r
190         static synchronized public InputStream getResourceAsStream(String name)\r
191         {\r
192                 InputStream is = null;\r
193                 \r
194                 if (hook!=null)\r
195                 {\r
196                         is = hook.getResourceAsStream(name);    \r
197                 }\r
198                 else\r
199                 {\r
200                         Class cls = JavaLayerUtils.class;\r
201                         is = cls.getResourceAsStream(name);\r
202                 }\r
203                 \r
204                 return is;              \r
205         }\r
206 }\r