List反序列化之java.io.EOFException

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); 
          }
        }
      }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值