Search in sources :

Example 1 with JavaObjectSerializer

use of org.h2.api.JavaObjectSerializer in project h2database by h2database.

the class JdbcUtils method deserialize.

/**
 * De-serialize the byte array to an object, eventually using the serializer
 * specified by the connection info.
 *
 * @param data the byte array
 * @param dataHandler provides the object serializer (may be null)
 * @return the object
 * @throws DbException if serialization fails
 */
public static Object deserialize(byte[] data, DataHandler dataHandler) {
    try {
        JavaObjectSerializer dbJavaObjectSerializer = null;
        if (dataHandler != null) {
            dbJavaObjectSerializer = dataHandler.getJavaObjectSerializer();
        }
        if (dbJavaObjectSerializer != null) {
            return dbJavaObjectSerializer.deserialize(data);
        }
        if (serializer != null) {
            return serializer.deserialize(data);
        }
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream is;
        if (SysProperties.USE_THREAD_CONTEXT_CLASS_LOADER) {
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            is = new ObjectInputStream(in) {

                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                    try {
                        return Class.forName(desc.getName(), true, loader);
                    } catch (ClassNotFoundException e) {
                        return super.resolveClass(desc);
                    }
                }
            };
        } else {
            is = new ObjectInputStream(in);
        }
        return is.readObject();
    } catch (Throwable e) {
        throw DbException.get(ErrorCode.DESERIALIZATION_FAILED_1, e, e.toString());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) JavaObjectSerializer(org.h2.api.JavaObjectSerializer) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with JavaObjectSerializer

use of org.h2.api.JavaObjectSerializer in project h2database by h2database.

the class TestJavaObjectSerializer method testStaticGlobalSerializer.

private void testStaticGlobalSerializer() throws Exception {
    JdbcUtils.serializer = new JavaObjectSerializer() {

        @Override
        public byte[] serialize(Object obj) throws Exception {
            assertEquals(100500, ((Integer) obj).intValue());
            return new byte[] { 1, 2, 3 };
        }

        @Override
        public Object deserialize(byte[] bytes) throws Exception {
            assertEquals(new byte[] { 1, 2, 3 }, bytes);
            return 100500;
        }
    };
    try {
        deleteDb("javaSerializer");
        Connection conn = getConnection("javaSerializer");
        Statement stat = conn.createStatement();
        stat.execute("create table t(id identity, val other)");
        PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)");
        ins.setObject(1, 100500, Types.JAVA_OBJECT);
        assertEquals(1, ins.executeUpdate());
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery("select val from t");
        assertTrue(rs.next());
        assertEquals(100500, ((Integer) rs.getObject(1)).intValue());
        assertEquals(new byte[] { 1, 2, 3 }, rs.getBytes(1));
        conn.close();
        deleteDb("javaSerializer");
    } finally {
        JdbcUtils.serializer = null;
    }
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JavaObjectSerializer(org.h2.api.JavaObjectSerializer)

Example 3 with JavaObjectSerializer

use of org.h2.api.JavaObjectSerializer in project h2database by h2database.

the class JdbcUtils method serialize.

/**
 * Serialize the object to a byte array, using the serializer specified by
 * the connection info if set, or the default serializer.
 *
 * @param obj the object to serialize
 * @param dataHandler provides the object serializer (may be null)
 * @return the byte array
 */
public static byte[] serialize(Object obj, DataHandler dataHandler) {
    try {
        JavaObjectSerializer handlerSerializer = null;
        if (dataHandler != null) {
            handlerSerializer = dataHandler.getJavaObjectSerializer();
        }
        if (handlerSerializer != null) {
            return handlerSerializer.serialize(obj);
        }
        if (serializer != null) {
            return serializer.serialize(obj);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(obj);
        return out.toByteArray();
    } catch (Throwable e) {
        throw DbException.get(ErrorCode.SERIALIZATION_FAILED_1, e, e.toString());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JavaObjectSerializer(org.h2.api.JavaObjectSerializer)

Example 4 with JavaObjectSerializer

use of org.h2.api.JavaObjectSerializer in project ignite by apache.

the class InlineIndexColumnTest method resetState.

/**
 */
@Before
public void resetState() throws Exception {
    inlineObjHash = false;
    IndexProcessor.serializer = new JavaObjectKeySerializer(getConfiguration());
    JdbcUtils.serializer = new JavaObjectSerializer() {

        @Override
        public byte[] serialize(Object o) throws Exception {
            return IndexProcessor.serializer.serialize(o);
        }

        @Override
        public Object deserialize(byte[] bytes) throws Exception {
            return IndexProcessor.serializer.deserialize(bytes);
        }
    };
}
Also used : ValueJavaObject(org.h2.value.ValueJavaObject) GridH2ValueCacheObject(org.apache.ignite.internal.processors.query.h2.opt.GridH2ValueCacheObject) JavaObjectKeySerializer(org.apache.ignite.internal.cache.query.index.sorted.inline.JavaObjectKeySerializer) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JavaObjectSerializer(org.h2.api.JavaObjectSerializer) Before(org.junit.Before)

Aggregations

JavaObjectSerializer (org.h2.api.JavaObjectSerializer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 JavaObjectKeySerializer (org.apache.ignite.internal.cache.query.index.sorted.inline.JavaObjectKeySerializer)1 GridH2ValueCacheObject (org.apache.ignite.internal.processors.query.h2.opt.GridH2ValueCacheObject)1 ValueJavaObject (org.h2.value.ValueJavaObject)1 Before (org.junit.Before)1