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