ObjectInputStream spits out IOException: Cannot read from source stream (persistent internal data)

Started by
0 comments, last by tom_mai78101 11 years, 6 months ago
I'm currently unable to read ObjectInputStream from a FileInputStream object.

Here's the code:

Saving Array (No problems here, but is included for clarity. It works flawlessly.)

[source lang="java"]public void arrayListSave(ArrayList<NewFormat> formats) {
FileOutputStream out;
try {
out = this.openFileOutput("format", Activity.MODE_PRIVATE);
ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(out));
output.writeObject(formats);
output.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}[/source]
Loading Array:




[source lang="java"]public ArrayList<NewFormat> loadArray() {
ArrayList<NewFormat> formats = null;
FileInputStream in;
try {
in = this.openFileInput("format");
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(in));
formats = (ArrayList<NewFormat>) input.readObject();
input.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
Log.d("LoadFile", "Can't load data", e); //Always hit IOException <---------------
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return formats;
}[/source]
I use a TableRow subclass, called NewFormat, to output data as a table. Here's the NewFormat class code:


[source lang="java"]package tt.tt;

import java.io.Serializable;
import android.content.Context;
import android.widget.TableRow;
import android.widget.TextView;

public class NewFormat extends TableRow implements Serializable {
private static final long serialVersionUID = 1L;
TextView test;
TextView string;

public NewFormat(Context c) {
super©;
test = new TextView©;
string = new TextView©;
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
this.addView(test, 0, params);
this.addView(string, 1, params);
}

public void save(int t, String words) {
test.setText(Integer.toString(t));
string.setText(words);
}

public void delete() {
test.getEditableText().clear();
string.getEditableText().clear();
}
}[/source]


The problem is that whenever I hit the line of code:

[source lang="java"]ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(in));[/source]

It always spits out IOException. It's as if the source stream is corrupted or something. But, I did other tests, and it seems the FileOutputStream I have created to save the arrays work flawlessly. I can read the data just fine. It's the problem of loading the arrays back from the stream file.

Does anyone know how to fix this problem? Thanks in advance.
Advertisement
Update:

I fixed this problem.

The actual fix to this is to:

  • Do not implement a Serializable nested class in a class extending from any ViewGroup classes.

I can't emphasize how important it is to not implement a Serializable in a nested class. If the nested class is Serializable, it would also mean the outer class is Serializable. It's a hypothesis I came up with when I realize how crucial it is.

In short, ObjectOutputStreams work as intended, just not for classes with nested Serializables.

This topic is closed to new replies.

Advertisement