diff options
| author | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-07-20 20:00:05 +0100 |
|---|---|---|
| committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-07-20 20:00:05 +0100 |
| commit | 3ef782d3745ea8f25a3151561a3cfb882190210e (patch) | |
| tree | 86b9c2f5fde051dd0bced99b3fc9f5a3ba08db69 /lang/java/src/com/sleepycat/bind/serial/SerialInput.java | |
| download | berkeleydb-3ef782d3745ea8f25a3151561a3cfb882190210e.tar.gz | |
Tarball conversion
Diffstat (limited to 'lang/java/src/com/sleepycat/bind/serial/SerialInput.java')
| -rw-r--r-- | lang/java/src/com/sleepycat/bind/serial/SerialInput.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lang/java/src/com/sleepycat/bind/serial/SerialInput.java b/lang/java/src/com/sleepycat/bind/serial/SerialInput.java new file mode 100644 index 00000000..23e493e6 --- /dev/null +++ b/lang/java/src/com/sleepycat/bind/serial/SerialInput.java @@ -0,0 +1,96 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2000, 2012 Oracle and/or its affiliates. All rights reserved. + * + */ + +package com.sleepycat.bind.serial; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; + +import com.sleepycat.db.DatabaseException; +import com.sleepycat.util.ClassResolver; +import com.sleepycat.util.RuntimeExceptionWrapper; + +/** + * A specialized <code>ObjectInputStream</code> that gets class description + * information from a <code>ClassCatalog</code>. It is used by + * <code>SerialBinding</code>. + * + * <p>This class is used instead of an {@link ObjectInputStream}, which it + * extends, to read an object stream written by the {@link SerialOutput} class. + * For reading objects from a database normally one of the serial binding + * classes is used. {@link SerialInput} is used when an {@link + * ObjectInputStream} is needed along with compact storage. A {@link + * ClassCatalog} must be supplied, however, to stored shared class + * descriptions.</p> + * + * @see <a href="SerialBinding.html#evolution">Class Evolution</a> + * + * @author Mark Hayes + */ +public class SerialInput extends ClassResolver.Stream { + + private ClassCatalog classCatalog; + + /** + * Creates a serial input stream. + * + * @param in is the input stream from which compact serialized objects will + * be read. + * + * @param classCatalog is the catalog containing the class descriptions + * for the serialized objects. + */ + public SerialInput(InputStream in, ClassCatalog classCatalog) + throws IOException { + + this(in, classCatalog, null); + } + + /** + * Creates a serial input stream. + * + * @param in is the input stream from which compact serialized objects will + * be read. + * + * @param classCatalog is the catalog containing the class descriptions + * for the serialized objects. + * + * @param classLoader is the class loader to use, or null if a default + * class loader should be used. + */ + public SerialInput(InputStream in, + ClassCatalog classCatalog, + ClassLoader classLoader) + throws IOException { + + super(in, classLoader); + this.classCatalog = classCatalog; + } + + @Override + protected ObjectStreamClass readClassDescriptor() + throws IOException, ClassNotFoundException { + + try { + byte len = readByte(); + byte[] id = new byte[len]; + readFully(id); + + return classCatalog.getClassFormat(id); + } catch (DatabaseException e) { + + /* + * Do not throw IOException from here since ObjectOutputStream + * will write the exception to the stream, which causes another + * call here, etc. + */ + throw RuntimeExceptionWrapper.wrapIfNeeded(e); + } + } +} |
