我猜这里做错了,但我无法弄清楚导致错误的原因。这是我第一次尝试自定义序列化。
我将对象存储在BLOB字段中的数据库中。在某些时候我再次阅读它们。
这是可序列化的类:
public class Job extends Observable implements Serializable {
private static final long serialVersionUID = 7530757972497270533L;
private Path file;
private String errorText = "";
private int moduleID;
private int jobID;
private JobStatus status;
public Job(int moduleID, Path file) {
this.moduleID = moduleID;
this.file = file;
status = JobStatus.ACCEPTED;
}
public Path getFile() {
return file;
}
public void setFile(Path file) {
setChanged();
this.file = file;
notifyObservers(this);
}
public int getModuleID() {
return moduleID;
}
public void setModuleID(int moduleID) {
setChanged();
this.moduleID = moduleID;
notifyObservers(this);
}
public void setStatus(JobStatus status) {
setChanged();
this.status = status;
notifyObservers(this);
}
public JobStatus getStatus() {
return status;
}
public void setID(int jobID) {
this.jobID = jobID;
}
public int getID() {
return jobID;
}
public void setErrorText(String errorText) {
this.errorText = errorText;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeUTF(file.toString());
out.writeUTF(errorText);
out.writeInt(moduleID);
out.writeInt(jobID);
out.writeInt(status.ordinal());
}
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
try {
file = Paths.get(in.readUTF());
errorText = in.readUTF();
moduleID = in.readInt();
jobID = in.readInt();
status = JobStatus.values()[in.readInt()];
} catch (Exception e) { //just for debugging purposes
e.printStackTrace();
}
}
@Override
public String toString() {
return "moduleID=" + moduleID + ", jobID=" + jobID;
}
}
出于简化的原因,这里的评论中提到了如何在不使用数据库的情况下测试它的附加代码:
public enum JobStatus {
ACCEPTED, PROCESSING, FINISHED, ERROR
}
public class Test {
public static void main(String[] args) {
try {
Job job = new Job(1, Paths.get("C:/temp"));
Job job2 = deepCopy(job);
System.out.println(job2.getFile());
} catch (Exception e) {
e.printStackTrace();
}
}
public static Job deepCopy(Job job) throws Exception {
// Serialization of object
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(job);
// De-serialization of object
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
Job copied = (Job) in.readObject();
return copied;
}
}
现在如果我让程序运行一切似乎都很好。 Job
已创建,我可以使用它。但是在某些时候我想调试readObject()
中的Job
方法。仅在调试模式下(在Netbeans中),此方法抛出以下异常:
java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2823)
at java.io.ObjectInputStream.readInt(ObjectInputStream.java:972)
at test.Job.readObject(Job.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at test.Test.deepCopy(Test.java:40)
at test.Test.main(Test.java:23)
readObject()
方法抛出此异常。如果我点击非常快,我可以使用其中一个writeInt()
方法,但后来我得到EOFException
。有什么东西操纵我的ObjectInputStream
吗?
我无法使用Eclipse中完全相同的代码重现相同的错误。
有谁能告诉我这里出了什么问题?