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.DataOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectOutput;
25 import java.io.OutputStream;
26
27 import org.apache.mina.core.buffer.IoBuffer;
28
29
30
31
32
33
34
35 public class ObjectSerializationOutputStream extends OutputStream implements ObjectOutput {
36
37 private final DataOutputStream out;
38
39 private int maxObjectSize = Integer.MAX_VALUE;
40
41
42
43
44
45 public ObjectSerializationOutputStream(OutputStream out) {
46 if (out == null) {
47 throw new IllegalArgumentException("out");
48 }
49
50 if (out instanceof DataOutputStream) {
51 this.out = (DataOutputStream) out;
52 } else {
53 this.out = new DataOutputStream(out);
54 }
55 }
56
57
58
59
60
61
62
63 public int getMaxObjectSize() {
64 return maxObjectSize;
65 }
66
67
68
69
70
71
72
73
74
75 public void setMaxObjectSize(int maxObjectSize) {
76 if (maxObjectSize <= 0) {
77 throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize);
78 }
79
80 this.maxObjectSize = maxObjectSize;
81 }
82
83
84
85
86 @Override
87 public void close() throws IOException {
88 out.close();
89 }
90
91
92
93
94 @Override
95 public void flush() throws IOException {
96 out.flush();
97 }
98
99
100
101
102 @Override
103 public void write(int b) throws IOException {
104 out.write(b);
105 }
106
107
108
109
110 @Override
111 public void write(byte[] b) throws IOException {
112 out.write(b);
113 }
114
115
116
117
118 @Override
119 public void write(byte[] b, int off, int len) throws IOException {
120 out.write(b, off, len);
121 }
122
123
124
125
126 @Override
127 public void writeObject(Object obj) throws IOException {
128 IoBuffer buf = IoBuffer.allocate(64, false);
129 buf.setAutoExpand(true);
130 buf.putObject(obj);
131
132 int objectSize = buf.position() - 4;
133 if (objectSize > maxObjectSize) {
134 throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
135 + ')');
136 }
137
138 out.write(buf.array(), 0, buf.position());
139 }
140
141
142
143
144 @Override
145 public void writeBoolean(boolean v) throws IOException {
146 out.writeBoolean(v);
147 }
148
149
150
151
152 @Override
153 public void writeByte(int v) throws IOException {
154 out.writeByte(v);
155 }
156
157
158
159
160 @Override
161 public void writeBytes(String s) throws IOException {
162 out.writeBytes(s);
163 }
164
165
166
167
168 @Override
169 public void writeChar(int v) throws IOException {
170 out.writeChar(v);
171 }
172
173
174
175
176 @Override
177 public void writeChars(String s) throws IOException {
178 out.writeChars(s);
179 }
180
181
182
183
184 @Override
185 public void writeDouble(double v) throws IOException {
186 out.writeDouble(v);
187 }
188
189
190
191
192 @Override
193 public void writeFloat(float v) throws IOException {
194 out.writeFloat(v);
195 }
196
197
198
199
200 @Override
201 public void writeInt(int v) throws IOException {
202 out.writeInt(v);
203 }
204
205
206
207
208 @Override
209 public void writeLong(long v) throws IOException {
210 out.writeLong(v);
211 }
212
213
214
215
216 @Override
217 public void writeShort(int v) throws IOException {
218 out.writeShort(v);
219 }
220
221
222
223
224 @Override
225 public void writeUTF(String str) throws IOException {
226 out.writeUTF(str);
227 }
228 }