Search in sources :

Example 26 with ObjectStreamClass

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

the class HBaseHelper method fromBytes.

public Object fromBytes(byte[] binary) {
    Object result = null;
    ObjectInputStream ois = null;
    if (binary == null) {
        return null;
    }
    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 = 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);
        IOHelper.close(bais);
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 27 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 28 with ObjectStreamClass

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

the class SerializationTest method testSerializeFieldMadeTransient.

// http://b/4471249
public void testSerializeFieldMadeTransient() throws Exception {
    // Does ObjectStreamClass have the right idea?
    ObjectStreamClass osc = ObjectStreamClass.lookup(FieldMadeTransient.class);
    ObjectStreamField[] fields = osc.getFields();
    assertEquals(1, fields.length);
    assertEquals("nonTransientInt", fields[0].getName());
    assertEquals(int.class, fields[0].getType());
    // this was created by serializing a FieldMadeTransient with a non-0 transientInt
    String s = "aced0005737200346c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657" + "374244669656c644d6164655472616e7369656e74000000000000000002000149000c7472616e736" + "9656e74496e747870abababab";
    FieldMadeTransient deserialized = (FieldMadeTransient) SerializationTester.deserializeHex(s);
    assertEquals(0, deserialized.transientInt);
}
Also used : ObjectStreamField(java.io.ObjectStreamField) ObjectStreamClass(java.io.ObjectStreamClass)

Example 29 with ObjectStreamClass

use of java.io.ObjectStreamClass in project platform_frameworks_base by android.

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 30 with ObjectStreamClass

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

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)

Aggregations

ObjectStreamClass (java.io.ObjectStreamClass)40 ObjectInputStream (java.io.ObjectInputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 ObjectOutputStream (java.io.ObjectOutputStream)8 ObjectStreamField (java.io.ObjectStreamField)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Serializable (java.io.Serializable)5 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 StreamCorruptedException (java.io.StreamCorruptedException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 SerializationException (krati.io.SerializationException)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 FileContext (org.apache.hadoop.fs.FileContext)1 Path (org.apache.hadoop.fs.Path)1