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

posted @ 2019-07-18 10:00  高圈圈  阅读(268)  评论(0编辑  收藏  举报