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