1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.codec.serialization;
21
22 import java.io.DataInput;
23 import java.io.DataInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInput;
27 import java.io.StreamCorruptedException;
28
29 import org.apache.mina.core.buffer.BufferDataException;
30 import org.apache.mina.core.buffer.IoBuffer;
31
32
33
34
35
36
37
38 public class ObjectSerializationInputStream extends InputStream implements ObjectInput {
39
40 private final DataInputStream in;
41
42 private final ClassLoader classLoader;
43
44 private int maxObjectSize = 1048576;
45
46
47
48
49
50 public ObjectSerializationInputStream(InputStream in) {
51 this(in, null);
52 }
53
54
55
56
57
58
59 public ObjectSerializationInputStream(InputStream in, ClassLoader classLoader) {
60 if (in == null) {
61 throw new IllegalArgumentException("in");
62 }
63
64 if (classLoader == null) {
65 classLoader = Thread.currentThread().getContextClassLoader();
66 }
67
68 if (in instanceof DataInputStream) {
69 this.in = (DataInputStream) in;
70 } else {
71 this.in = new DataInputStream(in);
72 }
73
74 this.classLoader = classLoader;
75 }
76
77
78
79
80
81
82
83 public int getMaxObjectSize() {
84 return maxObjectSize;
85 }
86
87
88
89
90
91
92
93
94
95 public void setMaxObjectSize(int maxObjectSize) {
96 if (maxObjectSize <= 0) {
97 throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize);
98 }
99
100 this.maxObjectSize = maxObjectSize;
101 }
102
103
104
105
106 @Override
107 public int read() throws IOException {
108 return in.read();
109 }
110
111
112
113
114 public Object readObject() throws ClassNotFoundException, IOException {
115 int objectSize = in.readInt();
116 if (objectSize <= 0) {
117 throw new StreamCorruptedException("Invalid objectSize: " + objectSize);
118 }
119 if (objectSize > maxObjectSize) {
120 throw new StreamCorruptedException("ObjectSize too big: " + objectSize + " (expected: <= " + maxObjectSize
121 + ')');
122 }
123
124 IoBuffer buf = IoBuffer.allocate(objectSize + 4, false);
125 buf.putInt(objectSize);
126 in.readFully(buf.array(), 4, objectSize);
127 buf.position(0);
128 buf.limit(objectSize + 4);
129
130 return buf.getObject(classLoader);
131 }
132
133
134
135
136 public boolean readBoolean() throws IOException {
137 return in.readBoolean();
138 }
139
140
141
142
143 public byte readByte() throws IOException {
144 return in.readByte();
145 }
146
147
148
149
150 public char readChar() throws IOException {
151 return in.readChar();
152 }
153
154
155
156
157 public double readDouble() throws IOException {
158 return in.readDouble();
159 }
160
161
162
163
164 public float readFloat() throws IOException {
165 return in.readFloat();
166 }
167
168
169
170
171 public void readFully(byte[] b) throws IOException {
172 in.readFully(b);
173 }
174
175
176
177
178 public void readFully(byte[] b, int off, int len) throws IOException {
179 in.readFully(b, off, len);
180 }
181
182
183
184
185 public int readInt() throws IOException {
186 return in.readInt();
187 }
188
189
190
191
192
193 @Deprecated
194 public String readLine() throws IOException {
195 return in.readLine();
196 }
197
198
199
200
201 public long readLong() throws IOException {
202 return in.readLong();
203 }
204
205
206
207
208 public short readShort() throws IOException {
209 return in.readShort();
210 }
211
212
213
214
215 public String readUTF() throws IOException {
216 return in.readUTF();
217 }
218
219
220
221
222 public int readUnsignedByte() throws IOException {
223 return in.readUnsignedByte();
224 }
225
226
227
228
229 public int readUnsignedShort() throws IOException {
230 return in.readUnsignedShort();
231 }
232
233
234
235
236 public int skipBytes(int n) throws IOException {
237 return in.skipBytes(n);
238 }
239 }