add another test case for ssjava:
[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 package javazoom.jl.decoder;\r
22 \r
23 import java.io.IOException;\r
24 import java.io.InputStream;\r
25 import java.io.InvalidClassException;\r
26 import java.io.InvalidObjectException;\r
27 import java.io.ObjectInputStream;\r
28 import java.io.ObjectOutputStream;\r
29 import java.io.OutputStream;\r
30 import java.lang.reflect.Array;\r
31 \r
32 /**\r
33  * The JavaLayerUtils class is not strictly part of the JavaLayer API.\r
34  * It serves to provide useful methods and system-wide hooks.\r
35  * \r
36  * @author MDM\r
37  */\r
38 public class JavaLayerUtils\r
39 {\r
40         static private JavaLayerHook    hook = null;\r
41         \r
42         /**\r
43          * Deserializes the object contained in the given input stream.\r
44          * @param in    The input stream to deserialize an object from.\r
45          * @param cls   The expected class of the deserialized object. \r
46          */\r
47         static public Object deserialize(InputStream in, Class cls)\r
48                 throws IOException\r
49         {\r
50                 if (cls==null)\r
51                         throw new NullPointerException("cls");\r
52                 \r
53                 Object obj = deserialize(in, cls);\r
54                 if (!cls.isInstance(obj))\r
55                 {\r
56                         throw new InvalidObjectException("type of deserialized instance not of required class.");\r
57                 }\r
58                 \r
59                 return obj;\r
60         }\r
61         \r
62         /**\r
63          * Deserializes an object from the given <code>InputStream</code>.\r
64          * The deserialization is delegated to an <code>\r
65          * ObjectInputStream</code> instance. \r
66          * \r
67          * @param in    The <code>InputStream</code> to deserialize an object\r
68          *                              from.\r
69          * \r
70          * @return The object deserialized from the stream. \r
71          * @exception IOException is thrown if there was a problem reading\r
72          *              the underlying stream, or an object could not be deserialized\r
73          *              from the stream.\r
74          * \r
75          * @see java.io.ObjectInputStream\r
76          */\r
77         static public Object deserialize(InputStream in)\r
78                 throws IOException\r
79         {\r
80                 if (in==null)\r
81                         throw new NullPointerException("in");\r
82                 \r
83                 ObjectInputStream objIn = new ObjectInputStream(in);\r
84                 \r
85                 Object obj;\r
86                 \r
87                 try\r
88                 {\r
89                         obj = objIn.readObject();\r
90                 }\r
91                 catch (ClassNotFoundException ex)\r
92                 {\r
93                         throw new InvalidClassException(ex.toString());\r
94                 }\r
95                 \r
96                 return obj;\r
97         }\r
98 \r
99         /**\r
100          * Deserializes an array from a given <code>InputStream</code>.\r
101          * \r
102          * @param in            The <code>InputStream</code> to \r
103          *                                      deserialize an object from.\r
104          *                              \r
105          * @param elemType      The class denoting the type of the array\r
106          *                                      elements.\r
107          * @param length        The expected length of the array, or -1 if\r
108          *                                      any length is expected.\r
109          */\r
110         static public Object deserializeArray(InputStream in, Class elemType, int length)\r
111                 throws IOException\r
112         {\r
113                 if (elemType==null)\r
114                         throw new NullPointerException("elemType");\r
115                 \r
116                 if (length<-1)\r
117                         throw new IllegalArgumentException("length");\r
118                 \r
119                 Object obj = deserialize(in);\r
120                 \r
121                 Class cls = obj.getClass();\r
122                 \r
123                 \r
124                 if (!cls.isArray())\r
125                         throw new InvalidObjectException("object is not an array");\r
126                 \r
127                 Class arrayElemType = cls.getComponentType();\r
128                 if (arrayElemType!=elemType)\r
129                         throw new InvalidObjectException("unexpected array component type");\r
130                                 \r
131                 if (length != -1)\r
132                 {\r
133                         int arrayLength = Array.getLength(obj);\r
134                         if (arrayLength!=length)\r
135                                 throw new InvalidObjectException("array length mismatch");\r
136                 }\r
137                 \r
138                 return obj;\r
139         }\r
140 \r
141         static public Object deserializeArrayResource(String name, Class elemType, int length)\r
142                 throws IOException\r
143         {               \r
144                 InputStream str = getResourceAsStream(name);\r
145                 if (str==null)\r
146                         throw new IOException("unable to load resource '"+name+"'");\r
147                 \r
148                 Object obj = deserializeArray(str, elemType, length);\r
149                 \r
150                 return obj;\r
151         }       \r
152         \r
153         static public void serialize(OutputStream out, Object obj)\r
154                 throws IOException\r
155         {\r
156                 if (out==null)\r
157                         throw new NullPointerException("out");\r
158                 \r
159                 if (obj==null)\r
160                         throw new NullPointerException("obj");\r
161                 \r
162                 ObjectOutputStream objOut = new ObjectOutputStream(out);\r
163                 objOut.writeObject(obj);\r
164                                 \r
165         }\r
166 \r
167         /**\r
168          * Sets the system-wide JavaLayer hook.\r
169          */\r
170         static synchronized public void setHook(JavaLayerHook hook0)            \r
171         {\r
172                 hook = hook0;\r
173         }\r
174         \r
175         static synchronized public JavaLayerHook getHook()\r
176         {\r
177                 return hook;    \r
178         }\r
179         \r
180         /**\r
181          * Retrieves an InputStream for a named resource. \r
182          * \r
183          * @param name  The name of the resource. This must be a simple\r
184          *                              name, and not a qualified package name.\r
185          * \r
186          * @return              The InputStream for the named resource, or null if\r
187          *                              the resource has not been found. If a hook has been \r
188          *                              provided, its getResourceAsStream() method is called\r
189          *                              to retrieve the resource. \r
190          */\r
191         static synchronized public InputStream getResourceAsStream(String name)\r
192         {\r
193                 InputStream is = null;\r
194                 \r
195                 if (hook!=null)\r
196                 {\r
197                         is = hook.getResourceAsStream(name);    \r
198                 }\r
199                 else\r
200                 {\r
201                         Class cls = JavaLayerUtils.class;\r
202                         is = cls.getResourceAsStream(name);\r
203                 }\r
204                 \r
205                 return is;              \r
206         }\r
207 }\r