为什么我java.io.OptionalDataException
在 run 方法中收到?
ObjectInputStream 多次读取项目和用户,然后它与OptionalDataException
. 我认为这有关EOFException
。
创建新套接字时打印一条消息 它打印 objectInputStream 的值 它还打印消息
堆栈跟踪 + 消息
已创建新套接字 已
创建新套接字 已创建
新套接字
这里是 ObjectInputStream :java.io.ObjectInputStream@1430b5c
这里是消息 :REA:ITEM:
这里是 ObjectInputStream :java.io.ObjectInputStream@1decdec <
这里是消息 :ADD:ITEM:
新套接字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。
在 fedaih.SocketTask.run(SocketTask.java:55) 处读取对象(未知来源)
在 java.lang.Thread.run(Unknown Source)
创建了新套接字
这里是 ObjectInputStream :java.io.ObjectInputStream@1c39a2d
这里是消息 :REA:USER:
这里是 ObjectInputStream :java.io.ObjectInputStream@13bad12
这里是消息 :ADD :USER:
New socket created
New socket created
Here is ObjectInputStream :java.io.ObjectInputStream@eb7859
这是消息 :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)
在 java.lang.Thread.run(未知来源)
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();
}
}