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