Sign in to follow this  
tom_mai78101

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

Recommended Posts

tom_mai78101    693
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. Edited by tom_mai78101

Share this post


Link to post
Share on other sites
tom_mai78101    693
Update:

I fixed this problem.

The actual fix to this is to:[list]
[*]Do not implement a Serializable nested class in a class extending from any ViewGroup classes.
[/list]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this