java newio,运行方法中是否存在java.io.OptionalDataException?

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();

}

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值