1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.serialization;
17
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectOutput;
21 import java.io.ObjectOutputStream;
22 import java.io.OutputStream;
23
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.jboss.netty.buffer.ChannelBufferOutputStream;
26 import org.jboss.netty.buffer.ChannelBuffers;
27
28
29
30
31
32
33
34
35
36
37
38 public class ObjectEncoderOutputStream extends OutputStream implements
39 ObjectOutput {
40
41 private final DataOutputStream out;
42 private final int estimatedLength;
43
44
45
46
47
48
49
50
51
52 public ObjectEncoderOutputStream(OutputStream out) {
53 this(out, 512);
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public ObjectEncoderOutputStream(OutputStream out, int estimatedLength) {
72 if (out == null) {
73 throw new NullPointerException("out");
74 }
75 if (estimatedLength < 0) {
76 throw new IllegalArgumentException("estimatedLength: " + estimatedLength);
77 }
78
79 if (out instanceof DataOutputStream) {
80 this.out = (DataOutputStream) out;
81 } else {
82 this.out = new DataOutputStream(out);
83 }
84 this.estimatedLength = estimatedLength;
85 }
86
87 public void writeObject(Object obj) throws IOException {
88 ChannelBufferOutputStream bout = new ChannelBufferOutputStream(
89 ChannelBuffers.dynamicBuffer(estimatedLength));
90 ObjectOutputStream oout = new CompactObjectOutputStream(bout);
91 oout.writeObject(obj);
92 oout.flush();
93 oout.close();
94
95 ChannelBuffer buffer = bout.buffer();
96 int objectSize = buffer.readableBytes();
97 writeInt(objectSize);
98 buffer.getBytes(0, this, objectSize);
99 }
100
101 @Override
102 public void write(int b) throws IOException {
103 out.write(b);
104 }
105
106 @Override
107 public void close() throws IOException {
108 out.close();
109 }
110
111 @Override
112 public void flush() throws IOException {
113 out.flush();
114 }
115
116 public final int size() {
117 return out.size();
118 }
119
120 @Override
121 public void write(byte[] b, int off, int len) throws IOException {
122 out.write(b, off, len);
123 }
124
125 @Override
126 public void write(byte[] b) throws IOException {
127 out.write(b);
128 }
129
130 public final void writeBoolean(boolean v) throws IOException {
131 out.writeBoolean(v);
132 }
133
134 public final void writeByte(int v) throws IOException {
135 out.writeByte(v);
136 }
137
138 public final void writeBytes(String s) throws IOException {
139 out.writeBytes(s);
140 }
141
142 public final void writeChar(int v) throws IOException {
143 out.writeChar(v);
144 }
145
146 public final void writeChars(String s) throws IOException {
147 out.writeChars(s);
148 }
149
150 public final void writeDouble(double v) throws IOException {
151 out.writeDouble(v);
152 }
153
154 public final void writeFloat(float v) throws IOException {
155 out.writeFloat(v);
156 }
157
158 public final void writeInt(int v) throws IOException {
159 out.writeInt(v);
160 }
161
162 public final void writeLong(long v) throws IOException {
163 out.writeLong(v);
164 }
165
166 public final void writeShort(int v) throws IOException {
167 out.writeShort(v);
168 }
169
170 public final void writeUTF(String str) throws IOException {
171 out.writeUTF(str);
172 }
173 }