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