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 <a href="http://mina.apache.org">Apache MINA Project</a> 36 */ 37 public class ObjectSerializationEncoder extends ProtocolEncoderAdapter { 38 private int maxObjectSize = Integer.MAX_VALUE; // 2GB 39 40 /** 41 * Creates a new instance. 42 */ 43 public ObjectSerializationEncoder() { 44 // Do nothing 45 } 46 47 /** 48 * @return 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 * @param maxObjectSize the maximum size for an encoded object 64 */ 65 public void setMaxObjectSize(int maxObjectSize) { 66 if (maxObjectSize <= 0) { 67 throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize); 68 } 69 70 this.maxObjectSize = maxObjectSize; 71 } 72 73 /** 74 * {@inheritDoc} 75 */ 76 @Override 77 public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { 78 if (!(message instanceof Serializable)) { 79 throw new NotSerializableException(); 80 } 81 82 IoBuffer buf = IoBuffer.allocate(64); 83 buf.setAutoExpand(true); 84 buf.putObject(message); 85 86 int objectSize = buf.position() - 4; 87 if (objectSize > maxObjectSize) { 88 throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize 89 + ')'); 90 } 91 92 buf.flip(); 93 out.write(buf); 94 } 95 }