zoukankan      html  css  js  c++  java
  • Java——序列化 反序列化

    记录一下:

    先粘两个比较繁琐的方法:

    put:

    public void putSerializableObject(String key, Object value, int expireTime) {
            key = preProcessKey(key);
            ByteArrayOutputStream byteArrayOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
            try {
                byteArrayOutputStream = new ByteArrayOutputStream();
                objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(value);
                byte[] bytes = byteArrayOutputStream.toByteArray();
                cluster.add(key, expireTime, bytes);
            } catch (Exception e) {
                logger.warn("Memcache put() failed! key=" + key, e);
            } finally {
                if (byteArrayOutputStream != null) {
                    try {
                        byteArrayOutputStream.close();
                    } catch (IOException e) {
                        logger.warn("ByteArrayOutputStream close() failed! key=" + key + e);
                    }
                }
                if (objectOutputStream != null) {
                    try {
                        objectOutputStream.close();
                    } catch (IOException e) {
                        logger.warn("ObjectOutputStream close() failed! key=" + key + e);
                    }
                }
            }
        }

    get:

    public <T> T getSerializableObject(String key, Class<T> clazz) {
            key = preProcessKey(key);
            T result = null;
            ByteArrayInputStream byteArrayInputStream = null;
            ObjectInputStream objectInputStream = null;
            try {
                byte[] resultByte = (byte[]) cluster.get(key);
                if (null != resultByte) {
                    byteArrayInputStream = new ByteArrayInputStream(resultByte);
                    objectInputStream = new ObjectInputStream(byteArrayInputStream);
                    result = (T) objectInputStream.readObject();
                }
            } catch (Exception e) {
                logger.warn("Memcache get() failed! key=" + key, e);
                return null;
            } finally {
                if (objectInputStream != null) {
                    try {
                        objectInputStream.close();
                    } catch (IOException e) {
                        logger.warn("ObjectInputStream close() failed ! key=" + key + e);
                    }
                }
                if (byteArrayInputStream != null) {
                    try {
                        byteArrayInputStream.close();
                    } catch (IOException e) {
                        logger.warn("ByteArrayInputStream close() failed ! key=" + key + e);
                    }
                }
            }
            return result;
        }

     重点!

    上面两个方法,有冗余的代码,可以进一步简化:

      序列化:

        public static byte[] serialize(Object object) throws IOException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
                oos.writeObject(object);
                return baos.toByteArray();
            }
        }

      反序列化:

        public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            try (ObjectInputStream ois = new ObjectInputStream(bais)) {
                return ois.readObject();
            }
        }

    由于 InputStream继承了Closeable,当在try-cache中使用流的时候,会在执行结束try-cache后自动调用close方法,无论是否抛出异常,代码简洁多了,很棒棒哦 (*^▽^*)~

  • 相关阅读:
    GlusterFS-分布式存储集群部署
    keepalived+HAproxy集群部署
    LB-HAproxy负载均衡部署
    Pacemaker高可用环境实践
    Nginx-负载均衡部署
    LB-LVS常见模式NAT/DR部署
    HTTPS原理、应用
    LDAP-autofs挂载用户验证
    GPG-非对称加密
    大数据入门学习(Linux)
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/11205305.html
Copyright © 2011-2022 走看看