001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.io;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.NotSerializableException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.OutputStream;
025:
026: import org.apache.wicket.util.lang.Objects;
027:
028: /**
029: * Interface for serializing and deserializing so that we can very the
030: * implementation of the {@link ObjectOutputStream} and
031: * {@link ObjectInputStream} implementations.
032: *
033: * @see Objects#objectToByteArray(Object)
034: * @see Objects#byteArrayToObject(byte[])
035: *
036: * @author eelcohillenius
037: */
038: public interface IObjectStreamFactory {
039: /**
040: * Default implementation that uses the JDK's plain implementations.
041: */
042: public static final class DefaultObjectStreamFactory implements
043: IObjectStreamFactory {
044: /**
045: * @see org.apache.wicket.util.io.IObjectStreamFactory#newObjectInputStream(java.io.InputStream)
046: */
047: public ObjectInputStream newObjectInputStream(InputStream in)
048: throws IOException {
049: return new ObjectInputStream(in);
050: }
051:
052: /**
053: * @see org.apache.wicket.util.io.IObjectStreamFactory#newObjectOutputStream(java.io.OutputStream)
054: */
055: public ObjectOutputStream newObjectOutputStream(
056: final OutputStream out) throws IOException {
057: final ObjectOutputStream oos = new ObjectOutputStream(out);
058: return new ObjectOutputStream() {
059: protected void writeObjectOverride(final Object obj)
060: throws IOException {
061: try {
062: oos.writeObject(obj);
063: } catch (IOException e) {
064: if (SerializableChecker.isAvailable()) {
065: // trigger serialization again, but this time gather
066: // some more info
067: new SerializableChecker(
068: (NotSerializableException) e)
069: .writeObject(obj);
070: // if we get here, we didn't fail, while we
071: // should;
072: throw e;
073: }
074: throw e;
075: }
076: }
077: };
078: }
079: }
080:
081: /**
082: * Gets a new instance of an {@link ObjectInputStream} with the provided
083: * {@link InputStream}.
084: *
085: * @param in
086: * The inpu stream that should be used for the reading
087: * @return a new object input stream instance
088: * @throws IOException
089: * if an I/O error occurs while reading stream header
090: */
091: ObjectInputStream newObjectInputStream(InputStream in)
092: throws IOException;
093:
094: /**
095: * Gets a new instance of an {@link ObjectOutputStream} with the provided
096: * {@link OutputStream}.
097: *
098: * @param out
099: * The output stream that should be used for the writing
100: * @return a new object output stream instance
101: * @throws IOException
102: * if an I/O error occurs while writing stream header
103: */
104: ObjectOutputStream newObjectOutputStream(OutputStream out)
105: throws IOException;
106: }
|