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