View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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 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   * An {@link ObjectOutput} which is interoperable with {@link ObjectDecoder}
    30   * and {@link ObjectDecoderInputStream}.
    31   *
    32   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    34   *
    35   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    36   *
    37   */
    38  public class ObjectEncoderOutputStream extends OutputStream implements
    39          ObjectOutput {
    40
    41      private final DataOutputStream out;
    42      private final int estimatedLength;
    43
    44      /**
    45       * Creates a new {@link ObjectOutput} with the estimated length of 512
    46       * bytes.
    47       *
    48       * @param out
    49       *        the {@link OutputStream} where the serialized form will be
    50       *        written out
    51       */
    52      public ObjectEncoderOutputStream(OutputStream out) {
    53          this(out, 512);
    54      }
    55
    56      /**
    57       * Creates a new {@link ObjectOutput}.
    58       *
    59       * @param out
    60       *        the {@link OutputStream} where the serialized form will be
    61       *        written out
    62       *
    63       * @param estimatedLength
    64       *        the estimated byte length of the serialized form of an object.
    65       *        If the length of the serialized form exceeds this value, the
    66       *        internal buffer will be expanded automatically at the cost of
    67       *        memory bandwidth.  If this value is too big, it will also waste
    68       *        memory bandwidth.  To avoid unnecessary memory copy or allocation
    69       *        cost, please specify the properly estimated value.
    70       */
    71      public ObjectEncoderOutputStream(OutputStream out, int estimatedLength) {
    72          if (out == null) {
    73              throw new NullPointerException("out");
    74          }
    75          if (estimatedLength < 0) {
    76              throw new IllegalArgumentException("estimatedLength: " + estimatedLength);
    77          }
    78
    79          if (out instanceof DataOutputStream) {
    80              this.out = (DataOutputStream) out;
    81          } else {
    82              this.out = new DataOutputStream(out);
    83          }
    84          this.estimatedLength = estimatedLength;
    85      }
    86
    87      public void writeObject(Object obj) throws IOException {
    88          ChannelBufferOutputStream bout = new ChannelBufferOutputStream(
    89                  ChannelBuffers.dynamicBuffer(estimatedLength));
    90          ObjectOutputStream oout = new CompactObjectOutputStream(bout);
    91          oout.writeObject(obj);
    92          oout.flush();
    93          oout.close();
    94
    95          ChannelBuffer buffer = bout.buffer();
    96          int objectSize = buffer.readableBytes();
    97          writeInt(objectSize);
    98          buffer.getBytes(0, this, objectSize);
    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     public final void writeBoolean(boolean v) throws IOException {
    131         out.writeBoolean(v);
    132     }
    133
    134     public final void writeByte(int v) throws IOException {
    135         out.writeByte(v);
    136     }
    137
    138     public final void writeBytes(String s) throws IOException {
    139         out.writeBytes(s);
    140     }
    141
    142     public final void writeChar(int v) throws IOException {
    143         out.writeChar(v);
    144     }
    145
    146     public final void writeChars(String s) throws IOException {
    147         out.writeChars(s);
    148     }
    149
    150     public final void writeDouble(double v) throws IOException {
    151         out.writeDouble(v);
    152     }
    153
    154     public final void writeFloat(float v) throws IOException {
    155         out.writeFloat(v);
    156     }
    157
    158     public final void writeInt(int v) throws IOException {
    159         out.writeInt(v);
    160     }
    161
    162     public final void writeLong(long v) throws IOException {
    163         out.writeLong(v);
    164     }
    165
    166     public final void writeShort(int v) throws IOException {
    167         out.writeShort(v);
    168     }
    169
    170     public final void writeUTF(String str) throws IOException {
    171         out.writeUTF(str);
    172     }
    173 }