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