【问题标题】:StreamCorruptedException with ObjectInputStream and ByteArrayInputStream带有 ObjectInputStream 和 ByteArrayInputStream 的 StreamCorruptedException
【发布时间】:2013-01-09 19:06:16
【问题描述】:

我有许多使用 ObjectOutputStream 写入磁盘的对象。在读取过程中,出于某些实现原因,我首先将文件作为 ByteArray 检索,我想读取缓冲数组并从中解码数据。这是一个代码sn-p

byte [] fileArray=org.apache.commons.io.IOUtils.toByteArray(filePath);

ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(fileArray));

while(true){
 Records pos=(Records)in.readObject();
}

但是,我得到了这个错误

java.io.StreamCorruptedException: invalid stream header: 2F6C6F63

总而言之,我想将文件加载到内存中,然后在读取时解码对象而不是从磁盘中解码。


文件写成:

fout=new FileOutputStream(filePath);
bos=new ByteArrayOutputStream();
oos=new ObjectOutputStream(bos);

for(int i=0;i<size;i++){
 oos.writeObject(list.get(i));
}
oos.flush();
bos.writeTo(fout);
bos=null;
oos=null;
fout.flush();
fout.close();

oos 根本没有关闭!


这是重现错误的完整示例:

import java.util.*;
import java.io.*;

import org.apache.commons.io.IOUtils.*;

public class Example{

    private int[] data;

    public Example(){
        data=new int[40];
    }

    public void generate(){
        for(int i=0;i<data.length;i++){
            data[i]=i;
        }
        System.out.println("Data generated!");
    }

    public void write(){
        FileOutputStream fout=null;
        ByteArrayOutputStream bos=null;
        ObjectOutputStream oos=null;
        try{
            fout=new FileOutputStream("obj.data");
            bos=new ByteArrayOutputStream();
            oos=new ObjectOutputStream(bos);
            for(int i=0;i<data.length;i++){
                oos.writeObject((Integer)data[i]);
            }
            oos.flush();
            bos.writeTo(fout);
            bos=null;
            oos=null;
            fout.flush();
            fout.close();
        }catch(IOException ioe){}
        System.out.println("Data written!");
    }

    public void read(){
        ObjectInputStream in=null;
        try{
            byte[] fileArray=org.apache.commons.io.IOUtils.toByteArray("obj.data");
            in=new ObjectInputStream(new ByteArrayInputStream(fileArray));
            while(true){
                Integer data=(Integer)in.readObject();
            }
        }catch (EOFException eofe){
            try{
                in.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }       
        System.out.println("Data read!");
    }

    public static void main(String[] args){
        Example example=new Example();
        example.generate();
        example.write();
        example.read();
    }


}

【问题讨论】:

  • 嗯,听起来数据坏了。编写它的代码是什么样的?
  • @JonSkeet 感谢您的回复。更新了问题!我认为这是错误,但有没有办法通过不再进行数据生成来解决!
  • 好吧,让我们先诊断一下问题...您能否在一个简短但完整的示例中重现此问题,同时进行读取和写入,但显示相同的错误?跨度>
  • @JonSkeet 谢谢。更新了,还是一样的错误
  • 为什么所有的字节数组流?只需直接对文件进行 I/O 操作,并为自己节省大量内存。代码也更简单。

标签: java serialization deserialization java-io


【解决方案1】:

好的,现在找到了。这就是问题所在:

byte[] fileArray=org.apache.commons.io.IOUtils.toByteArray("obj.data");

That method 并没有按照你的想法去做:

使用平台的默认字符编码以字节[]的形式获取字符串的内容。

它根本没有加载一个文件

如果你改用这个:

 byte[] fileArray = 
     org.apache.commons.io.FileUtils.readFileToByteArray(new File("obj.data"));

...那么数据就被正确恢复了。

(顺便说一句,我个人更喜欢Guava这种事情......)

【讨论】:

  • 谢谢乔恩。这对我有用。也感谢 Guava 的链接。
猜你喜欢
  • 2020-05-13
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
相关资源
最近更新 更多