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.DataInput;
23  import java.io.DataInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.ObjectInput;
27  import java.io.StreamCorruptedException;
28  
29  import org.apache.mina.core.buffer.BufferDataException;
30  import org.apache.mina.core.buffer.IoBuffer;
31  
32  /**
33   * An {@link ObjectInput} and {@link InputStream} that can read the objects encoded
34   * by {@link ObjectSerializationEncoder}.
35   *
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class ObjectSerializationInputStream extends InputStream implements
39          ObjectInput {
40  
41      private final DataInputStream in;
42  
43      private final ClassLoader classLoader;
44  
45      private int maxObjectSize = 1048576;
46  
47      public ObjectSerializationInputStream(InputStream in) {
48          this(in, null);
49      }
50  
51      public ObjectSerializationInputStream(InputStream in,
52              ClassLoader classLoader) {
53          if (in == null) {
54              throw new NullPointerException("in");
55          }
56          if (classLoader == null) {
57              classLoader = Thread.currentThread().getContextClassLoader();
58          }
59  
60          if (in instanceof DataInputStream) {
61              this.in = (DataInputStream) in;
62          } else {
63              this.in = new DataInputStream(in);
64          }
65  
66          this.classLoader = classLoader;
67      }
68  
69      /**
70       * Returns the allowed maximum size of the object to be decoded.
71       * If the size of the object to be decoded exceeds this value, this
72       * decoder will throw a {@link BufferDataException}.  The default
73       * value is <tt>1048576</tt> (1MB).
74       */
75      public int getMaxObjectSize() {
76          return maxObjectSize;
77      }
78  
79      /**
80       * Sets the allowed maximum size of the object to be decoded.
81       * If the size of the object to be decoded exceeds this value, this
82       * decoder will throw a {@link BufferDataException}.  The default
83       * value is <tt>1048576</tt> (1MB).
84       */
85      public void setMaxObjectSize(int maxObjectSize) {
86          if (maxObjectSize <= 0) {
87              throw new IllegalArgumentException("maxObjectSize: "
88                      + maxObjectSize);
89          }
90  
91          this.maxObjectSize = maxObjectSize;
92      }
93  
94      @Override
95      public int read() throws IOException {
96          return in.read();
97      }
98  
99      public Object readObject() throws ClassNotFoundException, IOException {
100         int objectSize = in.readInt();
101         if (objectSize <= 0) {
102             throw new StreamCorruptedException("Invalid objectSize: "
103                     + objectSize);
104         }
105         if (objectSize > maxObjectSize) {
106             throw new StreamCorruptedException("ObjectSize too big: "
107                     + objectSize + " (expected: <= " + maxObjectSize + ')');
108         }
109 
110         IoBuffer buf = IoBuffer.allocate(objectSize + 4, false);
111         buf.putInt(objectSize);
112         in.readFully(buf.array(), 4, objectSize);
113         buf.position(0);
114         buf.limit(objectSize + 4);
115 
116         return buf.getObject(classLoader);
117     }
118 
119     public boolean readBoolean() throws IOException {
120         return in.readBoolean();
121     }
122 
123     public byte readByte() throws IOException {
124         return in.readByte();
125     }
126 
127     public char readChar() throws IOException {
128         return in.readChar();
129     }
130 
131     public double readDouble() throws IOException {
132         return in.readDouble();
133     }
134 
135     public float readFloat() throws IOException {
136         return in.readFloat();
137     }
138 
139     public void readFully(byte[] b) throws IOException {
140         in.readFully(b);
141     }
142 
143     public void readFully(byte[] b, int off, int len) throws IOException {
144         in.readFully(b, off, len);
145     }
146 
147     public int readInt() throws IOException {
148         return in.readInt();
149     }
150 
151     /**
152      * @see DataInput#readLine()
153      * @deprecated
154      */
155     @Deprecated
156     public String readLine() throws IOException {
157         return in.readLine();
158     }
159 
160     public long readLong() throws IOException {
161         return in.readLong();
162     }
163 
164     public short readShort() throws IOException {
165         return in.readShort();
166     }
167 
168     public String readUTF() throws IOException {
169         return in.readUTF();
170     }
171 
172     public int readUnsignedByte() throws IOException {
173         return in.readUnsignedByte();
174     }
175 
176     public int readUnsignedShort() throws IOException {
177         return in.readUnsignedShort();
178     }
179 
180     public int skipBytes(int n) throws IOException {
181         return in.skipBytes(n);
182     }
183 }