2013-12-22 29 views
0

解壓縮和反序列化字節數組時遇到了EOFException。
堆棧跟蹤:按字節數組反序列化時EOFExcpetion

java.io.EOFException的 在java.io.ObjectInputStream中的$ PeekInputStream.readFully(ObjectInputStream.java:2324) 在java.io.ObjectInputStream中的$ BlockDataInputStream.readShort(ObjectInputStream.java:2793 ) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799) at java.io.ObjectInputStream。(ObjectInputStream.java:299) at com.chuck.pack.ResourcePacket.load(ResourcePacket.java:44)

// Functions related to load 

public static ResourcePacket load(String packetName) { 
    try { 
     byte[] bytes = uncompress(fileToByteArray(new File(packetName))); 
     ByteArrayInputStream in = new ByteArrayInputStream(bytes); 
     ObjectInputStream objIn = new ObjectInputStream(in); // Error occured here 
     return (ResourcePacket) objIn.readObject(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

    return null; 
} 

private static byte[] uncompress(byte[] bytes) { 
    try { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     Inflater ifl = new Inflater(); 
     ifl.setInput(bytes); 

     byte[] buffer = new byte[4*1024]; 
     while(ifl.finished()) { 
      int size = ifl.inflate(buffer); 
      out.write(buffer, 0, size); 
     } 
     out.close(); 
     return out.toByteArray(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     System.out.println("Error while load"); 
    } 
    return null; 
} 

private static byte[] fileToByteArray(File file) { 
    byte[] bytes = new byte[(int) file.length()]; 
    try { 
     FileInputStream fileInputStream = new FileInputStream(file); 
     fileInputStream.read(bytes); 
     fileInputStream.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

    return bytes; 
} 

// Functions related to save 

public void save() { 
    try { 
     byte[] bytes = compress(toByteArray()); 
     //byte[] bytes = toByteArray(); 
     FileOutputStream fileOutputStream = new FileOutputStream(packetName); 
     fileOutputStream.write(bytes); 
     fileOutputStream.close(); 
     System.out.println("Packet saved (" + resourceFiles.length + " Files)"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
     System.exit(-1); 
    } 
} 

private byte[] compress(byte[] bytes) { 
    try { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     Deflater dfl = new Deflater(); 
     dfl.setLevel(Deflater.BEST_SPEED); 
     dfl.setInput(bytes); 
     dfl.finish(); 
     byte[] tmp = new byte[4*1024]; 
     while(!dfl.finished()) { 
      int size = dfl.deflate(tmp); 
      out.write(tmp, 0, size); 
     } 
     out.close(); 
     return out.toByteArray(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
     System.out.println("Error while save"); 
    } 
    System.exit(-1); 
    return null; 
} 

private byte[] toByteArray() { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    ObjectOutputStream out = null; 
    try { 
     out = new ObjectOutputStream(bos); 
     out.writeObject(this); 
     byte[] bytes = bos.toByteArray(); 
     return bytes; 
    } catch(IOException ex) { 
     ex.printStackTrace(); 
     System.out.println("Error while create byte array"); 
    } 
    System.exit(-1); 
    return null; 
} 

有人想解決這個問題嗎?

+0

當程序得知您是Chuck Norris時,它會自行修復。聖誕快樂:) – Melquiades

+0

這將是不錯的;)另外,聖誕快樂 – ChuckNorris

+0

我們需要看到對象是如何序列化的,也是如何壓縮的。這可能是序列化的問題,也可能是由於錯誤導致壓縮的數據與解壓縮後的結果不匹配的問題。 –

回答

0

uncompress(..)方法while循環:

while(ifl.finished()) { 
    int size = ifl.inflate(buffer); 
    out.write(buffer, 0, size); 
} 

應該是

while(!ifl.finished()) { 
    int size = ifl.inflate(buffer); 
    out.write(buffer, 0, size); 
} 

否則的uncompress(..)的結果是一個空字節數組,如果該方法終止。

+0

是的,經過長時間的搜索後,我發現自己。 – ChuckNorris