1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.serialization;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufOutputStream;
20 import io.netty.buffer.Unpooled;
21
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectOutput;
25 import java.io.ObjectOutputStream;
26 import java.io.OutputStream;
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 @Override
82 public void writeObject(Object obj) throws IOException {
83 ByteBuf buf = Unpooled.buffer(estimatedLength);
84 try {
85 ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
86 try {
87 oout.writeObject(obj);
88 oout.flush();
89 } finally {
90 oout.close();
91 }
92
93 int objectSize = buf.readableBytes();
94 writeInt(objectSize);
95 buf.getBytes(0, this, objectSize);
96 } finally {
97 buf.release();
98 }
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 @Override
131 public final void writeBoolean(boolean v) throws IOException {
132 out.writeBoolean(v);
133 }
134
135 @Override
136 public final void writeByte(int v) throws IOException {
137 out.writeByte(v);
138 }
139
140 @Override
141 public final void writeBytes(String s) throws IOException {
142 out.writeBytes(s);
143 }
144
145 @Override
146 public final void writeChar(int v) throws IOException {
147 out.writeChar(v);
148 }
149
150 @Override
151 public final void writeChars(String s) throws IOException {
152 out.writeChars(s);
153 }
154
155 @Override
156 public final void writeDouble(double v) throws IOException {
157 out.writeDouble(v);
158 }
159
160 @Override
161 public final void writeFloat(float v) throws IOException {
162 out.writeFloat(v);
163 }
164
165 @Override
166 public final void writeInt(int v) throws IOException {
167 out.writeInt(v);
168 }
169
170 @Override
171 public final void writeLong(long v) throws IOException {
172 out.writeLong(v);
173 }
174
175 @Override
176 public final void writeShort(int v) throws IOException {
177 out.writeShort(v);
178 }
179
180 @Override
181 public final void writeUTF(String str) throws IOException {
182 out.writeUTF(str);
183 }
184 }