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方法,无论是否抛出异常,代码简洁多了,很棒棒哦 (*^▽^*)~

转载于:https://www.cnblogs.com/gaoquanquan/p/11205305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值