Search in sources :

Example 36 with InvalidObjectException

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);
}
Also used : InvalidObjectException(java.io.InvalidObjectException) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 37 with InvalidObjectException

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) {
        }
    }
}
Also used : MessageFormat(java.text.MessageFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidObjectException(java.io.InvalidObjectException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 38 with InvalidObjectException

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidObjectException(java.io.InvalidObjectException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) NumberFormat(java.text.NumberFormat) ObjectInputStream(java.io.ObjectInputStream)

Example 39 with InvalidObjectException

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) {
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidObjectException(java.io.InvalidObjectException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 40 with InvalidObjectException

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
    }
}
Also used : Rational(android.util.Rational) InvalidObjectException(java.io.InvalidObjectException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

InvalidObjectException (java.io.InvalidObjectException)56 ObjectInputStream (java.io.ObjectInputStream)13 IOException (java.io.IOException)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)5 Rational (android.util.Rational)5 GwtIncompatible (com.google.common.annotations.GwtIncompatible)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 GwtIncompatible (com.google_voltpatches.common.annotations.GwtIncompatible)2 Point (java.awt.Point)2 RemoteObjectInvocationHandler (java.rmi.server.RemoteObjectInvocationHandler)2 RemoteRef (java.rmi.server.RemoteRef)2 Comparator (java.util.Comparator)2 SimpleTimeZone (java.util.SimpleTimeZone)2 TimeZone (java.util.TimeZone)2 MBeanException (javax.management.MBeanException)2 OpenDataException (javax.management.openmbean.OpenDataException)2 IgniteLogger (org.apache.ignite.IgniteLogger)2 ANTLRException (antlr.ANTLRException)1