[英]Why is readObject hanging when I try to read from an external file?
我有一個已序列化的類(“播放器”)(為簡短起見,省略了一個getters)。
長話短說,我能夠將對象的所有組件讀寫到硬盤上的文件中,但數組中包含的圖像除外。 當執行下面的“ 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()
:
int imageLength = ois.readInt();
byte[] buffer = new byte[imageLength];
ois.read(buffer);
this.image = ImageIO.read(new ByteArrayInputStream(buffer));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.