java.io.EOFException[java.io.ObjectInputStream]

详细的错误信息:

16:53:20.132 [main] ERROR com.uet.common.exception.SerializeException - Caught IOException decoding 580 bytes of data

java.io.EOFException: null

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2576) ~[na:1.6.0_45]

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1295) ~[na:1.6.0_45]

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349) ~[na:1.6.0_45]

at com.uet.common.serialize.ListTranscoder.deserialize(ListTranscoder.java:25) ~[classes/:na]

at com.uet.common.cache.RedisTest.main(RedisTest.java:44) [classes/:na]

Exception in thread "main" com.uet.common.exception.SerializeException: Caught IOException decoding 580 bytes of data

at com.uet.common.serialize.ListTranscoder.deserialize(ListTranscoder.java:37)

at com.uet.common.cache.RedisTest.main(RedisTest.java:44)

Caused by: java.io.EOFException

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2576)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1295)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)

at com.uet.common.serialize.ListTranscoder.deserialize(ListTranscoder.java:25)

... 1 more



LIST序列化与反序列化的代码如下:

package com.uet.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.uet.common.exception.SerializeException;

public class ListSerializeUtil {
	@SuppressWarnings("unchecked")
	public static <T> List<T> deserialize(Class<T> resultClass,byte[] in) {
		List<T> list = new ArrayList<T>();
		ByteArrayInputStream bis = null;
		ObjectInputStream is = null;
		try {
			if (in != null) {
				bis = new ByteArrayInputStream(in);
				is = new ObjectInputStream(bis);
				while (true) {
					T m = (T) is.readObject();
					if (m == null) {
						break;
					}

					list.add(m);

				}
				is.close();
				bis.close();
			}
		} catch (IOException e) {
			throw new SerializeException(String.format("Caught IOException decoding %d bytes of data", in == null ? 0 : in.length) + e);
		} catch (ClassNotFoundException e) {
			throw new SerializeException(String.format("Caught CNFE decoding %d bytes of data", in == null ? 0 : in.length) + e);
		} finally {
			close(is);
			close(bis);
		}

		return list;
	}
	public static <T> byte[] serialize(List<T> values) {
		if (values == null)
			throw new NullPointerException("Can't serialize null"); 
		byte[] results = null;
		ByteArrayOutputStream bos = null;
		ObjectOutputStream os = null;
		try {
			bos = new ByteArrayOutputStream();
			os = new ObjectOutputStream(bos);
			for (T m : values) {
				os.writeObject(m);
			} 
			os.close();
			bos.close();
			results = bos.toByteArray();
		} catch (IOException e) {
			throw new IllegalArgumentException("Non-serializable object", e);
		} finally {
			close(os);
			close(bos);
		}

		return results;
	}
	public static void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (Exception e) {
				throw new SerializeException(e);
			}
		}
	}
}



请高手解答


爱追问
分享到:
0

ListSerializeUtil 的58行后,加一个

os.writeObject(null);
避免EOFException

//你可以通过序列化的时候写入一个null对象来避免EOF异常

ObjectOutputStream out = new ObjectOutputStream
(new BufferedOutputStream(
new FileOutputStream(PathConfig.dictionaryObjectPath)));
out.writeObject(obj);	
out.writeObject(null);//避免EOFException

out.flush();
out.close();




2

用户hessian的时候,要注意与jdk 版本相对应,否则也会报这个错。

hessian3.5与jdk6对应。

hessian4与jdk7对应

0
共 2 条   当前1/1页

© 2014 究问社区 copyRight 豫ICP备13003319号-1