View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * An {@link ObjectOutput} which is interoperable with {@link ObjectDecoder}
30   * and {@link ObjectDecoderInputStream}.
31   */
32  public class ObjectEncoderOutputStream extends OutputStream implements
33          ObjectOutput {
34  
35      private final DataOutputStream out;
36      private final int estimatedLength;
37  
38      /**
39       * Creates a new {@link ObjectOutput} with the estimated length of 512
40       * bytes.
41       *
42       * @param out
43       *        the {@link OutputStream} where the serialized form will be
44       *        written out
45       */
46      public ObjectEncoderOutputStream(OutputStream out) {
47          this(out, 512);
48      }
49  
50      /**
51       * Creates a new {@link ObjectOutput}.
52       *
53       * @param out
54       *        the {@link OutputStream} where the serialized form will be
55       *        written out
56       *
57       * @param estimatedLength
58       *        the estimated byte length of the serialized form of an object.
59       *        If the length of the serialized form exceeds this value, the
60       *        internal buffer will be expanded automatically at the cost of
61       *        memory bandwidth.  If this value is too big, it will also waste
62       *        memory bandwidth.  To avoid unnecessary memory copy or allocation
63       *        cost, please specify the properly estimated value.
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          ByteBufOutputStream bout = new ByteBufOutputStream(Unpooled.buffer(estimatedLength));
84          ObjectOutputStream oout = new CompactObjectOutputStream(bout);
85          oout.writeObject(obj);
86          oout.flush();
87          oout.close();
88  
89          ByteBuf 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     @Override
125     public final void writeBoolean(boolean v) throws IOException {
126         out.writeBoolean(v);
127     }
128 
129     @Override
130     public final void writeByte(int v) throws IOException {
131         out.writeByte(v);
132     }
133 
134     @Override
135     public final void writeBytes(String s) throws IOException {
136         out.writeBytes(s);
137     }
138 
139     @Override
140     public final void writeChar(int v) throws IOException {
141         out.writeChar(v);
142     }
143 
144     @Override
145     public final void writeChars(String s) throws IOException {
146         out.writeChars(s);
147     }
148 
149     @Override
150     public final void writeDouble(double v) throws IOException {
151         out.writeDouble(v);
152     }
153 
154     @Override
155     public final void writeFloat(float v) throws IOException {
156         out.writeFloat(v);
157     }
158 
159     @Override
160     public final void writeInt(int v) throws IOException {
161         out.writeInt(v);
162     }
163 
164     @Override
165     public final void writeLong(long v) throws IOException {
166         out.writeLong(v);
167     }
168 
169     @Override
170     public final void writeShort(int v) throws IOException {
171         out.writeShort(v);
172     }
173 
174     @Override
175     public final void writeUTF(String str) throws IOException {
176         out.writeUTF(str);
177     }
178 }