java - 当我尝试从外部文件读取时,为什么 readObject 挂起?

标签 java serialization

我有一个已序列化的类(“Player”)(为了保持简短,省略了 getters 和 setters)。

长话短说,我能够读取对象的所有组件并将其写入硬盘驱动器上的文件中,数组中包含的图像除外。执行下面的“readObject”代码时系统挂起。 (但是,据我所知,也许“writeObject”代码没有按照我想象的方式工作)。

我已经尝试过仅使用 String、int、独立图像来编写此代码,并且它有效!

我在这里研究了一些解决方案,但所有问题都指向通过网络使用这两种方法,而不是保存的文件。

我错过了什么?如果有帮助的话,我还提供了下面的包装器组件。

更新:在“readObject”中打印“count”的值会产生一个大得离谱的数字,这可能解释了它挂起的原因(请参见下面的代码)。但我不明白的是为什么它读取的数字与保存的数字不同?

public class Player implements Serializable {
    private static final long serialVersionUID = -8193759868470009555L;
    private String someString = "";
    private int someInt = 0;
    private transient BufferedImage image = null;
    private transient ArrayList<BufferedImage> imageArray = new ArrayList<BufferedImage>();

    private void writeObject(ObjectOutputStream oos){
        oos.defaultWriteObject();

        // Image
        ImageIO.write(image, "png", oos);

        // Number of images in array
        oos.writeInt(imageArray.size());
        for (BufferedImage image: imageArray){
            ImageIO.write(image, "png", oos);
        }
    }

    private void readObject(ObjectInputStream ois){
        ois.defaultReadObject();

        // Image
        this.image = ImageIO.read(ois);

        // Number of images in array
        imageArray = new ArrayList<BufferedImage>();
        int count = ois.readInt();

                           //****** count = 415301485
        for (int i=0; i<count; i++){
            imageArray.add(ImageIO.read(ois));
        }
    }
}

包装组件:

public static Object LoadObject(String filename){
FileInputStream fis = null;
ObjectInputStream ois = null;
Object obj = null;

try {
    fis = new FileInputStream(filename);
    ois = new ObjectInputStream(fis);
    obj = ois.readObject();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {

    if (ois!=null){
        try {
            ois.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    if (fis!=null){
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
    return obj;
}

public static void SaveObject(Object obj, String filename){
FileOutputStream fos = null;
ObjectOutputStream oos = null;

try {
    fos = new FileOutputStream(filename);
    oos = new ObjectOutputStream(fos);
    oos.writeObject(obj);
    oos.flush();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {

    if (oos!=null){
        try {
            oos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (fos!=null){
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

最佳答案

ois.readInt() 的意外结果清楚地表明您的序列化和反序列化实现不同步。

我猜这是因为 ImageIO.read() 实际上读取的字节数多于解码图像所需的字节数。

尝试在图像数据前面添加其实际长度:

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", buffer);
oos.writeInt(buffer.size());
oos.write(buffer.toByteArray());

并使用它仅向 ImageIO.read() 提供有限的流 block :

int imageLength = ois.readInt();
byte[] buffer = new byte[imageLength];
ois.read(buffer);
this.image = ImageIO.read(new ByteArrayInputStream(buffer));

关于java - 当我尝试从外部文件读取时,为什么 readObject 挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281794/

相关文章:

java - 我应该将变量保留为 transient 吗?

java - 检查是否选择了 RadioButton 后按钮不会切换 Activity

java - 用于并发访问的 Jackrabbit 存储库锁定

java - ImmutableCollection.contains() 区分大小写吗?

java - 创建帐户时的验证 JAVA

java - Google App Engine (GAE) 上的高性能 Java 对象序列化

java - 如何告诉 Mockito 使用帮助类的真实(未模拟)版本?

python - 为什么 Python 的 json.dumps 拒绝序列化我的对象中的字符串?

android - 如何在 Activity 重新启动时保留复杂对象?

c# - 为什么我的 JSON 中没有添加大括号?