use of java.io.InvalidObjectException in project guava by google.
the class ImmutableListMultimap method readObject.
// java.io.ObjectInputStream
@GwtIncompatible
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int keyCount = stream.readInt();
if (keyCount < 0) {
throw new InvalidObjectException("Invalid key count " + keyCount);
}
ImmutableMap.Builder<Object, ImmutableList<Object>> builder = ImmutableMap.builder();
int tmpSize = 0;
for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}
ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
}
builder.put(key, valuesBuilder.build());
tmpSize += valueCount;
}
ImmutableMap<Object, ImmutableList<Object>> tmpMap;
try {
tmpMap = builder.build();
} catch (IllegalArgumentException e) {
throw (InvalidObjectException) new InvalidObjectException(e.getMessage()).initCause(e);
}
FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
}
use of java.io.InvalidObjectException in project robovm by robovm.
the class OldMessageFormatFieldTest method test_readResolve.
public void test_readResolve() {
// test for method java.lang.Object readResolve()
// see serialization stress tests:
// implemented in
// SerializationStressTest4.test_writeObject_MessageFormat_Field()
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
out = new ObjectOutputStream(bytes);
MessageFormat.Field mfield, mfield2;
MyMessageFormat field;
mfield = MessageFormat.Field.ARGUMENT;
field = new MyMessageFormat(null);
out.writeObject(mfield);
out.writeObject(field);
in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
try {
mfield2 = (MessageFormat.Field) in.readObject();
assertSame("resolved incorrectly", mfield, mfield2);
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException: " + e);
}
try {
in.readObject();
fail("Expected InvalidObjectException for subclass instance with null name");
} catch (InvalidObjectException e) {
}
} catch (IOException e) {
fail("unexpected IOException" + e);
} catch (ClassNotFoundException e) {
fail("unexpected ClassNotFoundException" + e);
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
use of java.io.InvalidObjectException in project robovm by robovm.
the class OldNumberFormatFieldTest method test_readResolve.
public void test_readResolve() throws IOException, ClassNotFoundException {
// test for method java.lang.Object readResolve()
// see serialization stress tests:
// implemented in
// SerializationStressTest4.test_writeObject_NumberFormat_Field()
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
MyNumberFormat field;
NumberFormat.Field nfield = NumberFormat.Field.CURRENCY;
field = new MyNumberFormat(null);
out.writeObject(nfield);
out.writeObject(field);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
NumberFormat.Field nfield2 = (NumberFormat.Field) in.readObject();
assertSame("resolved incorrectly", nfield, nfield2);
try {
in.readObject();
fail("Expected InvalidObjectException for subclass instance with null name");
} catch (InvalidObjectException e) {
}
in.close();
out.close();
}
use of java.io.InvalidObjectException in project robovm by robovm.
the class OldAttributedCharacterIteratorAttributeTest method test_readResolve.
/**
* java.text.AttributedCharacterIterator.Attribute#readResolve() Test
* of method
* java.text.AttributedCharacterIterator.Attribute#readResolve().
*/
public void test_readResolve() {
MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute("test");
try {
mac1.readResolve();
fail("InvalidObjectException has not been thrown");
} catch (InvalidObjectException e) {
// expected
}
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
out = new ObjectOutputStream(bytes);
AttributedCharacterIterator.Attribute attr1, attr2;
attr1 = AttributedCharacterIterator.Attribute.LANGUAGE;
out.writeObject(attr1);
in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
try {
attr2 = (AttributedCharacterIterator.Attribute) in.readObject();
assertSame("resolved incorrectly", attr1, attr2);
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException: " + e);
}
} catch (IOException e) {
fail("unexpected IOException" + e);
} catch (ClassNotFoundException e) {
fail("unexpected ClassNotFoundException" + e);
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
use of java.io.InvalidObjectException in project android_frameworks_base by ResurrectionRemix.
the class RationalTest method testSerialize.
@SmallTest
public void testSerialize() throws ClassNotFoundException, IOException {
/*
* Check correct [de]serialization
*/
assertEqualsAfterSerializing(ZERO);
assertEqualsAfterSerializing(NaN);
assertEqualsAfterSerializing(NEGATIVE_INFINITY);
assertEqualsAfterSerializing(POSITIVE_INFINITY);
assertEqualsAfterSerializing(UNIT);
assertEqualsAfterSerializing(new Rational(100, 200));
assertEqualsAfterSerializing(new Rational(-100, 200));
assertEqualsAfterSerializing(new Rational(5, 1));
assertEqualsAfterSerializing(new Rational(Integer.MAX_VALUE, Integer.MIN_VALUE));
/*
* Check bad deserialization fails
*/
try {
// [0, 100] , should be [0, 1]
Rational badZero = createIllegalRational(0, 100);
Rational results = serializeRoundTrip(badZero);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [100, 0] , should be [1, 0]
Rational badPosInfinity = createIllegalRational(100, 0);
Rational results = serializeRoundTrip(badPosInfinity);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
Rational badNegInfinity = // [-100, 0] , should be [-1, 0]
createIllegalRational(-100, 0);
Rational results = serializeRoundTrip(badNegInfinity);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [2,4] , should be [1, 2]
Rational badReduced = createIllegalRational(2, 4);
Rational results = serializeRoundTrip(badReduced);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [-2, 4] should be [-1, 2]
Rational badReducedNeg = createIllegalRational(-2, 4);
Rational results = serializeRoundTrip(badReducedNeg);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
}
Aggregations