ObjectInputStream在字节数组读取期间被破坏

时间:2013-10-11 22:31:34

标签: java

在读取ObjectInputStream时,我似乎遇到了损坏。附加的代码段在完成之前抛出异常。我修复了示例,按照建议调用oos.writeObject(p1)。

异常堆栈如下:

java.lang.OutOfMemoryError: Java heap space
at test.POJO.readExternal(STest.java:82)
at java.io.ObjectInputStream.readExternalData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at test.STest.test(STest.java:37)

我认为这个OutOfMemoryError异常会产生误导。我添加了一个显示readExternal(..)行为的print语句,并且看到从ObjectInputStream中提取了一个大值,这与写入的内容无关。如果DIM设置为5,则设置为15时可以工作。我得到上述异常。如果我降低每个数组元素写入的字节数,我会得到更多成功的迭代。

package test;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class STest
{
    @Test
    public void test() throws Exception
    {
        POJO p1 = new POJO();
        POJO p2 = new POJO();

        // Initialize and serialize POJO 1
        // --------------------------------
        p1.hydrate();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject( p1 );
        oos.flush();
        oos.close();
        byte [] baSerialized = baos.toByteArray();

        // Parse POJO 2
        // -------------
        ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baSerialized ) );
        p2 = (POJO)ois.readObject();

        // Test Result
        // ------------
        byte [][] baa1 = p1._baa;
        byte [][] baa2 = p2._baa;
        for ( int i=0; i < baa1.length; i++ )
        {
            String str1 = new String( baa1[ i ] );
            String str2 = new String( baa2[ i ] );
            assertTrue( str1.equals( str2 ) );
        }
    }
}

class POJO implements Externalizable
{
    protected static final int DIM = 5; 
    protected byte [][] _baa = null;

    public POJO()
    {
    }

    public void hydrate()
    {
        _baa = new byte[ DIM ][];
        for ( int i = 0; i < _baa.length; i++ )
        {
            _baa[ i ] = ("This is a serialize and parse test, it will be interesting to see if it completes without exception, I suspect not as there appears be a bug in the JRE - " + i).getBytes();
        }
    }

    @Override
    public void readExternal( ObjectInput oi ) throws IOException, ClassNotFoundException
    {
        int iDim = oi.readInt();
        _baa = new byte[ iDim ][];      
        for ( int i=0; i < iDim; i++ )
        {
            int iSize = oi.readInt();

            System.out.println( iSize );

            byte [] ba = new byte[ iSize ];
            oi.read( ba );
            _baa[ i ] = ba;
        }
    }

    @Override
    public void writeExternal(  ObjectOutput oo ) throws IOException
    {
        oo.writeInt( _baa.length );
        for ( int i=0; i < _baa.length; i++ )
        {
            oo.writeInt( _baa[ i ].length );
            oo.write( _baa[ i ] );
        }
    }
}

1 个答案:

答案 0 :(得分:1)

p1.writeExternal(o);

应该是:

oo.writeObject(p1);

您不应该直接调用自己的writeExternal()方法。 ObjectOutputStream就是这样做的。