public class ValidatingObjectInputStream
extends java.io.ObjectInputStream
ObjectInputStream that's restricted to deserialize a limited set of classes.
Various accept/reject methods allow for specifying which classes can be deserialized.
Here is the only way to safely read a HashMap of String keys and Integer values:
// Defining Object fixture
final HashMap<String, Integer> map1 = new HashMap<>();
map1.put("1", 1);
// Writing serialized fixture
final byte[] byteArray;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(map1);
oos.flush();
byteArray = baos.toByteArray();
}
// Reading
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
ValidatingObjectInputStream vois = ValidatingObjectInputStream.builder()
.accept(HashMap.class, Number.class, Integer.class)
.setInputStream(bais)
.get()) {
// String.class is automatically accepted
final HashMap<String, Integer> map2 = (HashMap<String, Integer>) vois.readObject();
assertEquals(map1, map2);
}
// Reusing a configuration
final ObjectStreamClassPredicate predicate = new ObjectStreamClassPredicate()
.accept(HashMap.class, Number.class, Integer.class);
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
ValidatingObjectInputStream vois = ValidatingObjectInputStream.builder()
.setPredicate(predicate)
.setInputStream(bais)
.get()) {
// String.class is automatically accepted
final HashMap<String, Integer> map2 = (HashMap<String, Integer>) vois.readObject();
assertEquals(map1, map2);
}
Design inspired by a IBM DeveloperWorks Article.
| Modifier and Type | Class and Description |
|---|---|
static class |
ValidatingObjectInputStream.Builder
Builds a new
ValidatingObjectInputStream. |
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING| Constructor and Description |
|---|
ValidatingObjectInputStream(java.io.InputStream input)
Deprecated.
Use
builder(). |
| Modifier and Type | Method and Description |
|---|---|
ValidatingObjectInputStream |
accept(java.lang.Class<?>... classes)
Accepts the specified classes for deserialization, unless they are otherwise rejected.
|
ValidatingObjectInputStream |
accept(ClassNameMatcher matcher)
Accepts class names where the supplied ClassNameMatcher matches for deserialization, unless they are otherwise rejected.
|
ValidatingObjectInputStream |
accept(java.util.regex.Pattern pattern)
Accepts class names that match the supplied pattern for deserialization, unless they are otherwise rejected.
|
ValidatingObjectInputStream |
accept(java.lang.String... patterns)
Accepts the wildcard specified classes for deserialization, unless they are otherwise rejected.
|
static ValidatingObjectInputStream.Builder |
builder()
Constructs a new
ValidatingObjectInputStream.Builder. |
<T> T |
readObjectCast()
Delegates to
ObjectInputStream.readObject() and casts to the generic T. |
ValidatingObjectInputStream |
reject(java.lang.Class<?>... classes)
Rejects the specified classes for deserialization, even if they are otherwise accepted.
|
ValidatingObjectInputStream |
reject(ClassNameMatcher matcher)
Rejects class names where the supplied ClassNameMatcher matches for deserialization, even if they are otherwise accepted.
|
ValidatingObjectInputStream |
reject(java.util.regex.Pattern pattern)
Rejects class names that match the supplied pattern for deserialization, even if they are otherwise accepted.
|
ValidatingObjectInputStream |
reject(java.lang.String... patterns)
Rejects the wildcard specified classes for deserialization, even if they are otherwise accepted.
|
available, close, defaultReadObject, read, read, readBoolean, readByte, readChar, readDouble, readFields, readFloat, readFully, readFully, readInt, readLine, readLong, readObject, readShort, readUnshared, readUnsignedByte, readUnsignedShort, readUTF, registerValidation, skipBytes@Deprecated
public ValidatingObjectInputStream(java.io.InputStream input)
throws java.io.IOException
builder().input - an input streamjava.io.IOException - if an I/O error occurs while reading stream headerpublic static ValidatingObjectInputStream.Builder builder()
ValidatingObjectInputStream.Builder.ValidatingObjectInputStream.Builder.public ValidatingObjectInputStream accept(java.lang.Class<?>... classes)
The reject list takes precedence over the accept list.
classes - Classes to acceptpublic ValidatingObjectInputStream accept(ClassNameMatcher matcher)
The reject list takes precedence over the accept list.
matcher - a class name matcher to accept objects.public ValidatingObjectInputStream accept(java.util.regex.Pattern pattern)
The reject list takes precedence over the accept list.
pattern - a Pattern for compiled regular expression.public ValidatingObjectInputStream accept(java.lang.String... patterns)
The reject list takes precedence over the accept list.
patterns - Wildcard file name patterns as defined by FilenameUtils.wildcardMatch.public <T> T readObjectCast()
throws java.lang.ClassNotFoundException,
java.io.IOException
ObjectInputStream.readObject() and casts to the generic T.T - The return type.ObjectInputStream.readObject().java.lang.ClassNotFoundException - Thrown by ObjectInputStream.readObject().java.io.IOException - Thrown by ObjectInputStream.readObject().java.lang.ClassCastException - Thrown when ObjectInputStream.readObject() does not match T.public ValidatingObjectInputStream reject(java.lang.Class<?>... classes)
The reject list takes precedence over the accept list.
classes - Classes to reject.public ValidatingObjectInputStream reject(ClassNameMatcher matcher)
The reject list takes precedence over the accept list.
matcher - a class name matcher to reject objects.public ValidatingObjectInputStream reject(java.util.regex.Pattern pattern)
The reject list takes precedence over the accept list.
pattern - a Pattern for compiled regular expression.public ValidatingObjectInputStream reject(java.lang.String... patterns)
The reject list takes precedence over the accept list.
patterns - An array of wildcard file name patterns as defined by FilenameUtils.wildcardMatchCopyright © 2010 - 2025 Adobe. All Rights Reserved