Karlisky
Karlisky

Reputation: 1

java.io.OptionalDataException in run method?

Why I receive java.io.OptionalDataException in the run method?

ObjectInputStream reads items & users couple times, then it crashes with OptionalDataException. I think it relates EOFException.

It prints a message when new socket created It print the value of objectInputStream It also prints the message

STACK TRACE + MESSAGES

New socket created
New socket created
New socket created
Here is ObjectInputStream :java.io.ObjectInputStream@1430b5c
Here is message :REA:ITEM:
Here is ObjectInputStream :java.io.ObjectInputStream@1decdec <
Here is message :ADD:ITEM:
New socket created
New socket created
Here is ObjectInputStream :java.io.ObjectInputStream@b2fd8f
Here is message :REA:ITEM:
New socket created
java.io.OptionalDataException
                   at java.io.ObjectInputStream.readObject0(Unknown Source)
                   at java.io.ObjectInputStream.readObject(Unknown Source)
                   at fedaih.SocketTask.run(SocketTask.java:55)
                   at java.lang.Thread.run(Unknown Source)
New socket created
Here is ObjectInputStream :java.io.ObjectInputStream@1c39a2d
Here is message :REA:USER:
Here is ObjectInputStream :java.io.ObjectInputStream@13bad12
Here is message :ADD:USER:
New socket created
New socket created
Here is ObjectInputStream :java.io.ObjectInputStream@eb7859
Here is message :REA:USER:
java.io.OptionalDataException
                   at java.io.ObjectInputStream.readObject0(Unknown Source)
                   at java.io.ObjectInputStream.readObject(Unknown Source)
                   at fedaih.SocketTask.run(SocketTask.java:55)
                   at java.lang.Thread.run(Unknown Source)

public void run() {
        
    try {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        User user = null;
        Item item = null;
        String upc = null;
        String username = null;
        
        // This is the line where the error occurs
        String message = (String) objectInputStream.readObject();
        switch (message) {
        
            case "REA:ITEM:":   upc = (String) objectInputStream.readObject();
                                item = inventoryFileManager.readItem(upc);
                                objectOutputStream.writeObject(item);
                                objectOutputStream.flush();
                                break;
                                
            case "REA:USER:":   username = (String) objectInputStream.readObject();
                                user = userFileManager.readUser(username);
                                objectOutputStream.writeObject(user);
                                objectOutputStream.flush();
                                break;
            
            case "REM:ITEM:":   upc = (String) objectInputStream.readObject();
                                inventoryFileManager.removeItem(upc);
                                break;
                                
            case "REM:USER:":   username = (String) objectInputStream.readObject();
                                userFileManager.removeUser(username);
                                break;
            
            case "ADD:ITEM:":   item = (Item) objectInputStream.readObject();
                                if (item != null) {
                                        inventoryFileManager.addItem(item);
                                }
                                break;
                                
            case "ADD:USER:":   user = (User) objectInputStream.readObject();
                                if (user != null) {
                                        userFileManager.addUser(user);
                                }
                                break;
                        
            case "UPD:ITEM:":   item = (Item) objectInputStream.readObject();
                                inventoryFileManager.updateItem(item);
                                break;
                
            case "UPD:USER:":   user = (User) objectInputStream.readObject();
                                userFileManager.updateUser(user);
                                break;
                                
            case "ALL:ITEM:":   objectOutputStream.writeObject(inventoryFileManager.readAllItems());
                                objectOutputStream.flush();
                                break;
                
            case "ALL:USER:":   objectOutputStream.writeObject(userFileManager.readAllUsers());
                                objectOutputStream.flush();
        }
        objectOutputStream.close();
        objectInputStream.close();
        
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (PointOfSaleException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 5980

Answers (1)

Stephen C
Stephen C

Reputation: 719229

The javadoc explains the exception as follows:

Exception indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream. This exception may be thrown in two cases:

  • An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException's length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.
  • An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. In this case, the OptionalDataException's eof field is set to true, and the length field is set to 0.

In short, there is a mismatch between what was written to the socket and what your code is attempting to read. To get to the bottom of this, you need to:

  • find out what the values of eof and length in the exception are, and
  • tie down the circumstances under which the failure is occuring.

Then, compare your code against the code that writes the objects and (using the evidence above) figure out what the mismatch is.

Upvotes: 2

Related Questions