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.DataOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.ObjectOutput;
26  import java.io.OutputStream;
27  
28  import org.apache.mina.core.buffer.IoBuffer;
29  
30  /**
31   * An {@link ObjectOutput} and {@link OutputStream} that can write the objects as
32   * the serialized form that {@link ObjectSerializationDecoder} can decode.
33   *
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class ObjectSerializationOutputStream extends OutputStream implements ObjectOutput {
37  
38      private final DataOutputStream out;
39  
40      private int maxObjectSize = Integer.MAX_VALUE;
41  
42      /**
43       * Create a new instance of an ObjectSerializationOutputStream
44       * @param out The {@link OutputStream} to use
45       */
46      public ObjectSerializationOutputStream(OutputStream out) {
47          if (out == null) {
48              throw new IllegalArgumentException("out");
49          }
50  
51          if (out instanceof DataOutputStream) {
52              this.out = (DataOutputStream) out;
53          } else {
54              this.out = new DataOutputStream(out);
55          }
56      }
57  
58      /**
59       * @return the allowed maximum size of the encoded object.
60       * If the size of the encoded object exceeds this value, this encoder
61       * will throw a {@link IllegalArgumentException}.  The default value
62       * is {@link Integer#MAX_VALUE}.
63       */
64      public int getMaxObjectSize() {
65          return maxObjectSize;
66      }
67  
68      /**
69       * Sets the allowed maximum size of the encoded object.
70       * If the size of the encoded object exceeds this value, this encoder
71       * will throw a {@link IllegalArgumentException}.  The default value
72       * is {@link Integer#MAX_VALUE}.
73       * 
74       * @param maxObjectSize The maximum object size
75       */
76      public void setMaxObjectSize(int maxObjectSize) {
77          if (maxObjectSize <= 0) {
78              throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize);
79          }
80  
81          this.maxObjectSize = maxObjectSize;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void close() throws IOException {
89          out.close();
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public void flush() throws IOException {
97          out.flush();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public void write(int b) throws IOException {
105         out.write(b);
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public void write(byte[] b) throws IOException {
113         out.write(b);
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public void write(byte[] b, int off, int len) throws IOException {
121         out.write(b, off, len);
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     public void writeObject(Object obj) throws IOException {
128         IoBuffer buf = IoBuffer.allocate(64, false);
129         buf.setAutoExpand(true);
130         buf.putObject(obj);
131 
132         int objectSize = buf.position() - 4;
133         if (objectSize > maxObjectSize) {
134             throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
135                     + ')');
136         }
137 
138         out.write(buf.array(), 0, buf.position());
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     public void writeBoolean(boolean v) throws IOException {
145         out.writeBoolean(v);
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     public void writeByte(int v) throws IOException {
152         out.writeByte(v);
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public void writeBytes(String s) throws IOException {
159         out.writeBytes(s);
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     public void writeChar(int v) throws IOException {
166         out.writeChar(v);
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     public void writeChars(String s) throws IOException {
173         out.writeChars(s);
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     public void writeDouble(double v) throws IOException {
180         out.writeDouble(v);
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     public void writeFloat(float v) throws IOException {
187         out.writeFloat(v);
188     }
189 
190     /**
191      * {@inheritDoc}
192      */
193     public void writeInt(int v) throws IOException {
194         out.writeInt(v);
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     public void writeLong(long v) throws IOException {
201         out.writeLong(v);
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     public void writeShort(int v) throws IOException {
208         out.writeShort(v);
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     public void writeUTF(String str) throws IOException {
215         out.writeUTF(str);
216     }
217 }