title: java.io.OptionalDataException in run method?
body: <p>Why I receive <code>java.io.OptionalDataException</code> in the run method?</p>
  <p>ObjectInputStream reads items &amp; users couple times, then it crashes with <code>OptionalDataException</code>. I think it relates <code>EOFException</code>.</p>
  <p>It prints a message when new socket created
  It print the value of objectInputStream
  It also prints the message</p>
  <h3>STACK TRACE + MESSAGES <br/></h3>
  <p>New socket created <br/>
  New socket created <br/>
  New socket created <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@1430b5c <br/>
  Here is message :REA:ITEM: <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@1decdec &lt;<br/>
  Here is message :ADD:ITEM: <br/>
  New socket created <br/>
  New socket created <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@b2fd8f <br/>
  Here is message :REA:ITEM: <br/>
  New socket created <br/>
  <strong>java.io.OptionalDataException</strong> <br/>
                    
  at java.io.ObjectInputStream.readObject0(Unknown Source) <br/>
                    
  at java.io.ObjectInputStream.readObject(Unknown Source) <br/>
                    
  at fedaih.SocketTask.run(SocketTask.java:55) <br/>
                    
  at java.lang.Thread.run(Unknown Source) <br/>
  New socket created <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@1c39a2d <br/>
  Here is message :REA:USER: <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@13bad12 <br/>
  Here is message :ADD:USER: <br/>
  New socket created <br/>
  New socket created <br/>
  Here is ObjectInputStream :java.io.ObjectInputStream@eb7859 <br/>
  Here is message :REA:USER: <br/>
  <strong>java.io.OptionalDataException</strong> <br/>
                    
  at java.io.ObjectInputStream.readObject0(Unknown Source) <br/>
                    
  at java.io.ObjectInputStream.readObject(Unknown Source) <br/>
                    
  at fedaih.SocketTask.run(SocketTask.java:55) <br/>
                    
  at java.lang.Thread.run(Unknown Source) <br/></p>
  <pre><code>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 &quot;REA:ITEM:&quot;:   upc = (String) objectInputStream.readObject();
                                  item = inventoryFileManager.readItem(upc);
                                  objectOutputStream.writeObject(item);
                                  objectOutputStream.flush();
                                  break;
                                  
              case &quot;REA:USER:&quot;:   username = (String) objectInputStream.readObject();
                                  user = userFileManager.readUser(username);
                                  objectOutputStream.writeObject(user);
                                  objectOutputStream.flush();
                                  break;
              
              case &quot;REM:ITEM:&quot;:   upc = (String) objectInputStream.readObject();
                                  inventoryFileManager.removeItem(upc);
                                  break;
                                  
              case &quot;REM:USER:&quot;:   username = (String) objectInputStream.readObject();
                                  userFileManager.removeUser(username);
                                  break;
              
              case &quot;ADD:ITEM:&quot;:   item = (Item) objectInputStream.readObject();
                                  if (item != null) {
                                          inventoryFileManager.addItem(item);
                                  }
                                  break;
                                  
              case &quot;ADD:USER:&quot;:   user = (User) objectInputStream.readObject();
                                  if (user != null) {
                                          userFileManager.addUser(user);
                                  }
                                  break;
                          
              case &quot;UPD:ITEM:&quot;:   item = (Item) objectInputStream.readObject();
                                  inventoryFileManager.updateItem(item);
                                  break;
                  
              case &quot;UPD:USER:&quot;:   user = (User) objectInputStream.readObject();
                                  userFileManager.updateUser(user);
                                  break;
                                  
              case &quot;ALL:ITEM:&quot;:   objectOutputStream.writeObject(inventoryFileManager.readAllItems());
                                  objectOutputStream.flush();
                                  break;
                  
              case &quot;ALL:USER:&quot;:   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();
      }
  }
  </code></pre>