Search in sources :

Example 6 with InvalidClassException

use of java.io.InvalidClassException in project midpoint by Evolveum.

the class PropertySimpleValueFilterType method copyOf.

/**
     * Creates and returns a deep copy of a given {@code Serializable}.
     * 
     * @param serializable
     *     The instance to copy or {@code null}.
     * @return
     *     A deep copy of {@code serializable} or {@code null} if {@code serializable} is {@code null}.
     */
private static Serializable copyOf(final Serializable serializable) {
    // CC-XJC Version 2.0 Build 2011-09-16T18:27:24+0000
    if (serializable != null) {
        try {
            final ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(byteArrayOutput);
            out.writeObject(serializable);
            out.close();
            final ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(byteArrayOutput.toByteArray());
            final ObjectInputStream in = new ObjectInputStream(byteArrayInput);
            final Serializable copy = ((Serializable) in.readObject());
            in.close();
            return copy;
        } catch (SecurityException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (ClassNotFoundException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (InvalidClassException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (NotSerializableException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (StreamCorruptedException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (OptionalDataException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (IOException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) InvalidClassException(java.io.InvalidClassException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with InvalidClassException

use of java.io.InvalidClassException in project midpoint by Evolveum.

the class SearchFilterType method copyOf.

/**
     * Creates and returns a deep copy of a given {@code Serializable}.
     * 
     * @param serializable
     *     The instance to copy or {@code null}.
     * @return
     *     A deep copy of {@code serializable} or {@code null} if {@code serializable} is {@code null}.
     */
private static Serializable copyOf(final Serializable serializable) {
    // CC-XJC Version 2.0 Build 2011-09-16T18:27:24+0000
    if (serializable != null) {
        try {
            final ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(byteArrayOutput);
            out.writeObject(serializable);
            out.close();
            final ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(byteArrayOutput.toByteArray());
            final ObjectInputStream in = new ObjectInputStream(byteArrayInput);
            final Serializable copy = ((Serializable) in.readObject());
            in.close();
            return copy;
        } catch (SecurityException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (ClassNotFoundException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (InvalidClassException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (NotSerializableException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (StreamCorruptedException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (OptionalDataException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (IOException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) InvalidClassException(java.io.InvalidClassException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with InvalidClassException

use of java.io.InvalidClassException in project jdk8u_jdk by JetBrains.

the class GlobalFilterTest method testGlobalPattern.

/**
     * Serialize and deserialize an object using the default process-wide filter
     * and check allowed or reject.
     *
     * @param pattern the pattern
     * @param object the test object
     * @param allowed the expected result from ObjectInputStream (exception or not)
     */
static void testGlobalPattern(String pattern, Object object, boolean allowed) {
    try {
        //            System.out.printf("global %s pattern: %s, obj: %s%n", (allowed ? "allowed" : "not allowed"), pattern, object);
        byte[] bytes = SerialFilterTest.writeObjects(object);
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais)) {
            Object o = ois.readObject();
        } catch (EOFException eof) {
        // normal completion
        } catch (ClassNotFoundException cnf) {
            Assert.fail("Deserializing", cnf);
        }
        Assert.assertTrue(allowed, "filter should have thrown an exception");
    } catch (IllegalArgumentException iae) {
        Assert.fail("bad format pattern", iae);
    } catch (InvalidClassException ice) {
        Assert.assertFalse(allowed, "filter should not have thrown an exception: " + ice);
    } catch (IOException ioe) {
        Assert.fail("Unexpected IOException", ioe);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidClassException(java.io.InvalidClassException) EOFException(java.io.EOFException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 9 with InvalidClassException

use of java.io.InvalidClassException in project jdk8u_jdk by JetBrains.

the class MixedFiltersTest method testRejectedInGlobal.

/**
     * Test:
     *   "global filter reject" + "specific ObjectInputStream filter is empty" => should reject
     *   "global filter reject" + "specific ObjectInputStream filter allow"    => should allow
     */
@Test(dataProvider = "RejectedInGlobal")
public void testRejectedInGlobal(Object toDeserialized, String pattern) throws Exception {
    byte[] bytes = SerialFilterTest.writeObjects(toDeserialized);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        Object o = ois.readObject();
        fail("filter should have thrown an exception");
    } catch (InvalidClassException expected) {
    }
    ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        ObjectInputFilter.Config.setObjectInputFilter(ois, filter);
        Object o = ois.readObject();
    }
}
Also used : ObjectInputFilter(sun.misc.ObjectInputFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidClassException(java.io.InvalidClassException) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Example 10 with InvalidClassException

use of java.io.InvalidClassException in project android-beacon-library by AltBeacon.

the class MonitoringStatus method restoreMonitoringStatus.

protected void restoreMonitoringStatus() {
    FileInputStream inputStream = null;
    ObjectInputStream objectInputStream = null;
    try {
        inputStream = mContext.openFileInput(STATUS_PRESERVATION_FILE_NAME);
        objectInputStream = new ObjectInputStream(inputStream);
        Map<Region, RegionMonitoringState> obj = (Map<Region, RegionMonitoringState>) objectInputStream.readObject();
        LogManager.d(TAG, "Restored region monitoring state for " + obj.size() + " regions.");
        for (Region region : obj.keySet()) {
            LogManager.d(TAG, "Region  " + region + " uniqueId: " + region.getUniqueId() + " state: " + obj.get(region));
        }
        // Mark all beacons that were inside again so they don't trigger as a new exit - enter.
        for (RegionMonitoringState regionMonitoringState : obj.values()) {
            if (regionMonitoringState.getInside()) {
                regionMonitoringState.markInside();
            }
        }
        mRegionsStatesMap.putAll(obj);
    } catch (IOException | ClassNotFoundException | ClassCastException e) {
        if (e instanceof InvalidClassException) {
            LogManager.d(TAG, "Serialized Monitoring State has wrong class. Just ignoring saved state...");
        } else
            LogManager.e(TAG, "Deserialization exception, message: %s", e.getMessage());
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (IOException ignored) {
            }
        }
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : InvalidClassException(java.io.InvalidClassException) Region(org.altbeacon.beacon.Region) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

InvalidClassException (java.io.InvalidClassException)26 ObjectInputStream (java.io.ObjectInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 IOException (java.io.IOException)9 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)6 StreamCorruptedException (java.io.StreamCorruptedException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NotSerializableException (java.io.NotSerializableException)4 ObjectOutputStream (java.io.ObjectOutputStream)4 OptionalDataException (java.io.OptionalDataException)4 Serializable (java.io.Serializable)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 Server (org.compiere.interfaces.Server)4 ObjectInputFilter (sun.misc.ObjectInputFilter)4 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1