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