use of java.io.ObjectInput in project stream-lib by addthis.
the class TestStreamSummary method testSerialization.
@SuppressWarnings("unchecked")
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
StreamSummary<String> vs = new StreamSummary<String>(3);
String[] stream = { "X", "X", "Y", "Z", "A", "B", "C", "X", "X", "A", "C", "A", "A" };
for (String i : stream) {
vs.offer(i);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput oo = new ObjectOutputStream(baos);
oo.writeObject(vs);
oo.close();
ObjectInput oi = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
StreamSummary<String> clone = (StreamSummary<String>) oi.readObject();
assertEquals(vs.toString(), clone.toString());
}
use of java.io.ObjectInput in project imcache by Cetsoft.
the class Serializer method deserialize.
/**
* Deserialize.
*
* @param <C> the generic type
* @param bytes the bytes
* @return the c
*/
@SuppressWarnings("unchecked")
public static <C> C deserialize(byte[] bytes) {
Object object = null;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
try {
ObjectInput in = new ObjectInputStream(bis);
object = in.readObject();
bis.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return (C) object;
}
use of java.io.ObjectInput in project wildfly by wildfly.
the class DsTestCase method testDatasourceSerialization.
@Test
public void testDatasourceSerialization() throws Exception {
InitialContext context = new InitialContext();
DataSource originalDs = (DataSource) context.lookup(JNDI_NAME);
//serialize
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
DataSource ds;
ObjectInput in = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(originalDs);
byte[] bytes = bos.toByteArray();
//deserialize
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
try {
in = new ObjectInputStream(bis);
ds = (DataSource) in.readObject();
} finally {
try {
bis.close();
} catch (IOException ex) {
// ignore close exception
}
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
//use
Connection conn = ds.getConnection();
ResultSet rs = conn.prepareStatement("select 1").executeQuery();
Assert.assertTrue(rs.next());
conn.close();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
// ignore close exception
}
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
}
use of java.io.ObjectInput in project motan by weibocom.
the class CompressRpcCodec method decodeRequest.
private Object decodeRequest(byte[] body, long requestId, String remoteIp, Serialization serialization) throws IOException, ClassNotFoundException {
ObjectInput input = createInput(getInputStream(body));
String interfaceName = null;
String methodName = null;
String paramtersDesc = null;
String group = null;
String version = null;
String flag = input.readUTF();
if (SIGN_FLAG.equals(flag)) {
// 方法签名方式
String sign = input.readUTF();
MethodInfo mInfo = SIGN_METHOD_MAP.get(sign);
if (mInfo == null) {
throw new MotanFrameworkException("decode error: invalid method sign: " + sign, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
interfaceName = mInfo.getInterfaceName();
methodName = mInfo.getMethodName();
paramtersDesc = mInfo.getParamtersDesc();
group = mInfo.getGroup();
version = mInfo.getVersion();
} else {
interfaceName = flag;
methodName = input.readUTF();
paramtersDesc = input.readUTF();
}
DefaultRequest rpcRequest = new DefaultRequest();
rpcRequest.setRequestId(requestId);
rpcRequest.setInterfaceName(interfaceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParamtersDesc(paramtersDesc);
rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
rpcRequest.setAttachments(decodeRequestAttachments(input));
rpcRequest.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
input.close();
Map<String, String> attachments = rpcRequest.getAttachments();
// 根据签名添加client固定参数。
putSignedAttachment(attachments, remoteIp);
if (attachments.get(URLParamType.group.name()) == null) {
// 如果attachment sign失效时,需要使用methodsign中的group信息。
attachments.put(URLParamType.group.name(), group);
attachments.put(URLParamType.version.name(), version);
}
return rpcRequest;
}
use of java.io.ObjectInput in project motan by weibocom.
the class DefaultRpcCodec method decodeRequest.
private Object decodeRequest(byte[] body, long requestId, Serialization serialization) throws IOException, ClassNotFoundException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(body);
ObjectInput input = createInput(inputStream);
String interfaceName = input.readUTF();
String methodName = input.readUTF();
String paramtersDesc = input.readUTF();
DefaultRequest rpcRequest = new DefaultRequest();
rpcRequest.setRequestId(requestId);
rpcRequest.setInterfaceName(interfaceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParamtersDesc(paramtersDesc);
rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
rpcRequest.setAttachments(decodeRequestAttachments(input));
input.close();
return rpcRequest;
}
Aggregations