Search in sources :

Example 26 with ObjectInput

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());
}
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 27 with ObjectInput

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 28 with ObjectInput

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
        }
    }
}
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 29 with ObjectInput

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;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ObjectInput(java.io.ObjectInput)

Example 30 with ObjectInput

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;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput)

Aggregations

ObjectInput (java.io.ObjectInput)45 IOException (java.io.IOException)31 ObjectOutput (java.io.ObjectOutput)25 ObjectInputStream (java.io.ObjectInputStream)20 ByteArrayInputStream (java.io.ByteArrayInputStream)16 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 Test (org.junit.Test)9 ObjectOutputStream (java.io.ObjectOutputStream)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 InputStream (java.io.InputStream)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 FileInputStream (java.io.FileInputStream)6