Search in sources :

Example 16 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 17 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 18 with ObjectInput

use of java.io.ObjectInput in project azure-tools-for-java by Microsoft.

the class ApplicationInsightsPreferences method loadPreferences.

/**
	 * Read and load preference file data.
	 * Converts byte array format data to list of application insights resources.
	 */
private void loadPreferences() {
    Preferences prefs = PluginUtil.getPrefs(PREF_FILE);
    try {
        byte[] data = prefs.getByteArray(PREF_KEY, null);
        if (data != null) {
            ByteArrayInputStream buffer = new ByteArrayInputStream(data);
            ObjectInput input = new ObjectInputStream(buffer);
            try {
                ApplicationInsightsResource[] resources = (ApplicationInsightsResource[]) input.readObject();
                for (ApplicationInsightsResource resource : resources) {
                    if (!ApplicationInsightsResourceRegistry.getAppInsightsResrcList().contains(resource)) {
                        ApplicationInsightsResourceRegistry.getAppInsightsResrcList().add(resource);
                    }
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
        Activator.getDefault().log(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) Preferences(org.osgi.service.prefs.Preferences) ObjectInputStream(java.io.ObjectInputStream)

Example 19 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 20 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)26 ObjectInputStream (java.io.ObjectInputStream)18 IOException (java.io.IOException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ObjectOutput (java.io.ObjectOutput)10 InputStream (java.io.InputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 FileInputStream (java.io.FileInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 RemoteException (java.rmi.RemoteException)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 File (java.io.File)3 MarshalException (java.rmi.MarshalException)3 UnmarshalException (java.rmi.UnmarshalException)3 Test (org.junit.Test)3 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServerError (java.rmi.ServerError)2