Exception when deserializing an object in debug mode

I am doing something wrong here, I think, but I cannot understand what causes the error. This is my first attempt at custom serialization.

I store objects in a database in a BLOB field. At some point, I read them again.

Here is a serializable class:

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 the sake of simplification mentioned in the comments, additional code is given on how to test it without using a database:

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

Now, if I just run the program, everything seems fine. Job 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) does this method throw exceptions, such as:

 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 quickly, I can reach one of the writeInt() methods, but then get an EOFException . Is something manipulating my ObjectInputStream ?

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

Can someone tell me what is going wrong here?

+6
source share

Source: https://habr.com/ru/post/989612/


All Articles