Search code examples
javaclientvoice-recordingoptionaldataexception

OptionalDataException Java


I am making a voice chat program and I got the OptionalDataException error and I never had this problem with the code before I added voice. The voice communication is handles by a different socket so I don't see the problem.

Code:

    public class Client implements Runnable {                                                   // CLIENT
    private String msg;
    public void run() {
        try {
            s1 = new Socket(ipAddress, port);
            s2 = new Socket(ipAddress, 1210);
            o1 = new ObjectOutputStream(s1.getOutputStream());
            o1.writeObject(name);
            serverListModel.addElement(name);
            i1 = new ObjectInputStream(s1.getInputStream());
            Thread voice = new Thread(new ClientAudio());
            voice.start();
            while(true) {
                msg = (String) i1.readObject();
                    String[] namePart = msg.split("-");
                    if(namePart[0].equals("AddName") && !namePart[1].equals(name) && !serverListModel.contains(namePart[1])) {
                        serverListModel.addElement(namePart[1]);
                    }
                    if(namePart[0].equals("RemoveName") && !namePart[1].equals(name)) {
                        serverListModel.removeElement(namePart[1]);
                    }
                    if(!msg.equals(null) && !namePart[0].equals("AddName") && !namePart[0].equals("RemoveName")) {
                        chatWindow.append(msg+"\n");
                    }
                }
        } catch (IOException | ClassNotFoundException e) {
            chatWindow.append("Server Closed");
            e.printStackTrace();
            try {
                s1.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            mainWindow(true);
        }
    }
}


flag

it was thrown at msg = (String) i1.readObject(); and it says

java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1361)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at client.chat$Client.run(chat.java:319)
at java.lang.Thread.run(Thread.java:745)

Solution

  • From Oracle:

    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.

    It would appear that the next object in the Stream is not a String.

    Is the server code under your control? Or do you at least have the source? If so, verify that String objects are the only ones being transmitted, or adjust your code to handle the actual objects/primitives being sent.

    Edit

    From your other question Voice Server not working:

    byte[] soundData = 
    //...
    o.write(soundData, 0, bytesRead);
    

    ... It looks like you are not writing String objects to the ObjectOutputStream. Actually, not even writing an Object, but raw bytes. You have to read data the same way you write it; anything else just won't work.