Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/JavaLayerUtils.java
8650 views
1
/*
2
* 11/19/04 1.0 moved to LGPL.
3
* 12/12/99 Initial version. [email protected]
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
21
package javazoom.jl.decoder;
22
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.InvalidClassException;
26
import java.io.InvalidObjectException;
27
import java.io.ObjectInputStream;
28
import java.io.ObjectOutputStream;
29
import java.io.OutputStream;
30
import 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
*/
38
public class JavaLayerUtils
39
{
40
private static JavaLayerHook hook = null;
41
42
/**
43
* Deserializes the object contained in the given input stream.
44
*
45
* @param in The input stream to deserialize an object from.
46
* @param cls The expected class of the deserialized object.
47
*/
48
public static Object deserialize(InputStream in, Class cls)
49
throws IOException
50
{
51
if (cls==null)
52
throw new NullPointerException("cls");
53
54
Object obj = deserialize(in, cls);
55
if (!cls.isInstance(obj))
56
{
57
throw new InvalidObjectException("type of deserialized instance not of required class.");
58
}
59
60
return obj;
61
}
62
63
/**
64
* Deserializes an object from the given <code>InputStream</code>.
65
* The deserialization is delegated to an <code>
66
* ObjectInputStream</code> instance.
67
*
68
* @param in The <code>InputStream</code> to deserialize an object
69
* from.
70
*
71
* @return The object deserialized from the stream.
72
* @exception IOException is thrown if there was a problem reading
73
* the underlying stream, or an object could not be deserialized
74
* from the stream.
75
*
76
* @see java.io.ObjectInputStream
77
*/
78
public static Object deserialize(InputStream in)
79
throws IOException
80
{
81
if (in==null)
82
throw new NullPointerException("in");
83
84
ObjectInputStream objIn = new ObjectInputStream(in);
85
86
Object obj;
87
88
try
89
{
90
obj = objIn.readObject();
91
}
92
catch (ClassNotFoundException ex)
93
{
94
throw new InvalidClassException(ex.toString());
95
}
96
97
return obj;
98
}
99
100
/**
101
* Deserializes an array from a given <code>InputStream</code>.
102
*
103
* @param in The <code>InputStream</code> to
104
* deserialize an object from.
105
*
106
* @param elemType The class denoting the type of the array
107
* elements.
108
* @param length The expected length of the array, or -1 if
109
* any length is expected.
110
*/
111
public static Object deserializeArray(InputStream in, Class elemType, int length)
112
throws IOException
113
{
114
if (elemType==null)
115
throw new NullPointerException("elemType");
116
117
if (length<-1)
118
throw new IllegalArgumentException("length");
119
120
Object obj = deserialize(in);
121
122
Class cls = obj.getClass();
123
124
125
if (!cls.isArray())
126
throw new InvalidObjectException("object is not an array");
127
128
Class arrayElemType = cls.getComponentType();
129
if (arrayElemType!=elemType)
130
throw new InvalidObjectException("unexpected array component type");
131
132
if (length != -1)
133
{
134
int arrayLength = Array.getLength(obj);
135
if (arrayLength!=length)
136
throw new InvalidObjectException("array length mismatch");
137
}
138
139
return obj;
140
}
141
142
public static Object deserializeArrayResource(String name, Class elemType, int length)
143
throws IOException
144
{
145
InputStream str = getResourceAsStream(name);
146
if (str==null)
147
throw new IOException("unable to load resource '"+name+"'");
148
149
Object obj = deserializeArray(str, elemType, length);
150
151
return obj;
152
}
153
154
public static void serialize(OutputStream out, Object obj)
155
throws IOException
156
{
157
if (out==null)
158
throw new NullPointerException("out");
159
160
if (obj==null)
161
throw new NullPointerException("obj");
162
163
ObjectOutputStream objOut = new ObjectOutputStream(out);
164
objOut.writeObject(obj);
165
166
}
167
168
/**
169
* Sets the system-wide JavaLayer hook.
170
*/
171
public static synchronized void setHook(JavaLayerHook hook0)
172
{
173
hook = hook0;
174
}
175
176
public static synchronized JavaLayerHook getHook()
177
{
178
return hook;
179
}
180
181
/**
182
* Retrieves an InputStream for a named resource.
183
*
184
* @param name The name of the resource. This must be a simple
185
* name, and not a qualified package name.
186
*
187
* @return The InputStream for the named resource, or null if
188
* the resource has not been found. If a hook has been
189
* provided, its getResourceAsStream() method is called
190
* to retrieve the resource.
191
*/
192
public static synchronized InputStream getResourceAsStream(String name)
193
{
194
InputStream is = null;
195
196
if (hook!=null)
197
{
198
is = hook.getResourceAsStream(name);
199
}
200
else
201
{
202
Class cls = JavaLayerUtils.class;
203
is = cls.getResourceAsStream(name);
204
}
205
206
return is;
207
}
208
}
209
210