add source code that does not have location annotations.
[IRC.git] / Robust / src / Benchmarks / 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         static public Object deserialize(InputStream in)\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              Object obj = deserialize(in);\r
55 //              if (!cls.isInstance(obj))\r
56 //              {\r
57 //                      throw new InvalidObjectException("type of deserialized instance not of required class.");\r
58 //              }\r
59                 \r
60                 return obj;\r
61         }\r
62         \r
63         /**\r
64          * Deserializes an object from the given <code>InputStream</code>.\r
65          * The deserialization is delegated to an <code>\r
66          * ObjectInputStream</code> instance. \r
67          * \r
68          * @param in    The <code>InputStream</code> to deserialize an object\r
69          *                              from.\r
70          * \r
71          * @return The object deserialized from the stream. \r
72          * @exception IOException is thrown if there was a problem reading\r
73          *              the underlying stream, or an object could not be deserialized\r
74          *              from the stream.\r
75          * \r
76          * @see java.io.ObjectInputStream\r
77          */\r
78         static public Object deserialize(InputStream in)\r
79                 throws IOException\r
80         {\r
81                 if (in==null)\r
82                         throw new NullPointerException("in");\r
83                 \r
84                 //TODO : need to enable after having objectinputstream\r
85                 /*\r
86                 ObjectInputStream objIn = new ObjectInputStream(in);\r
87                 \r
88                 Object obj;\r
89                 \r
90                 try\r
91                 {\r
92                         obj = objIn.readObject();\r
93                 }\r
94                 catch (ClassNotFoundException ex)\r
95                 {\r
96                         throw new InvalidClassException(ex.toString());\r
97                 }\r
98                 \r
99                 return obj;\r
100                 */\r
101                 return null;\r
102         }\r
103 \r
104         /**\r
105          * Deserializes an array from a given <code>InputStream</code>.\r
106          * \r
107          * @param in            The <code>InputStream</code> to \r
108          *                                      deserialize an object from.\r
109          *                              \r
110          * @param elemType      The class denoting the type of the array\r
111          *                                      elements.\r
112          * @param length        The expected length of the array, or -1 if\r
113          *                                      any length is expected.\r
114          */\r
115             static public Object deserializeArray(InputStream in, int length)\r
116          throws IOException\r
117     {\r
118          if (length<-1)\r
119               throw new IllegalArgumentException("length");\r
120          \r
121          Object obj = deserialize(in);\r
122          \r
123          return obj;\r
124     }\r
125 //      static public Object deserializeArray(InputStream in, Class elemType, int length)\r
126 //              throws IOException\r
127 //      {\r
128 //              if (elemType==null)\r
129 //                      throw new NullPointerException("elemType");\r
130 //              \r
131 //              if (length<-1)\r
132 //                      throw new IllegalArgumentException("length");\r
133 //              \r
134 //              Object obj = deserialize(in);\r
135 //              \r
136 //              //SSJava will never throw exceptions as it is so this code is meaningless\r
137 //              /*\r
138 //              Class cls = obj.getClass();\r
139 //              \r
140 //              if (!cls.isArray())\r
141 //                      throw new InvalidObjectException("object is not an array");\r
142 //              \r
143 //              Class arrayElemType = cls.getComponentType();\r
144 //              if (arrayElemType!=elemType)\r
145 //                      throw new InvalidObjectException("unexpected array component type");\r
146 //                              \r
147 //              if (length != -1)\r
148 //              {\r
149 //                      int arrayLength = Array.getLength(obj);\r
150 //                      if (arrayLength!=length)\r
151 //                              throw new InvalidObjectException("array length mismatch");\r
152 //              }\r
153 //              */\r
154 //              return obj;\r
155 //      }\r
156 \r
157 //      static public Object deserializeArrayResource(String name, Class elemType, int length)\r
158             static public Object deserializeArrayResource(String name, int length)\r
159                 throws IOException\r
160         {               \r
161                 InputStream str = getResourceAsStream(name);\r
162                 if (str==null)\r
163                         throw new IOException("unable to load resource '"+name+"'");\r
164                 \r
165 //              Object obj = deserializeArray(str, elemType, length);\r
166                 Object obj = deserializeArray(str, length);\r
167                 \r
168                 return obj;\r
169         }       \r
170         \r
171         static public void serialize(OutputStream out, Object obj)\r
172                 throws IOException\r
173         {\r
174        //TODO : need to enable after having objectinputstream\r
175 //              if (out==null)\r
176 //                      throw new NullPointerException("out");\r
177 //              \r
178 //              if (obj==null)\r
179 //                      throw new NullPointerException("obj");\r
180 //              \r
181 //              ObjectOutputStream objOut = new ObjectOutputStream(out);\r
182 //              objOut.writeObject(obj);\r
183                                 \r
184         }\r
185 \r
186         /**\r
187          * Sets the system-wide JavaLayer hook.\r
188          */\r
189         static synchronized public void setHook(JavaLayerHook hook0)            \r
190         {\r
191                 hook = hook0;\r
192         }\r
193         \r
194         static synchronized public JavaLayerHook getHook()\r
195         {\r
196                 return hook;    \r
197         }\r
198         \r
199         /**\r
200          * Retrieves an InputStream for a named resource. \r
201          * \r
202          * @param name  The name of the resource. This must be a simple\r
203          *                              name, and not a qualified package name.\r
204          * \r
205          * @return              The InputStream for the named resource, or null if\r
206          *                              the resource has not been found. If a hook has been \r
207          *                              provided, its getResourceAsStream() method is called\r
208          *                              to retrieve the resource. \r
209          */\r
210         static synchronized public InputStream getResourceAsStream(String name)\r
211         {\r
212                 InputStream is = null;\r
213                 \r
214                 if (hook!=null)\r
215                 {\r
216                         is = hook.getResourceAsStream(name);    \r
217                 }\r
218                 //TODO java reflection\r
219 //              else\r
220 //              {\r
221 //                      Class cls = JavaLayerUtils.class;\r
222 //                      is = cls.getResourceAsStream(name);\r
223 //              }\r
224                 \r
225                 return is;              \r
226         }\r
227 }\r