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());
}
}
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;
}
}
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());
}
}
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);
}
};
}
Aggregations