Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/JavaLayerUtils.java
8650 views
/*1* 11/19/04 1.0 moved to LGPL.2* 12/12/99 Initial version. [email protected]3*-----------------------------------------------------------------------4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU Library General Public License as published6* by the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU Library General Public License for more details.13*14* You should have received a copy of the GNU Library General Public15* License along with this program; if not, write to the Free Software16* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.17*----------------------------------------------------------------------18*/1920package javazoom.jl.decoder;2122import java.io.IOException;23import java.io.InputStream;24import java.io.InvalidClassException;25import java.io.InvalidObjectException;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.io.OutputStream;29import java.lang.reflect.Array;3031/**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 MDM36*/37public class JavaLayerUtils38{39private static JavaLayerHook hook = null;4041/**42* Deserializes the object contained in the given input stream.43*44* @param in The input stream to deserialize an object from.45* @param cls The expected class of the deserialized object.46*/47public static Object deserialize(InputStream in, Class cls)48throws IOException49{50if (cls==null)51throw new NullPointerException("cls");5253Object obj = deserialize(in, cls);54if (!cls.isInstance(obj))55{56throw new InvalidObjectException("type of deserialized instance not of required class.");57}5859return obj;60}6162/**63* Deserializes an object from the given <code>InputStream</code>.64* The deserialization is delegated to an <code>65* ObjectInputStream</code> instance.66*67* @param in The <code>InputStream</code> to deserialize an object68* from.69*70* @return The object deserialized from the stream.71* @exception IOException is thrown if there was a problem reading72* the underlying stream, or an object could not be deserialized73* from the stream.74*75* @see java.io.ObjectInputStream76*/77public static Object deserialize(InputStream in)78throws IOException79{80if (in==null)81throw new NullPointerException("in");8283ObjectInputStream objIn = new ObjectInputStream(in);8485Object obj;8687try88{89obj = objIn.readObject();90}91catch (ClassNotFoundException ex)92{93throw new InvalidClassException(ex.toString());94}9596return obj;97}9899/**100* Deserializes an array from a given <code>InputStream</code>.101*102* @param in The <code>InputStream</code> to103* deserialize an object from.104*105* @param elemType The class denoting the type of the array106* elements.107* @param length The expected length of the array, or -1 if108* any length is expected.109*/110public static Object deserializeArray(InputStream in, Class elemType, int length)111throws IOException112{113if (elemType==null)114throw new NullPointerException("elemType");115116if (length<-1)117throw new IllegalArgumentException("length");118119Object obj = deserialize(in);120121Class cls = obj.getClass();122123124if (!cls.isArray())125throw new InvalidObjectException("object is not an array");126127Class arrayElemType = cls.getComponentType();128if (arrayElemType!=elemType)129throw new InvalidObjectException("unexpected array component type");130131if (length != -1)132{133int arrayLength = Array.getLength(obj);134if (arrayLength!=length)135throw new InvalidObjectException("array length mismatch");136}137138return obj;139}140141public static Object deserializeArrayResource(String name, Class elemType, int length)142throws IOException143{144InputStream str = getResourceAsStream(name);145if (str==null)146throw new IOException("unable to load resource '"+name+"'");147148Object obj = deserializeArray(str, elemType, length);149150return obj;151}152153public static void serialize(OutputStream out, Object obj)154throws IOException155{156if (out==null)157throw new NullPointerException("out");158159if (obj==null)160throw new NullPointerException("obj");161162ObjectOutputStream objOut = new ObjectOutputStream(out);163objOut.writeObject(obj);164165}166167/**168* Sets the system-wide JavaLayer hook.169*/170public static synchronized void setHook(JavaLayerHook hook0)171{172hook = hook0;173}174175public static synchronized JavaLayerHook getHook()176{177return hook;178}179180/**181* Retrieves an InputStream for a named resource.182*183* @param name The name of the resource. This must be a simple184* name, and not a qualified package name.185*186* @return The InputStream for the named resource, or null if187* the resource has not been found. If a hook has been188* provided, its getResourceAsStream() method is called189* to retrieve the resource.190*/191public static synchronized InputStream getResourceAsStream(String name)192{193InputStream is = null;194195if (hook!=null)196{197is = hook.getResourceAsStream(name);198}199else200{201Class cls = JavaLayerUtils.class;202is = cls.getResourceAsStream(name);203}204205return is;206}207}208209210