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    *   https://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  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   * An {@link ObjectOutput} which is interoperable with {@link ObjectDecoder}
31   * and {@link ObjectDecoderInputStream}.
32   */
33  public class ObjectEncoderOutputStream extends OutputStream implements
34          ObjectOutput {
35  
36      private final DataOutputStream out;
37      private final int estimatedLength;
38  
39      /**
40       * Creates a new {@link ObjectOutput} with the estimated length of 512
41       * bytes.
42       *
43       * @param out
44       *        the {@link OutputStream} where the serialized form will be
45       *        written out
46       */
47      public ObjectEncoderOutputStream(OutputStream out) {
48          this(out, 512);
49      }
50  
51      /**
52       * Creates a new {@link ObjectOutput}.
53       *
54       * @param out
55       *        the {@link OutputStream} where the serialized form will be
56       *        written out
57       *
58       * @param estimatedLength
59       *        the estimated byte length of the serialized form of an object.
60       *        If the length of the serialized form exceeds this value, the
61       *        internal buffer will be expanded automatically at the cost of
62       *        memory bandwidth.  If this value is too big, it will also waste
63       *        memory bandwidth.  To avoid unnecessary memory copy or allocation
64       *        cost, please specify the properly estimated value.
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              // Suppress a warning about resource leak since oout is closed below
83              ObjectOutputStream oout = new CompactObjectOutputStream(
84                      new ByteBufOutputStream(buf));  // lgtm[java/output-resource-leak]
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 }