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     public void writeObject(Object obj) throws IOException {
127         IoBuffer buf = IoBuffer.allocate(64, false);
128         buf.setAutoExpand(true);
129         buf.putObject(obj);
130 
131         int objectSize = buf.position() - 4;
132         if (objectSize > maxObjectSize) {
133             throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
134                     + ')');
135         }
136 
137         out.write(buf.array(), 0, buf.position());
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     public void writeBoolean(boolean v) throws IOException {
144         out.writeBoolean(v);
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     public void writeByte(int v) throws IOException {
151         out.writeByte(v);
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     public void writeBytes(String s) throws IOException {
158         out.writeBytes(s);
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     public void writeChar(int v) throws IOException {
165         out.writeChar(v);
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public void writeChars(String s) throws IOException {
172         out.writeChars(s);
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     public void writeDouble(double v) throws IOException {
179         out.writeDouble(v);
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     public void writeFloat(float v) throws IOException {
186         out.writeFloat(v);
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     public void writeInt(int v) throws IOException {
193         out.writeInt(v);
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     public void writeLong(long v) throws IOException {
200         out.writeLong(v);
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     public void writeShort(int v) throws IOException {
207         out.writeShort(v);
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     public void writeUTF(String str) throws IOException {
214         out.writeUTF(str);
215     }
216 }