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