View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.filter.codec.serialization;
    21
    22  import java.io.DataInput;
    23  import java.io.DataInputStream;
    24  import java.io.IOException;
    25  import java.io.InputStream;
    26  import java.io.ObjectInput;
    27  import java.io.StreamCorruptedException;
    28
    29  import org.apache.mina.common.BufferDataException;
    30  import org.apache.mina.common.ByteBuffer;
    31
    32  /**
    33   * An {@link ObjectInput} and {@link InputStream} that can read the objects encoded
    34   * by {@link ObjectSerializationEncoder}.
    35   *
    36   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    37   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
    38   */
    39  public class ObjectSerializationInputStream extends InputStream implements
    40          ObjectInput {
    41
    42      private final DataInputStream in;
    43
    44      private final ClassLoader classLoader;
    45
    46      private int maxObjectSize = 1048576;
    47
    48      public ObjectSerializationInputStream(InputStream in) {
    49          this(in, null);
    50      }
    51
    52      public ObjectSerializationInputStream(InputStream in,
    53              ClassLoader classLoader) {
    54          if (in == null) {
    55              throw new NullPointerException("in");
    56          }
    57          if (classLoader == null) {
    58              classLoader = Thread.currentThread().getContextClassLoader();
    59          }
    60
    61          if (in instanceof DataInputStream) {
    62              this.in = (DataInputStream) in;
    63          } else {
    64              this.in = new DataInputStream(in);
    65          }
    66
    67          this.classLoader = classLoader;
    68      }
    69
    70      /**
    71       * Returns the allowed maximum size of the object to be decoded.
    72       * If the size of the object to be decoded exceeds this value, this
    73       * decoder will throw a {@link BufferDataException}.  The default
    74       * value is <tt>1048576</tt> (1MB).
    75       */
    76      public int getMaxObjectSize() {
    77          return maxObjectSize;
    78      }
    79
    80      /**
    81       * Sets the allowed maximum size of the object to be decoded.
    82       * If the size of the object to be decoded exceeds this value, this
    83       * decoder will throw a {@link BufferDataException}.  The default
    84       * value is <tt>1048576</tt> (1MB).
    85       */
    86      public void setMaxObjectSize(int maxObjectSize) {
    87          if (maxObjectSize <= 0) {
    88              throw new IllegalArgumentException("maxObjectSize: "
    89                      + maxObjectSize);
    90          }
    91
    92          this.maxObjectSize = maxObjectSize;
    93      }
    94
    95      public int read() throws IOException {
    96          return in.read();
    97      }
    98
    99      public Object readObject() throws ClassNotFoundException, IOException {
    100         int objectSize = in.readInt();
    101         if (objectSize <= 0) {
    102             throw new StreamCorruptedException("Invalid objectSize: "
    103                     + objectSize);
    104         }
    105         if (objectSize > maxObjectSize) {
    106             throw new StreamCorruptedException("ObjectSize too big: "
    107                     + objectSize + " (expected: <= " + maxObjectSize + ')');
    108         }
    109
    110         ByteBuffer buf = ByteBuffer.allocate(objectSize + 4, false);
    111         buf.putInt(objectSize);
    112         in.readFully(buf.array(), 4, objectSize);
    113         buf.position(0);
    114         buf.limit(objectSize + 4);
    115
    116         Object answer = buf.getObject(classLoader);
    117         buf.release();
    118         return answer;
    119     }
    120
    121     public boolean readBoolean() throws IOException {
    122         return in.readBoolean();
    123     }
    124
    125     public byte readByte() throws IOException {
    126         return in.readByte();
    127     }
    128
    129     public char readChar() throws IOException {
    130         return in.readChar();
    131     }
    132
    133     public double readDouble() throws IOException {
    134         return in.readDouble();
    135     }
    136
    137     public float readFloat() throws IOException {
    138         return in.readFloat();
    139     }
    140
    141     public void readFully(byte[] b) throws IOException {
    142         in.readFully(b);
    143     }
    144
    145     public void readFully(byte[] b, int off, int len) throws IOException {
    146         in.readFully(b, off, len);
    147     }
    148
    149     public int readInt() throws IOException {
    150         return in.readInt();
    151     }
    152
    153     /**
    154      * @see DataInput#readLine()
    155      * @deprecated
    156      */
    157     public String readLine() throws IOException {
    158         return in.readLine();
    159     }
    160
    161     public long readLong() throws IOException {
    162         return in.readLong();
    163     }
    164
    165     public short readShort() throws IOException {
    166         return in.readShort();
    167     }
    168
    169     public String readUTF() throws IOException {
    170         return in.readUTF();
    171     }
    172
    173     public int readUnsignedByte() throws IOException {
    174         return in.readUnsignedByte();
    175     }
    176
    177     public int readUnsignedShort() throws IOException {
    178         return in.readUnsignedShort();
    179     }
    180
    181     public int skipBytes(int n) throws IOException {
    182         return in.skipBytes(n);
    183     }
    184 }