2012-10-26 50 views
3

我對java有一個奇怪的問題。我想在ObjectOutputStream中寫入(其中包含)一個byte [],並從那裏寫入一個新文件。該字節數組表示從磁盤讀取的其他文件。從ObjectInputStream中讀取與寫入ObjectOutputStream不同的字節[]

後來,寫入新創建的文件後,我想從該文件讀取。但是現在從ObjectInputStream讀取的byte []與寫入的不同。

這就是我的問題:爲什麼heck是那個byte []不同?

爲了明確這一點,併爲每一個檢查,我寫了一個小程序,這將正好說明我的意思:

import java.io.*; 
import java.net.URL; 
import java.nio.channels.Channels; 
import java.nio.channels.ReadableByteChannel; 
import java.security.MessageDigest; 
import org.bouncycastle.util.encoders.Hex; 

public class MyTest { 

    public static void main(String[] args) throws Exception { 

     // 1st step: 
     // ------------------------------------------------ 
     byte[] data = openFile(); 

     // Create file to write 
     FileOutputStream fos = new FileOutputStream(new File("test")); 
     ObjectOutputStream oosf = new ObjectOutputStream(fos); 
     // Write byte[]-length and byte[] 
     oosf.writeInt(data.length); 
     oosf.write(data); 

     // Flush & Close 
     fos.flush(); 
     fos.close(); 

     // Print hash value of saved byte[] 
     try { 
      final MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); 
      messageDigest.reset(); 
      System.out.println(new String(Hex.encode(messageDigest.digest(data)))); 
     } catch (Exception e) { 
     } 

     // 2nd step 
     // ------------------------------------------------ 

     // Open just saved file 
     FileInputStream fis = new FileInputStream(new File("test")); 
     ObjectInputStream ois = new ObjectInputStream(fis); 

     // Read the length and create a byte[] 
     int length = ois.readInt(); 
     byte[] dataRead = new byte[length]; 
     // Read the byte[] itself 
     ois.read(dataRead); 

     // Print hash value of read byte[] 
     try { 
      final MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); 
      messageDigest.reset(); 
      System.out.println(new String(Hex.encode(messageDigest.digest(dataRead)))); 
     } catch (Exception e) { 
     } 

     // Both printed hash values should be the same 

    } 

    private static byte[] openFile() throws Exception { 
     // Download a sample file which will be converted to a byte[] 
     URL website = new URL("http://www.marcel-carle.de/assets/Cryptonify/Cryptonify-1.7.8.zip"); 
     ReadableByteChannel rbc = Channels.newChannel(website.openStream()); 
     FileOutputStream fos2 = new FileOutputStream("tmp"); 
     fos2.getChannel().transferFrom(rbc, 0, 1 << 24); 
     fos2.flush(); 
     fos2.close(); 

     // Open downloaded file and convert to byte[] 
     File selectedFile = new File("tmp"); 
     FileInputStream fis1 = new FileInputStream(selectedFile); 
     byte[] data = new byte[(int) selectedFile.length()]; 
     fis1.read(data); 
     fis1.close(); 


     return data; 
    } 
} 

我希望你能幫幫我!

+0

它們有什麼不同? – zmbq

+0

它們在幾乎所有字節上都有差異...我沒有進一步檢查,現在生成一個散列值,它們不同 – Ph3n1x

+2

嘗試刷新並關閉最外面的流(即ObjectOutputStream)而不是FileOutputStream。然後停止忽略異常。然後停止忽略InputStream.read()的返回值並讀取一個循環,直到該方法返回-1。那就是說,如果你不寫和讀對象,爲什麼要使用Object流呢? –

回答

5

你忽略了異常;你沒有關閉正確的流;你假設read()填充緩衝區。使用readFully()。你不寫對象,所以你可以使用DataInputStreamDataOutputStream來節省一點空間。

+0

此代碼片段是一個簡短的書面示例,但我發現錯誤,我關閉了錯誤的流。在我真正的程序中,我使用了readFully(),只是忘記將它複製到我的示例項目中。 – Ph3n1x

+0

謝謝。無法弄清楚類似的問題。將read()更改爲readFully(),現在一切都很好! –

相關問題