本文整理匯總了Java中com.github.ddth.commons.serialization.ISerializationSupport類的典型用法代碼示例。如果您正苦於以下問題:Java ISerializationSupport類的具體用法?Java ISerializationSupport怎麽用?Java ISerializationSupport使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ISerializationSupport類屬於com.github.ddth.commons.serialization包,在下文中一共展示了ISerializationSupport類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toByteArray
import com.github.ddth.commons.serialization.ISerializationSupport; //導入依賴的package包/類
/**
* Serialize an object to byte array, with a custom class loader.
*
* <p>
* If the target object implements {@link ISerializationSupport}, this
* method calls its {@link ISerializationSupport#toBytes()} method;
* otherwise FST library is used to serialize the object.
* </p>
*
* @param obj
* @param classLoader
* @return
*/
public static byte[] toByteArray(Object obj, ClassLoader classLoader) {
if (obj instanceof ISerializationSupport) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null) {
Thread.currentThread().setContextClassLoader(classLoader);
}
try {
return ((ISerializationSupport) obj).toBytes();
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
} else {
return toByteArrayFst(obj, classLoader);
}
}
示例2: fromByteArray
import com.github.ddth.commons.serialization.ISerializationSupport; //導入依賴的package包/類
/**
* Deserialize a byte array back to an object, with custom class loader.
*
* <p>
* If the target class implements {@link ISerializationSupport}, this method
* calls its {@link ISerializationSupport#toBytes()} method; otherwise FST
* library is used to serialize the object.
* </p>
*
* @param data
* @param clazz
* @param classLoader
* @return
*/
public static <T> T fromByteArray(byte[] data, Class<T> clazz, ClassLoader classLoader) {
if (data == null) {
return null;
}
if (ReflectionUtils.hasInterface(clazz, ISerializationSupport.class)) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null) {
Thread.currentThread().setContextClassLoader(classLoader);
}
try {
Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
T obj = constructor.newInstance();
((ISerializationSupport) obj).fromBytes(data);
return obj;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| SecurityException | IllegalArgumentException | InvocationTargetException e) {
throw new DeserializationException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
return SerializationUtils.fromByteArrayFst(data, clazz, classLoader);
}
示例3: fromBytes
import com.github.ddth.commons.serialization.ISerializationSupport; //導入依賴的package包/類
@Override
public ISerializationSupport fromBytes(byte[] data) throws DeserializationException {
String[] tokens = StringUtils.split(new String(data, UTF8), DELIM);
version = Integer.parseInt(tokens[0]);
name = tokens[1];
return this;
}
示例4: fromBytes
import com.github.ddth.commons.serialization.ISerializationSupport; //導入依賴的package包/類
/**
* {@inheritDoc}
*
* @since 0.6.0
*/
@Override
public ISerializationSupport fromBytes(byte[] data) throws DeserializationException {
return fromByteArray(data);
}