001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.codec.serialization;
021:
022: import java.io.DataInput;
023: import java.io.DataInputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.ObjectInput;
027: import java.io.StreamCorruptedException;
028:
029: import org.apache.mina.common.BufferDataException;
030: import org.apache.mina.common.IoBuffer;
031:
032: /**
033: * An {@link ObjectInput} and {@link InputStream} that can read the objects encoded
034: * by {@link ObjectSerializationEncoder}.
035: *
036: * @author The Apache MINA Project (dev@mina.apache.org)
037: * @version $Rev: 581234 $, $Date: 2007-10-02 07:39:48 -0600 (Tue, 02 Oct 2007) $
038: */
039: public class ObjectSerializationInputStream extends InputStream
040: implements ObjectInput {
041:
042: private final DataInputStream in;
043:
044: private final ClassLoader classLoader;
045:
046: private int maxObjectSize = 1048576;
047:
048: public ObjectSerializationInputStream(InputStream in) {
049: this (in, null);
050: }
051:
052: public ObjectSerializationInputStream(InputStream in,
053: ClassLoader classLoader) {
054: if (in == null) {
055: throw new NullPointerException("in");
056: }
057: if (classLoader == null) {
058: classLoader = Thread.currentThread()
059: .getContextClassLoader();
060: }
061:
062: if (in instanceof DataInputStream) {
063: this .in = (DataInputStream) in;
064: } else {
065: this .in = new DataInputStream(in);
066: }
067:
068: this .classLoader = classLoader;
069: }
070:
071: /**
072: * Returns the allowed maximum size of the object to be decoded.
073: * If the size of the object to be decoded exceeds this value, this
074: * decoder will throw a {@link BufferDataException}. The default
075: * value is <tt>1048576</tt> (1MB).
076: */
077: public int getMaxObjectSize() {
078: return maxObjectSize;
079: }
080:
081: /**
082: * Sets the allowed maximum size of the object to be decoded.
083: * If the size of the object to be decoded exceeds this value, this
084: * decoder will throw a {@link BufferDataException}. The default
085: * value is <tt>1048576</tt> (1MB).
086: */
087: public void setMaxObjectSize(int maxObjectSize) {
088: if (maxObjectSize <= 0) {
089: throw new IllegalArgumentException("maxObjectSize: "
090: + maxObjectSize);
091: }
092:
093: this .maxObjectSize = maxObjectSize;
094: }
095:
096: @Override
097: public int read() throws IOException {
098: return in.read();
099: }
100:
101: public Object readObject() throws ClassNotFoundException,
102: IOException {
103: int objectSize = in.readInt();
104: if (objectSize <= 0) {
105: throw new StreamCorruptedException("Invalid objectSize: "
106: + objectSize);
107: }
108: if (objectSize > maxObjectSize) {
109: throw new StreamCorruptedException("ObjectSize too big: "
110: + objectSize + " (expected: <= " + maxObjectSize
111: + ')');
112: }
113:
114: IoBuffer buf = IoBuffer.allocate(objectSize + 4, false);
115: buf.putInt(objectSize);
116: in.readFully(buf.array(), 4, objectSize);
117: buf.position(0);
118: buf.limit(objectSize + 4);
119:
120: return buf.getObject(classLoader);
121: }
122:
123: public boolean readBoolean() throws IOException {
124: return in.readBoolean();
125: }
126:
127: public byte readByte() throws IOException {
128: return in.readByte();
129: }
130:
131: public char readChar() throws IOException {
132: return in.readChar();
133: }
134:
135: public double readDouble() throws IOException {
136: return in.readDouble();
137: }
138:
139: public float readFloat() throws IOException {
140: return in.readFloat();
141: }
142:
143: public void readFully(byte[] b) throws IOException {
144: in.readFully(b);
145: }
146:
147: public void readFully(byte[] b, int off, int len)
148: throws IOException {
149: in.readFully(b, off, len);
150: }
151:
152: public int readInt() throws IOException {
153: return in.readInt();
154: }
155:
156: /**
157: * @see DataInput#readLine()
158: * @deprecated
159: */
160: @Deprecated
161: public String readLine() throws IOException {
162: return in.readLine();
163: }
164:
165: public long readLong() throws IOException {
166: return in.readLong();
167: }
168:
169: public short readShort() throws IOException {
170: return in.readShort();
171: }
172:
173: public String readUTF() throws IOException {
174: return in.readUTF();
175: }
176:
177: public int readUnsignedByte() throws IOException {
178: return in.readUnsignedByte();
179: }
180:
181: public int readUnsignedShort() throws IOException {
182: return in.readUnsignedShort();
183: }
184:
185: public int skipBytes(int n) throws IOException {
186: return in.skipBytes(n);
187: }
188: }
|