add source code that does not have location annotations.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / JavaLayerUtils.java
diff --git a/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/JavaLayerUtils.java b/Robust/src/Benchmarks/SSJava/MP3DecoderInfer/JavaLayerUtils.java
new file mode 100644 (file)
index 0000000..13f2ac0
--- /dev/null
@@ -0,0 +1,227 @@
+/*\r
+ * 11/19/04            1.0 moved to LGPL.\r
+ * 12/12/99            Initial version.        mdm@techie.com\r
+ *-----------------------------------------------------------------------\r
+ *   This program is free software; you can redistribute it and/or modify\r
+ *   it under the terms of the GNU Library General Public License as published\r
+ *   by the Free Software Foundation; either version 2 of the License, or\r
+ *   (at your option) any later version.\r
+ *\r
+ *   This program is distributed in the hope that it will be useful,\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU Library General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU Library General Public\r
+ *   License along with this program; if not, write to the Free Software\r
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
+ *----------------------------------------------------------------------\r
+ */\r
+\r
+\r
+//import java.io.IOException;\r
+//import java.io.InputStream;\r
+//import java.io.InvalidClassException;\r
+//import java.io.InvalidObjectException;\r
+//import java.io.ObjectInputStream;\r
+//import java.io.ObjectOutputStream;\r
+//import java.io.OutputStream;\r
+//import java.lang.reflect.Array;\r
+\r
+/**\r
+ * The JavaLayerUtils class is not strictly part of the JavaLayer API.\r
+ * It serves to provide useful methods and system-wide hooks.\r
+ * \r
+ * @author MDM\r
+ */\r
+public class JavaLayerUtils\r
+{\r
+       static private JavaLayerHook    hook = null;\r
+       \r
+       /**\r
+        * Deserializes the object contained in the given input stream.\r
+        * @param in    The input stream to deserialize an object from.\r
+        * @param cls   The expected class of the deserialized object. \r
+        */\r
+//     static public Object deserialize(InputStream in, Class cls)\r
+       static public Object deserialize(InputStream in)\r
+               throws IOException\r
+       {\r
+//             if (cls==null)\r
+//                     throw new NullPointerException("cls");\r
+               \r
+//             Object obj = deserialize(in, cls);\r
+            Object obj = deserialize(in);\r
+//             if (!cls.isInstance(obj))\r
+//             {\r
+//                     throw new InvalidObjectException("type of deserialized instance not of required class.");\r
+//             }\r
+               \r
+               return obj;\r
+       }\r
+       \r
+       /**\r
+        * Deserializes an object from the given <code>InputStream</code>.\r
+        * The deserialization is delegated to an <code>\r
+        * ObjectInputStream</code> instance. \r
+        * \r
+        * @param in    The <code>InputStream</code> to deserialize an object\r
+        *                              from.\r
+        * \r
+        * @return The object deserialized from the stream. \r
+        * @exception IOException is thrown if there was a problem reading\r
+        *              the underlying stream, or an object could not be deserialized\r
+        *              from the stream.\r
+        * \r
+        * @see java.io.ObjectInputStream\r
+        */\r
+       static public Object deserialize(InputStream in)\r
+               throws IOException\r
+       {\r
+               if (in==null)\r
+                       throw new NullPointerException("in");\r
+               \r
+               //TODO : need to enable after having objectinputstream\r
+               /*\r
+               ObjectInputStream objIn = new ObjectInputStream(in);\r
+               \r
+               Object obj;\r
+               \r
+               try\r
+               {\r
+                       obj = objIn.readObject();\r
+               }\r
+               catch (ClassNotFoundException ex)\r
+               {\r
+                       throw new InvalidClassException(ex.toString());\r
+               }\r
+               \r
+               return obj;\r
+               */\r
+               return null;\r
+       }\r
+\r
+       /**\r
+        * Deserializes an array from a given <code>InputStream</code>.\r
+        * \r
+        * @param in            The <code>InputStream</code> to \r
+        *                                      deserialize an object from.\r
+        *                              \r
+        * @param elemType      The class denoting the type of the array\r
+        *                                      elements.\r
+        * @param length        The expected length of the array, or -1 if\r
+        *                                      any length is expected.\r
+        */\r
+           static public Object deserializeArray(InputStream in, int length)\r
+         throws IOException\r
+    {\r
+         if (length<-1)\r
+              throw new IllegalArgumentException("length");\r
+         \r
+         Object obj = deserialize(in);\r
+         \r
+         return obj;\r
+    }\r
+//     static public Object deserializeArray(InputStream in, Class elemType, int length)\r
+//             throws IOException\r
+//     {\r
+//             if (elemType==null)\r
+//                     throw new NullPointerException("elemType");\r
+//             \r
+//             if (length<-1)\r
+//                     throw new IllegalArgumentException("length");\r
+//             \r
+//             Object obj = deserialize(in);\r
+//             \r
+//             //SSJava will never throw exceptions as it is so this code is meaningless\r
+//             /*\r
+//             Class cls = obj.getClass();\r
+//             \r
+//             if (!cls.isArray())\r
+//                     throw new InvalidObjectException("object is not an array");\r
+//             \r
+//             Class arrayElemType = cls.getComponentType();\r
+//             if (arrayElemType!=elemType)\r
+//                     throw new InvalidObjectException("unexpected array component type");\r
+//                             \r
+//             if (length != -1)\r
+//             {\r
+//                     int arrayLength = Array.getLength(obj);\r
+//                     if (arrayLength!=length)\r
+//                             throw new InvalidObjectException("array length mismatch");\r
+//             }\r
+//             */\r
+//             return obj;\r
+//     }\r
+\r
+//     static public Object deserializeArrayResource(String name, Class elemType, int length)\r
+           static public Object deserializeArrayResource(String name, int length)\r
+               throws IOException\r
+       {               \r
+               InputStream str = getResourceAsStream(name);\r
+               if (str==null)\r
+                       throw new IOException("unable to load resource '"+name+"'");\r
+               \r
+//             Object obj = deserializeArray(str, elemType, length);\r
+               Object obj = deserializeArray(str, length);\r
+               \r
+               return obj;\r
+       }       \r
+       \r
+       static public void serialize(OutputStream out, Object obj)\r
+               throws IOException\r
+       {\r
+       //TODO : need to enable after having objectinputstream\r
+//             if (out==null)\r
+//                     throw new NullPointerException("out");\r
+//             \r
+//             if (obj==null)\r
+//                     throw new NullPointerException("obj");\r
+//             \r
+//             ObjectOutputStream objOut = new ObjectOutputStream(out);\r
+//             objOut.writeObject(obj);\r
+                               \r
+       }\r
+\r
+       /**\r
+        * Sets the system-wide JavaLayer hook.\r
+        */\r
+       static synchronized public void setHook(JavaLayerHook hook0)            \r
+       {\r
+               hook = hook0;\r
+       }\r
+       \r
+       static synchronized public JavaLayerHook getHook()\r
+       {\r
+               return hook;    \r
+       }\r
+       \r
+       /**\r
+        * Retrieves an InputStream for a named resource. \r
+        * \r
+        * @param name  The name of the resource. This must be a simple\r
+        *                              name, and not a qualified package name.\r
+        * \r
+        * @return              The InputStream for the named resource, or null if\r
+        *                              the resource has not been found. If a hook has been \r
+        *                              provided, its getResourceAsStream() method is called\r
+        *                              to retrieve the resource. \r
+        */\r
+       static synchronized public InputStream getResourceAsStream(String name)\r
+       {\r
+               InputStream is = null;\r
+               \r
+               if (hook!=null)\r
+               {\r
+                       is = hook.getResourceAsStream(name);    \r
+               }\r
+               //TODO java reflection\r
+//             else\r
+//             {\r
+//                     Class cls = JavaLayerUtils.class;\r
+//                     is = cls.getResourceAsStream(name);\r
+//             }\r
+               \r
+               return is;              \r
+       }\r
+}\r