Search in sources :

Example 51 with ObjectOutput

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());
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 52 with ObjectOutput

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());
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 53 with ObjectOutput

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
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) InitialContext(javax.naming.InitialContext) WildFlyDataSource(org.jboss.as.connector.subsystems.datasources.WildFlyDataSource) DataSource(javax.sql.DataSource) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 54 with ObjectOutput

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());
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 55 with ObjectOutput

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());
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ObjectOutput (java.io.ObjectOutput)77 ObjectOutputStream (java.io.ObjectOutputStream)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 IOException (java.io.IOException)33 ObjectInput (java.io.ObjectInput)25 Test (org.junit.Test)20 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 WorkingMemory (org.drools.core.WorkingMemory)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 OutputStream (java.io.OutputStream)6 ClassObjectType (org.drools.core.base.ClassObjectType)6