Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectInputStream在字节数组读取期间损坏_Java - Fatal编程技术网

Java ObjectInputStream在字节数组读取期间损坏

Java ObjectInputStream在字节数组读取期间损坏,java,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时,我似乎受到了损坏。附加的代码段在完成之前引发异常。我修复了这个示例,按照建议调用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 ] );
        }
    }
}
封装测试;
导入静态org.junit.Assert.*;
导入java.io.ByteArrayInputStream;
导入java.io.ByteArrayOutputStream;
导入java.io.Externalizable;
导入java.io.IOException;
导入java.io.ObjectInput;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutput;
导入java.io.ObjectOutputStream;
导入org.junit.Test;
公共级STest
{
@试验
public void test()引发异常
{
POJO p1=新的POJO();
POJO p2=新的POJO();
//初始化并序列化POJO 1
// --------------------------------
p1.水合物();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
ObjectOutputStream oos=新的ObjectOutputStream(BAS);
oos.writeObject(p1);
oos.flush();
oos.close();
字节[]baSerialized=bas.toByteArray();
//解析POJO2
// -------------
ObjectInputStream ois=新的ObjectInputStream(新的ByteArrayInputStream(baSerialized));
p2=(POJO)ois.readObject();
//测试结果
// ------------
字节[][]baa1=p1.\u-baa;
字节[][]baa2=p2.\u-baa;
对于(int i=0;i<1.length;i++)
{
字符串str1=新字符串(baa1[i]);
字符串str2=新字符串(baa2[i]);
assertTrue(str1.equals(str2));
}
}
}
类POJO实现了外部化
{
受保护静态最终int DIM=5;
受保护字节[][].\u baa=null;
公共POJO()
{
}
公众假期()
{
_baa=新字节[DIM][];
对于(int i=0;i<_baa.length;i++)
{
_baa[i]=(“这是一个序列化和解析测试,看看它是否无一例外地完成会很有趣,我怀疑不会,因为JRE-“+i.getBytes()中似乎有一个bug;
}
}
@凌驾
public void readExternal(ObjectInput oi)抛出IOException、ClassNotFoundException
{
int iDim=oi.readInt();
_baa=新字节[iDim][];
for(int i=0;i
这应该是:

oo.writeObject(p1);

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

您误解了异常。它并不是说流在读取过程中已经“损坏”。它说它遇到了一个损坏的输入流。不一样。请发布完整的异常消息和堆栈跟踪。我做了建议的修改,但是仍然看到意外的结果。
oo.writeObject(p1);