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.DataOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectOutput;
025: import java.io.OutputStream;
026:
027: import org.apache.mina.common.IoBuffer;
028:
029: /**
030: * An {@link ObjectOutput} and {@link OutputStream} that can write the objects as
031: * the serialized form that {@link ObjectSerializationDecoder} can decode.
032: *
033: * @author The Apache MINA Project (dev@mina.apache.org)
034: * @version $Rev: 581234 $, $Date: 2007-10-02 07:39:48 -0600 (Tue, 02 Oct 2007) $
035: */
036: public class ObjectSerializationOutputStream extends OutputStream
037: implements ObjectOutput {
038:
039: private final DataOutputStream out;
040:
041: private int maxObjectSize = Integer.MAX_VALUE;
042:
043: public ObjectSerializationOutputStream(OutputStream out) {
044: if (out == null) {
045: throw new NullPointerException("out");
046: }
047:
048: if (out instanceof DataOutputStream) {
049: this .out = (DataOutputStream) out;
050: } else {
051: this .out = new DataOutputStream(out);
052: }
053: }
054:
055: /**
056: * Returns the allowed maximum size of the encoded object.
057: * If the size of the encoded object exceeds this value, this encoder
058: * will throw a {@link IllegalArgumentException}. The default value
059: * is {@link Integer#MAX_VALUE}.
060: */
061: public int getMaxObjectSize() {
062: return maxObjectSize;
063: }
064:
065: /**
066: * Sets the allowed maximum size of the encoded object.
067: * If the size of the encoded object exceeds this value, this encoder
068: * will throw a {@link IllegalArgumentException}. The default value
069: * is {@link Integer#MAX_VALUE}.
070: */
071: public void setMaxObjectSize(int maxObjectSize) {
072: if (maxObjectSize <= 0) {
073: throw new IllegalArgumentException("maxObjectSize: "
074: + maxObjectSize);
075: }
076:
077: this .maxObjectSize = maxObjectSize;
078: }
079:
080: @Override
081: public void close() throws IOException {
082: out.close();
083: }
084:
085: @Override
086: public void flush() throws IOException {
087: out.flush();
088: }
089:
090: @Override
091: public void write(int b) throws IOException {
092: out.write(b);
093: }
094:
095: @Override
096: public void write(byte[] b) throws IOException {
097: out.write(b);
098: }
099:
100: @Override
101: public void write(byte[] b, int off, int len) throws IOException {
102: out.write(b, off, len);
103: }
104:
105: public void writeObject(Object obj) throws IOException {
106: IoBuffer buf = IoBuffer.allocate(64, false);
107: buf.setAutoExpand(true);
108: buf.putObject(obj);
109:
110: int objectSize = buf.position() - 4;
111: if (objectSize > maxObjectSize) {
112: throw new IllegalArgumentException(
113: "The encoded object is too big: " + objectSize
114: + " (> " + maxObjectSize + ')');
115: }
116:
117: out.write(buf.array(), 0, buf.position());
118: }
119:
120: public void writeBoolean(boolean v) throws IOException {
121: out.writeBoolean(v);
122: }
123:
124: public void writeByte(int v) throws IOException {
125: out.writeByte(v);
126: }
127:
128: public void writeBytes(String s) throws IOException {
129: out.writeBytes(s);
130: }
131:
132: public void writeChar(int v) throws IOException {
133: out.writeChar(v);
134: }
135:
136: public void writeChars(String s) throws IOException {
137: out.writeChars(s);
138: }
139:
140: public void writeDouble(double v) throws IOException {
141: out.writeDouble(v);
142: }
143:
144: public void writeFloat(float v) throws IOException {
145: out.writeFloat(v);
146: }
147:
148: public void writeInt(int v) throws IOException {
149: out.writeInt(v);
150: }
151:
152: public void writeLong(long v) throws IOException {
153: out.writeLong(v);
154: }
155:
156: public void writeShort(int v) throws IOException {
157: out.writeShort(v);
158: }
159:
160: public void writeUTF(String str) throws IOException {
161: out.writeUTF(str);
162: }
163: }
|