我正在获得一个异常块
Caused by: java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2353)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3092)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2892)
at java.io.ObjectInputStream.readString(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1344)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
我在反序列化String对象时遇到此错误。 序列化代码:
public <T> String serialize(T object) throws SerializationException
{
try {
if (object == null) {
return null;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream;
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
objectOutputStream.close();
String serializedString =
new String(base64.encode(byteArrayOutputStream.toByteArray()), Charset.forName("UTF-8"));
return serializedString;
}
catch (Exception e) {
throw new SerializationException(SERIALIZATION_FAILURE, e);
}
}
反序列化代码
public <T> T deserialize(String string, Class<T> classType)
throws SerializationException
{
try {
if (StringUtils.isBlank(string)) {
return null;
}
byte[] bytesOfString = base64.decode(string.getBytes(Charset.forName("UTF-8")));
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytesOfString);
ObjectInputStream objectInputStream;
objectInputStream = new ObjectInputStream(byteArrayInputStream);
T deserializedObject = (T) objectInputStream.readObject();
objectInputStream.close();
return deserializedObject;
}
catch (Exception e) {
throw new SerializationException(SERIALIZATION_FAILURE, e);
}
当我使用CLASSNAME.class反序列化任何pojo时,它正确地发生但当我尝试使用classtype String.class反序列化String类时,它会抛出此异常。