序列化

来源:互联网 发布:网络摄像机ip设置方法 编辑:程序博客网 时间:2024/06/05 00:09

List对象序列化,放在这里,下次可能用到

ListTranscoder类

package com.lrs.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;/** *    List 转换器 * @author lrs * * @param <M>  */public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {    /**     * 解码 反序列化     */      @SuppressWarnings("unchecked")      public List<M> deserialize(byte[] in) {        List<M> list = new ArrayList<>();        ByteArrayInputStream bis = null;        ObjectInputStream is = null;        System.out.println("in.leng"+in.length);        try {          if (in != null && in.length > 1) {            bis = new ByteArrayInputStream(in);            if(bis != null){                is = new ObjectInputStream(bis);                M m =null;                while ((m = (M)is.readObject()) != null) {                    list.add(m);                }                is.close();                bis.close();            }          }        } catch (IOException e) {            //e.printStackTrace();      } catch (ClassNotFoundException e) {          e.printStackTrace();      }  finally {          close(is);          close(bis);        }        return  list;      }      /**       * 编码 序列化       */      @SuppressWarnings("unchecked")      @Override      public byte[] serialize(Object value) {        if (value == null)          throw new NullPointerException("Can't serialize null");        List<M> values = (List<M>) value;        byte[] results = null;        ByteArrayOutputStream bos = null;        ObjectOutputStream os = null;        try {          bos = new ByteArrayOutputStream();          os = new ObjectOutputStream(bos);          for (M m : values) {            os.writeObject(m);          }          // os.writeObject(null);          os.close();          bos.close();          results = bos.toByteArray();        } catch (IOException e) {          throw new IllegalArgumentException("Non-serializable object", e);        } finally {          close(os);          close(bos);        }        return results;      }    }

SerializeTranscoder类

package com.lrs.util;import java.io.Closeable;import org.apache.log4j.Logger;public abstract class SerializeTranscoder {      protected static Logger logger = Logger.getLogger(SerializeTranscoder.class);      public abstract byte[] serialize(Object value);      public abstract Object deserialize(byte[] in);      public void close(Closeable closeable) {        if (closeable != null) {          try {            closeable.close();          } catch (Exception e) {             logger.info("Unable to close " + closeable, e);           }        }      }    }