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.core.buffer.IoBuffer; 26 import org.apache.mina.core.session.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 IoBuffer#putObject(Object)}. 34 * 35 * @author The Apache MINA Project (dev@mina.apache.org) 36 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 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 IoBuffer buf = IoBuffer.allocate(64); 79 buf.setAutoExpand(true); 80 buf.putObject(message); 81 82 int objectSize = buf.position() - 4; 83 if (objectSize > maxObjectSize) { 84 throw new IllegalArgumentException( 85 "The encoded object is too big: " + objectSize + " (> " 86 + maxObjectSize + ')'); 87 } 88 89 buf.flip(); 90 out.write(buf); 91 } 92 }