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
39 public class ObjectSerializationInputStream extends InputStream implements
40 ObjectInput {
41
42 private final DataInputStream in;
43
44 private final ClassLoader classLoader;
45
46 private int maxObjectSize = 1048576;
47
48 public ObjectSerializationInputStream(InputStream in) {
49 this(in, null);
50 }
51
52 public ObjectSerializationInputStream(InputStream in,
53 ClassLoader classLoader) {
54 if (in == null) {
55 throw new NullPointerException("in");
56 }
57 if (classLoader == null) {
58 classLoader = Thread.currentThread().getContextClassLoader();
59 }
60
61 if (in instanceof DataInputStream) {
62 this.in = (DataInputStream) in;
63 } else {
64 this.in = new DataInputStream(in);
65 }
66
67 this.classLoader = classLoader;
68 }
69
70
71
72
73
74
75
76 public int getMaxObjectSize() {
77 return maxObjectSize;
78 }
79
80
81
82
83
84
85
86 public void setMaxObjectSize(int maxObjectSize) {
87 if (maxObjectSize <= 0) {
88 throw new IllegalArgumentException("maxObjectSize: "
89 + maxObjectSize);
90 }
91
92 this.maxObjectSize = maxObjectSize;
93 }
94
95 @Override
96 public int read() throws IOException {
97 return in.read();
98 }
99
100 public Object readObject() throws ClassNotFoundException, IOException {
101 int objectSize = in.readInt();
102 if (objectSize <= 0) {
103 throw new StreamCorruptedException("Invalid objectSize: "
104 + objectSize);
105 }
106 if (objectSize > maxObjectSize) {
107 throw new StreamCorruptedException("ObjectSize too big: "
108 + objectSize + " (expected: <= " + maxObjectSize + ')');
109 }
110
111 IoBuffer buf = IoBuffer.allocate(objectSize + 4, false);
112 buf.putInt(objectSize);
113 in.readFully(buf.array(), 4, objectSize);
114 buf.position(0);
115 buf.limit(objectSize + 4);
116
117 return buf.getObject(classLoader);
118 }
119
120 public boolean readBoolean() throws IOException {
121 return in.readBoolean();
122 }
123
124 public byte readByte() throws IOException {
125 return in.readByte();
126 }
127
128 public char readChar() throws IOException {
129 return in.readChar();
130 }
131
132 public double readDouble() throws IOException {
133 return in.readDouble();
134 }
135
136 public float readFloat() throws IOException {
137 return in.readFloat();
138 }
139
140 public void readFully(byte[] b) throws IOException {
141 in.readFully(b);
142 }
143
144 public void readFully(byte[] b, int off, int len) throws IOException {
145 in.readFully(b, off, len);
146 }
147
148 public int readInt() throws IOException {
149 return in.readInt();
150 }
151
152
153
154
155
156 @Deprecated
157 public String readLine() throws IOException {
158 return in.readLine();
159 }
160
161 public long readLong() throws IOException {
162 return in.readLong();
163 }
164
165 public short readShort() throws IOException {
166 return in.readShort();
167 }
168
169 public String readUTF() throws IOException {
170 return in.readUTF();
171 }
172
173 public int readUnsignedByte() throws IOException {
174 return in.readUnsignedByte();
175 }
176
177 public int readUnsignedShort() throws IOException {
178 return in.readUnsignedShort();
179 }
180
181 public int skipBytes(int n) throws IOException {
182 return in.skipBytes(n);
183 }
184 }