Search in sources :

Example 46 with ObjectStreamClass

use of java.io.ObjectStreamClass in project android_frameworks_base by crdroidandroid.

the class Parcel method readSerializable.

private final Serializable readSerializable(final ClassLoader loader) {
    String name = readString();
    if (name == null) {
        // return null, which indicates that the name wasn't found in the parcel.
        return null;
    }
    byte[] serializedData = createByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
    try {
        ObjectInputStream ois = new ObjectInputStream(bais) {

            @Override
            protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundException {
                // try the custom classloader if provided
                if (loader != null) {
                    Class<?> c = Class.forName(osClass.getName(), false, loader);
                    if (c != null) {
                        return c;
                    }
                }
                return super.resolveClass(osClass);
            }
        };
        return (Serializable) ois.readObject();
    } catch (IOException ioe) {
        throw new RuntimeException("Parcelable encountered " + "IOException reading a Serializable object (name = " + name + ")", ioe);
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException("Parcelable encountered " + "ClassNotFoundException reading a Serializable object (name = " + name + ")", cnfe);
    }
}
Also used : Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream)

Example 47 with ObjectStreamClass

use of java.io.ObjectStreamClass in project camel by apache.

the class KratiDefaultSerializer method deserialize.

/**
     * Deserialize an object from its raw bytes generated by {{@link #serialize(Object)}.
     *
     * @param binary - the raw bytes from which an object is constructed.
     * @return an object constructed from the raw bytes.
     * @throws SerializationException if the object cannot be constructed from the raw bytes.
     */
@SuppressWarnings("unchecked")
public T deserialize(byte[] binary) throws SerializationException {
    T result = null;
    ObjectInputStream ois = null;
    if (binary == null) {
        return null;
    }
    // TODO: should use Camel's ClassResolver for classloading
    ByteArrayInputStream bais = new ByteArrayInputStream(binary);
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        ois = new ObjectInputStream(bais) {

            @Override
            public Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                try {
                    return classLoader.loadClass(desc.getName());
                } catch (Exception e) {
                }
                return super.resolveClass(desc);
            }
        };
        result = (T) ois.readObject();
    } catch (IOException e) {
        LOG.warn("Error while deserializing object. Null will be used.", e);
    } catch (ClassNotFoundException e) {
        LOG.warn("Could not find class while deserializing object. Null will be used.", e);
    } finally {
        IOHelper.close(ois, bais);
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) SerializationException(krati.io.SerializationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 48 with ObjectStreamClass

use of java.io.ObjectStreamClass in project WorldPainter by Captain-Chaos.

the class WPCustomObjectInputStream method readClassDescriptor.

@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
    ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
    Class localClass = resolveClass(resultClassDescriptor);
    if (patchClasses.contains(localClass)) {
        ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
        if (localClassDescriptor != null) {
            final long localSUID = localClassDescriptor.getSerialVersionUID();
            final long streamSUID = resultClassDescriptor.getSerialVersionUID();
            if (streamSUID != localSUID) {
                logger.warn("Overriding serialized class version mismatch: local serialVersionUID = " + localSUID + " stream serialVersionUID = " + streamSUID);
                resultClassDescriptor = localClassDescriptor;
            }
        }
    } else if (localClass == null) {
        logger.warn("No local class for " + resultClassDescriptor.getName());
    }
    return resultClassDescriptor;
}
Also used : ObjectStreamClass(java.io.ObjectStreamClass) ObjectStreamClass(java.io.ObjectStreamClass)

Example 49 with ObjectStreamClass

use of java.io.ObjectStreamClass in project fabric8 by jboss-fuse.

the class ZkDataStoreImpl method getContainerMetadata.

@Override
public CreateContainerMetadata getContainerMetadata(String containerId, final ClassLoader classLoader) {
    assertValid();
    try {
        byte[] encoded = getByteData(configCache, ZkPath.CONTAINER_METADATA.getPath(containerId));
        if (encoded == null) {
            return null;
        }
        byte[] decoded = Base64Encoder.decode(encoded);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(decoded)) {

            @Override
            protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                return classLoader.loadClass(desc.getName());
            }
        };
        return (CreateContainerMetadata) ois.readObject();
    } catch (ClassNotFoundException | InvalidClassException | KeeperException.NoNodeException e) {
        return null;
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) InvalidClassException(java.io.InvalidClassException) ObjectStreamClass(java.io.ObjectStreamClass) FabricException(io.fabric8.api.FabricException) InvalidClassException(java.io.InvalidClassException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 50 with ObjectStreamClass

use of java.io.ObjectStreamClass in project revapi by revapi.

the class SUIDGeneratorTest method testSUIDGeneration.

@Test
public void testSUIDGeneration() throws Exception {
    try {
        ObjectStreamClass s = ObjectStreamClass.lookup(TestClass.class);
        long officialSUID = s.getSerialVersionUID();
        SUIDGeneratingAnnotationProcessor ap = new SUIDGeneratingAnnotationProcessor();
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, Arrays.asList(TestClass.class.getName()), Arrays.asList(new SourceInClassLoader("suid/TestClass.java")));
        task.setProcessors(Arrays.asList(ap));
        task.call();
        Assert.assertEquals(officialSUID, ap.generatedSUID);
    } finally {
        new File("TestClass.class").delete();
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) ObjectStreamClass(java.io.ObjectStreamClass) File(java.io.File) Test(org.junit.Test)

Aggregations

ObjectStreamClass (java.io.ObjectStreamClass)70 IOException (java.io.IOException)27 ObjectInputStream (java.io.ObjectInputStream)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ObjectOutputStream (java.io.ObjectOutputStream)14 ObjectStreamField (java.io.ObjectStreamField)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Serializable (java.io.Serializable)7 InvalidClassException (java.io.InvalidClassException)3 Field (java.lang.reflect.Field)3 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)2 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JavaCompiler (javax.tools.JavaCompiler)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Test (org.junit.Test)2 ParseException (org.locationtech.jts.io.ParseException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 OGroup (eu.esdihumboldt.hale.common.instance.orient.OGroup)1