2014-03-31 30 views
0

當我嘗試讀取存儲在文件中的壓縮對象時,顯然一切似乎都正常,我可以獲取保存的值,但在日誌中始終顯示此錯誤當我嘗試讀取文件中的壓縮對象時,發生StreamCorruptedException

java.io.StreamCorruptedException 
W/System.err﹕ at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2067) 
W/System.err﹕ at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372) 

我不understod如何解決它

我使用它來裝載:

ObjectInputStream inputStream = null; 
     try { 
      inputStream = new ObjectInputStream(cipherInputStream); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if (inputStream != null) { 

      try { 
       myDecipheredObject = (Serializable) inputStream.readObject(); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 

和TH是存儲

ObjectOutputStream outputStream = null; 
      try { 
       outputStream = new ObjectOutputStream(cipherOutputStream); 
       outputStream.writeObject(object); 
       outputStream.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } catch (RuntimeException e) { 
      e.printStackTrace(); 
     } 

在這條線的負載方法發生錯誤

inputStream = new ObjectInputStream(cipherInputStream); 
+0

您可以顯示您用來創建密碼的輸入和輸出流的代碼嗎? – fge

+0

我使用與其他各種方法中使用的密碼相同的代碼,但此錯誤僅在此發生,所以我幾乎可以確定問題的原因是另一種。 – AndreaF

回答

0

,我能想到的,在您指定的線路異常的唯一可能的原因是這樣的:

當您從另一個InputStream構造一個新的ObjectInputStream時,構造函數將嘗試從另一個InputStream中讀取一個頭。如果它不能讀取和驗證這個頭,它會拋出StreamCorrupted異常。如果它不能讀取標題,或者標題錯誤(例如,包含不同的幻數或版本號),就會發生這種情況。

在輸入流試圖讀取對應的輸出流之前,是否有可能創建了相應的輸出流?或者是每個試圖從cipherOutputStream讀取的兩個不同的線程?

如果對象的寫作需要很長的時間,它可能是值得構建之後直接沖洗的OutputStream,這樣至少頭部被立即寫入:

  outputStream = new ObjectOutputStream(cipherOutputStream); 
      outputStream.flush(); 
      outputStream.writeObject(object); 
      outputStream.close(); 

最後,我假設您使用的密鑰與您用於加密時使用的密鑰相同。如果沒有,那麼這可能導致標題被錯誤地解密。

相關問題