Exception while deserializing object in Debug Mode

I am doing something wrong here I guess, but I can't figure out what causes the error. It's my first try at custom serialization.

I am storing objects in a database in a BLOB field. At some point I read them again.

Here is the class that is serializable:

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; } }

For reasons of simplification mentioned in the comments here is the additional code on how to test it without the use of a database:


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; } }

Now if I just let the program run everything seems fine. The Job gets created and I can work with it. But at some point I wanted to debug the readObject() method in Job. Only in debug mode (in Netbeans) this method throws Exceptions like the following:

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)

This exception is thrown in the readObject() method. If I click very fast I can reach one of the writeInt() methods but then I get an EOFException. Does something manipulate my ObjectInputStream?

I cannot reproduce the same error with the exact same code in Eclipse.

Can anybody tell me what's going wrong here?

Category:java Views:4 Time:2019-01-04

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.085 (s). 11 q(s)