Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

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

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 asperatology   Members   


Posted 21 September 2012 - 11:43 PM

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, 21 September 2012 - 11:48 PM.

#2 asperatology   Members   


Posted 24 September 2012 - 05:27 AM


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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.