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
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
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
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
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
36 public class JavaLayerUtils {
\r
37 static private JavaLayerHook hook = null;
\r
40 * Deserializes the object contained in the given input stream.
\r
43 * The input stream to deserialize an object from.
\r
45 * The expected class of the deserialized object.
\r
47 // static public Object deserialize(InputStream in, Class cls)
\r
48 static public Object deserialize(InputStream in) throws IOException {
\r
50 // throw new NullPointerException("cls");
\r
52 // Object obj = deserialize(in, cls);
\r
53 Object obj = deserialize(in);
\r
54 // if (!cls.isInstance(obj))
\r
57 // InvalidObjectException("type of deserialized instance not of required class.");
\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
69 * The <code>InputStream</code> to deserialize an object from.
\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
77 * @see java.io.ObjectInputStream
\r
79 static public Object deserialize(InputStream in) throws IOException {
\r
81 throw new NullPointerException("in");
\r
83 // TODO : need to enable after having objectinputstream
\r
85 * ObjectInputStream objIn = new ObjectInputStream(in);
\r
89 * try { obj = objIn.readObject(); } catch (ClassNotFoundException ex) {
\r
90 * throw new InvalidClassException(ex.toString()); }
\r
98 * Deserializes an array from a given <code>InputStream</code>.
\r
101 * The <code>InputStream</code> to deserialize an object from.
\r
104 * The class denoting the type of the array elements.
\r
106 * The expected length of the array, or -1 if any length is expected.
\r
108 static public Object deserializeArray(InputStream in, int length) throws IOException {
\r
110 throw new IllegalArgumentException("length");
\r
112 Object obj = deserialize(in);
\r
117 // static public Object deserializeArray(InputStream in, Class elemType, int
\r
119 // throws IOException
\r
121 // if (elemType==null)
\r
122 // throw new NullPointerException("elemType");
\r
125 // throw new IllegalArgumentException("length");
\r
127 // Object obj = deserialize(in);
\r
129 // //SSJava will never throw exceptions as it is so this code is meaningless
\r
131 // Class cls = obj.getClass();
\r
133 // if (!cls.isArray())
\r
134 // throw new InvalidObjectException("object is not an array");
\r
136 // Class arrayElemType = cls.getComponentType();
\r
137 // if (arrayElemType!=elemType)
\r
138 // throw new InvalidObjectException("unexpected array component type");
\r
140 // if (length != -1)
\r
142 // int arrayLength = Array.getLength(obj);
\r
143 // if (arrayLength!=length)
\r
144 // throw new InvalidObjectException("array length mismatch");
\r
150 // static public Object deserializeArrayResource(String name, Class elemType,
\r
152 static public Object deserializeArrayResource(String name, int length) throws IOException {
\r
153 InputStream str = getResourceAsStream(name);
\r
155 throw new IOException("unable to load resource '" + name + "'");
\r
157 // Object obj = deserializeArray(str, elemType, length);
\r
158 Object obj = deserializeArray(str, length);
\r
163 static public void serialize(OutputStream out, Object obj) throws IOException {
\r
164 // TODO : need to enable after having objectinputstream
\r
166 // throw new NullPointerException("out");
\r
169 // throw new NullPointerException("obj");
\r
171 // ObjectOutputStream objOut = new ObjectOutputStream(out);
\r
172 // objOut.writeObject(obj);
\r
177 * Sets the system-wide JavaLayer hook.
\r
179 static synchronized public void setHook(JavaLayerHook hook0) {
\r
183 static synchronized public JavaLayerHook getHook() {
\r
188 * Retrieves an InputStream for a named resource.
\r
191 * The name of the resource. This must be a simple name, and not a
\r
192 * qualified package name.
\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
198 static synchronized public InputStream getResourceAsStream(String name) {
\r
199 InputStream is = null;
\r
201 if (hook != null) {
\r
202 is = hook.getResourceAsStream(name);
\r
204 // TODO java reflection
\r
207 // Class cls = JavaLayerUtils.class;
\r
208 // is = cls.getResourceAsStream(name);
\r