androidandroid-5.0-lollipopandroid-6.0-marshmallowobjectinputstream

ObjectInputStream java.io.StreamCorruptedException: Wrong format: 0 lollipop and marshmalow


im a beginner at java and android, i get the follow:

"java.io.StreamCorruptedException: Wrong format: 0"

when i try to read back the object. but i only get it on lollipop and marshmallow devices, when i run this at Nougat(7.1) i have no problem at all. any suggestions?

public class MainActivity extends AppCompatActivity {

public final String FILE_NAME = "FILE_NAME";
private List<Item>;
private ReadWriteFunctions readWriteFunctions = new ReadWriteFunctions();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Bitmap defaultBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.no_pic);

    //Read from internal storage
    items =readWriteFunctions.readObj(this,FILE_NAME);
    if (items == null){//if the file don't exist it returns null
        items = new ArrayList<>();
        items.add(new Item(false,"",defaultBitmap));
        Log.d("readObj", "Created new list");
    }

Class code:

public class ReadWriteFunctions {


public ArrayList<Item> readObj(Context context, String fileName) { 
    ArrayList<Item> arrayList;
    try {
        FileInputStream fis = context.openFileInput(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        arrayList = (ArrayList<Item>) ois.readObject();
        ois.close();
        return arrayList;
    }catch (IOException | ClassNotFoundException e){
        e.printStackTrace();
        return null;
    }
}

public void writObj(Context context,String fileName,Object obj) {
    try {
        FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.close();
        Log.d("writObj", "Saved");
    }catch (IOException e){
        e.printStackTrace();
        Log.d("writObj", "Save Fault");
    }

}

}

public class Item implements Serializable{

private boolean itemChecked;
private String itemText;
private transient Bitmap itemPicture;


public Item(Item item) {//Constructor that receive item
    this.setItemChecked(item.isItemChecked());
    this.setItemText(item.getItemText());
    this.setItemPicture(item.getItemPicture());

}

public Item(boolean itemChecked, String itemText, Bitmap itemPicture) {
    this.itemChecked = itemChecked;
    this.itemText = itemText;
    this.itemPicture = itemPicture;
}


public boolean isItemChecked() {
    return itemChecked;
}

public void setItemChecked(boolean itemChecked) {
    this.itemChecked = itemChecked;
}

public String getItemText() {
    return itemText;
}

public void setItemText(String itemText) {
    this.itemText = itemText;
}

public Bitmap getItemPicture() {
    return itemPicture;
}

public void setItemPicture(Bitmap itemPicture) {
    this.itemPicture = itemPicture;
}



private void writeObject(java.io.ObjectOutputStream out) throws IOException {

    itemPicture.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.defaultWriteObject();

}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

    itemPicture = BitmapFactory.decodeStream(in);
    in.defaultReadObject();

}

}

the error:

09-08 12:38:53.607 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err: java.io.StreamCorruptedException: Wrong format: 0
09-08 12:38:53.607 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:675)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:788)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1113)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:454)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at milshtein.yaacov.advancedshoppinglist.Item.readObject(Item.java:70)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1330)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.util.ArrayList.readObject(ArrayList.java:661)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1330)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
09-08 12:38:53.608 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
09-08 12:38:53.609 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
09-08 12:38:53.609 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
09-08 12:38:53.614 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at milshtein.yaacov.advancedshoppinglist.WriteReadInternalStorage.ReadWriteFunctions.readObj(ReadWriteFunctions.java:29)
09-08 12:38:53.617 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at milshtein.yaacov.advancedshoppinglist.MainActivity.onCreate(MainActivity.java:56)
09-08 12:38:53.617 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.Activity.performCreate(Activity.java:5990)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
09-08 12:38:53.618 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-08 12:38:53.621 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.os.Looper.loop(Looper.java:135)
09-08 12:38:53.622 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
09-08 12:38:53.622 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-08 12:38:53.622 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
09-08 12:38:53.622 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-08 12:38:53.622 15576-15576/milshtein.yaacov.advancedshoppinglist W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Solution

  • just posting the way i solve the problem for anyone who get the same issue in the future.

    at:

    public class Item implements Serializable
    

    i changed:this line:

    private boolean itemChecked;
    

    to this line:

    private Boolean itemChecked;
    

    as i know primitives as boolean are Serializable so i dont know why it made a difference, also i don't understand why it worked on Nougat and didn't work for lollipop & marshmallow. but its working good now. if someone knows why, i be happy to hear.