Reputation: 329
I need to save the last 10 IPs that was assigned to device. I'm storing these IPs in a list and serializing that list. when a new IP Address is assigned to device, i need to update the saved serialized list.
however i have a problem. My app randomly keeps throwing exceptions. Sometimes EOFException, Sometimes StreamCorruptedException and ...
I use apache commons lang to serialize my list:
private static void serialize(Context context, List<IP> ipList){
try {
SerializationUtils.serialize((Serializable) ipList, new FileOutputStream(context.getFilesDir() + "/history.ser"));
} catch (SerializationException |IOException e) {
e.printStackTrace();
}
}
and i read it using this:
public static List<IP> readHistoryFile(Context context) {
File historyFile = new File(context.getFilesDir() + "/history.ser");
if (!historyFile.exists() || historyFile.length() == 0) {
return new ArrayList<>(10);
}
List<IP> list = null;
try {
list = SerializationUtils.deserialize(new FileInputStream(context.getFilesDir() + "/history.ser"));
} catch ( SerializationException |IOException e) {
e.printStackTrace();
}
return list;
and when i need to update this list, i call this method:
public static void addToHistory(Context context, IP ip) {
if (ip != null) {
if (ip.getIp() != null) {
List<IP> list = readHistoryFile(context);
add(ip, list); //add a new object to the list that i read
serialize(context, list);
}
}
}
This works sometimes. But not sometimes. for example, sometimes i get a NullPointerException because there was an EOFException thrown. This is usually what i get:
W/System.err: org.apache.commons.lang3.SerializationException: java.io.EOFException
W/System.err: at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:197)
at com.user.ip.HistoryManager.readHistoryFile(HistoryManager.java:51)
W/System.err: at com.user.ip.HistoryManager.addToHistory(HistoryManager.java:34)
at com.user.ip.NetworkActivityService.lambda$checkAndNotify$1$NetworkActivityService(NetworkActivityService.java:131)
at com.user.ip.-$$Lambda$NetworkActivityService$B5vDemM0IE9Rlkz9-oVaXjnp3b0.run(Unknown Source:2)
at java.lang.Thread.run(Thread.java:919)
W/System.err: Caused by: java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2751)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1378)
W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at java.util.ArrayList.readObject(ArrayList.java:791)
at java.lang.reflect.Method.invoke(Native Method)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1066)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2013)
W/System.err: at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1899)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1412)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:194)
... 5 more
Upvotes: 0
Views: 192
Reputation: 127
In what order the parts are called.
Could it be possible that the serialization of the file history.ser (your code part 1) has not ended when the code (code part 2) trying to access the file.
new FileOutputStream(context.getFilesDir() + "/history.ser")
Is creating a "new" file and this file is empty in the beginning.
So you are getting an error if you try to read from it while the file is created.
That can be the reason why your code it is working sometimes and sometimes not.
Upvotes: 1