use of java.io.ObjectOutput in project stream-lib by addthis.
the class TestStreamSummary method testCounterSerialization.
@SuppressWarnings("unchecked")
@Test
public void testCounterSerialization() 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);
}
List<Counter<String>> topK = vs.topK(3);
for (Counter<String> c : topK) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput oo = new ObjectOutputStream(baos);
oo.writeObject(c);
oo.close();
ObjectInput oi = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Counter<String> clone = (Counter<String>) oi.readObject();
assertEquals(c.getCount(), clone.getCount());
assertEquals(c.getError(), clone.getError());
assertEquals(c.getItem(), clone.getItem());
}
}
use of java.io.ObjectOutput 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.ObjectOutput 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.ObjectOutput in project motan by weibocom.
the class CompressRpcCodec method encodeResponse.
/**
* response body 数据:
*
* <pre>
*
* body:
*
* byte[] : serialize (result) or serialize (exception)
*
* </pre>
*
* @param channel
* @param value
* @return
* @throws IOException
*/
private byte[] encodeResponse(Channel channel, Response value) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
byte flag = 0;
output.writeLong(value.getProcessTime());
if (value.getException() != null) {
output.writeUTF(value.getException().getClass().getName());
serialize(output, value.getException(), serialization);
flag = MotanConstants.FLAG_RESPONSE_EXCEPTION;
} else if (value.getValue() == null) {
flag = MotanConstants.FLAG_RESPONSE_VOID;
} else {
output.writeUTF(value.getValue().getClass().getName());
serialize(output, value.getValue(), serialization);
// v2版本可以在response中添加attachment
Map<String, String> attachments = value.getAttachments();
if (attachments != null) {
String signed = attachments.get(ATTACHMENT_SIGN);
String unSigned = attachments.get(UN_ATTACHMENT_SIGN);
// 除了attachment签名外不返回其他信息。
attachments.clear();
if (StringUtils.isNotBlank(signed)) {
attachments.put(ATTACHMENT_SIGN, signed);
}
if (StringUtils.isNotBlank(unSigned)) {
attachments.put(UN_ATTACHMENT_SIGN, unSigned);
}
}
if (attachments != null && !attachments.isEmpty()) {
// 需要回传附加数据
addAttachment(output, attachments);
} else {
// empty attachments
output.writeShort(0);
}
// v2版本flag
flag = MotanConstants.FLAG_RESPONSE_ATTACHMENT;
}
output.flush();
byte[] body = outputStream.toByteArray();
output.close();
Boolean usegz = channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(), URLParamType.usegz.getBooleanValue());
int minGzSize = channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(), URLParamType.mingzSize.getIntValue());
return encode(compress(body, usegz, minGzSize), flag, value.getRequestId());
}
use of java.io.ObjectOutput in project motan by weibocom.
the class DefaultRpcCodec method encodeRequest.
/**
* request body 数据:
*
* <pre>
*
* body:
*
* byte[] data :
*
* serialize(interface_name, method_name, method_param_desc, method_param_value, attachments_size, attachments_value)
*
* method_param_desc: for_each (string.append(method_param_interface_name))
*
* method_param_value: for_each (method_param_name, method_param_value)
*
* attachments_value: for_each (attachment_name, attachment_value)
*
* </pre>
*
* @param request
* @return
* @throws IOException
*/
private byte[] encodeRequest(Channel channel, Request request) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
output.writeUTF(request.getInterfaceName());
output.writeUTF(request.getMethodName());
output.writeUTF(request.getParamtersDesc());
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
if (request.getArguments() != null && request.getArguments().length > 0) {
for (Object obj : request.getArguments()) {
serialize(output, obj, serialization);
}
}
if (request.getAttachments() == null || request.getAttachments().isEmpty()) {
// empty attachments
output.writeInt(0);
} else {
output.writeInt(request.getAttachments().size());
for (Map.Entry<String, String> entry : request.getAttachments().entrySet()) {
output.writeUTF(entry.getKey());
output.writeUTF(entry.getValue());
}
}
output.flush();
byte[] body = outputStream.toByteArray();
byte flag = MotanConstants.FLAG_REQUEST;
output.close();
return encode(body, flag, request.getRequestId());
}
Aggregations