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.NotSerializableException;
    23  import java.io.Serializable;
    24
    25  import org.apache.mina.common.ByteBuffer;
    26  import org.apache.mina.common.IoSession;
    27  import org.apache.mina.filter.codec.ProtocolEncoder;
    28  import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
    29  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
    30
    31  /**
    32   * A {@link ProtocolEncoder} which serializes {@link Serializable} Java objects
    33   * using {@link ByteBuffer#putObject(Object)}.
    34   *
    35   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    36   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    37   */
    38  public class ObjectSerializationEncoder extends ProtocolEncoderAdapter {
    39      private int maxObjectSize = Integer.MAX_VALUE; // 2GB
    40
    41      /**
    42       * Creates a new instance.
    43       */
    44      public ObjectSerializationEncoder() {
    45      }
    46
    47      /**
    48       * Returns the allowed maximum size of the encoded object.
    49       * If the size of the encoded object exceeds this value, this encoder
    50       * will throw a {@link IllegalArgumentException}.  The default value
    51       * is {@link Integer#MAX_VALUE}.
    52       */
    53      public int getMaxObjectSize() {
    54          return maxObjectSize;
    55      }
    56
    57      /**
    58       * Sets the allowed maximum size of the encoded object.
    59       * If the size of the encoded object exceeds this value, this encoder
    60       * will throw a {@link IllegalArgumentException}.  The default value
    61       * is {@link Integer#MAX_VALUE}.
    62       */
    63      public void setMaxObjectSize(int maxObjectSize) {
    64          if (maxObjectSize <= 0) {
    65              throw new IllegalArgumentException("maxObjectSize: "
    66                      + maxObjectSize);
    67          }
    68
    69          this.maxObjectSize = maxObjectSize;
    70      }
    71
    72      public void encode(IoSession session, Object message,
    73              ProtocolEncoderOutput out) throws Exception {
    74          if (!(message instanceof Serializable)) {
    75              throw new NotSerializableException();
    76          }
    77
    78          ByteBuffer buf = ByteBuffer.allocate(64);
    79          buf.setAutoExpand(true);
    80          buf.putObject(message);
    81
    82          int objectSize = buf.position() - 4;
    83          if (objectSize > maxObjectSize) {
    84              buf.release();
    85              throw new IllegalArgumentException(
    86                      "The encoded object is too big: " + objectSize + " (> "
    87                              + maxObjectSize + ')');
    88          }
    89
    90          buf.flip();
    91          out.write(buf);
    92      }
    93  }