Home Dashboard Directory Help
Search

StreamCorruptedException when deserializing from Java mark supported streams. by aha1


Status: 

Closed
 as External Help for as External


1
0
Sign in
to vote
Type: Bug
ID: 133860
Opened: 6/13/2006 7:18:49 PM
Access Restriction: Public
Primary Feedback Item: 133848
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

You often get a java.io.StreamCorruptedException when deserializing data from a
stream that supports mark and reset: java.io.InputStream.markSupported() == true.
For example java.io.BufferedInputStream.

If you deserialize the serialized data for a J# object through a stream that does not support mark
it works ok. If you deserialize the same data through a stream that does support mark
it fails with the exception below:

java.io.StreamCorruptedException: The Stream is Corrupted
at java.io.ObjectInputStream.invokeMethod(Method m, Object obj, Object[] params)
at java.io.ObjectInputStream.readObject()
at BooleanSerialization.Class1.main(String[] args) in C:\atrees\java_prototypes\dotnet\dotnet20\J# Bugs Test Cases\Serialization Bugs\BooleanSerialization\Class1.jsl:line 90

This problem is new to J# from .NET 2.0 it does not occur with J# from .NET 1.1.

The source for the test case is included below and in the attached Visual Studio project.

package BooleanSerialization;

import java.io.*;

/**
* Demonstrates serialization bug with mark supported streams.
*/
public class Class1
{
    public Class1()
    {
    }

    public static void main(String[] args)
    {
        try
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            FileOutputStream fos = new FileOutputStream("test.ser");
            BufferedOutputStream buf = new BufferedOutputStream(fos);
            ObjectOutputStream oos = new ObjectOutputStream(buf);
            
            test t = new test();
            String before1 = t.toString();
            oos.writeObject(t);
            
            String s = "Hello";
            oos.writeObject(s);
            
            t = null;
            oos.writeObject(t);
            
            oos.writeBoolean(false);
            oos.writeBoolean(false);
            oos.writeObject(new test());
            oos.writeObject(s);
            t = new test();
            String before2 = t.toString();
            oos.writeObject(t);
            oos.close();
            

            // Deserialize with a file input stream.
            // File input stream does NOT support marking and this seems
            // to work fine.
            System.out.println("======= Deserialize without mark supported stream");
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            FileInputStream fis = new FileInputStream("test.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            boolean b;
            t = (test) ois.readObject();
            String after1 = t.toString();
            if (after1.equals(before1) == false)
            {
                System.out.println("Error.\nBefore: " + before1 + "\nAfter: " + after1);
            }
            s = (String)ois.readObject();
            System.out.println(s);
            s = (String)ois.readObject();
            System.out.println(s);
            b = ois.readBoolean();
            System.out.println(b);
            b = ois.readBoolean();
            System.out.println(b);
            t = (test)ois.readObject();
            System.out.println(t);
            s = (String)ois.readObject();
            System.out.println(s);
            t = (test)ois.readObject();
            String after2 = t.toString();
            if (after2.equals(before2) == false)
            {
                System.out.println("Error.\nBefore: " + before2 + "\nAfter: " + after2);
            }
            ois.close();
            System.out.println("======= Deserialize successful");

            // Now do it again.
            // This time however we put the stream in a buffered inputstream.
            // This buffered stream does support marking and the mark support
            // is corrupting the read algorithm and will result in a corrupted stream
            // exception (the same stream we just successfully read in the code
            // above.
            System.out.println("======= Deserialize with mark supported stream");
            bis = new ByteArrayInputStream(bos.toByteArray());
            fis = new FileInputStream("test.ser");
            BufferedInputStream bufis = new BufferedInputStream(fis);
            ois = new ObjectInputStream(bufis);
            
            t = (test)ois.readObject();
            after1 = t.toString();
            if (after1.equals(before1) == false)
            {
                System.out.println("Error.\nBefore: " + before1 + "\nAfter: " + after1);
            }
            s = (String)ois.readObject();
            System.out.println(s);
            s = (String)ois.readObject();
            System.out.println(s);
            b = ois.readBoolean();
            System.out.println(b);
            b = ois.readBoolean();
            System.out.println(b);
            t = (test)ois.readObject();
            System.out.println(t);
            s = (String)ois.readObject();
            System.out.println(s);
            t = (test)ois.readObject();
            after2 = t.toString();
            if (after2.equals(before2) == false)
            {
                System.out.println("Error.\nBefore: " + before2 + "\nAfter: " + after2);
            }
            ois.close();
            System.out.println("======= Deserialize successful");

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        try {
            System.out.println("Enter <CR> to exit:");
            System.in.read();
        }
        catch (IOException e) {
        }
    }
}

public class test implements Serializable
{
    private transient boolean b1;
    private transient boolean b2;
    private transient String msg;
    private transient test2 t2;
    private transient test2 t22;

    private Boolean def_b1;
    private Boolean def_b2;
    private String def_msg;

    public test()
    {
        b1 = false;
        b2 = true;
        msg = "Hello";
        t22 = new test2();
        def_msg = "Internal";
        def_b1 = Boolean.FALSE;
        def_b2 = Boolean.TRUE;
    }

    private void readObject(ObjectInputStream stream) throws IOException,
        ClassNotFoundException
    {
        stream.defaultReadObject();
        msg = (String)stream.readObject();
        t2 = (test2)stream.readObject();
        b1 = stream.readBoolean();
        b2 = stream.readBoolean();
        t22 = (test2)stream.readObject();
    }

    private void writeObject(ObjectOutputStream stream) throws IOException
    {
        stream.defaultWriteObject();

        stream.writeObject(msg);
        stream.writeObject(t2);
        stream.writeBoolean(b1);
        stream.writeBoolean(b2);
        stream.writeObject(t22);
    }

    public String toString()
    {
        return "" + msg + "(" + t2 + ")(" + b1 + ":" + b2 + "(" + t22 + ")" +
            "[" + def_b1 + ":" + def_b2 + "][" + def_msg + "]";
    }
}

public class test2 implements Serializable
{
    public String toString()
    {
        return "test2";
    }
}
Details
Sign in to post a comment.
Sign in to post a workaround.