简体   繁体   English

从ObjectInputStream中读取不同的byte [],而不是写入ObjectOutputStream

[英]Read different byte[] from ObjectInputStream than written to ObjectOutputStream

I have a strange problem with java. 我对java有一个奇怪的问题。 I want to write (amongst others) a byte[] in an ObjectOutputStream and from there to a new file. 我想在ObjectOutputStream中写一个byte []并从那里写到一个新文件。 That byte-array represent an other file read from disk. 该字节数组表示从磁盘读取的另一个文件。

Later, after writing into the newly created file, I want to read from that file. 之后,在写入新创建的文件后,我想从该文件中读取。 But the byte[] which is now read from the ObjectInputStream is different from the written one. 但是现在从ObjectInputStream读取的byte []与写入的不同。

And that is my question: WHY the heck is that byte[] different? 这就是我的问题:为什么这个字节[]不同?

To make it clear and for every one to check, I wrote a short program, which will exactly show what I mean: 为了清楚说明并让每个人都检查,我写了一个简短的程序,它将准确地表明我的意思:

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;
    }
}

I hope you can help me! 我希望你能帮帮我!

You're ignoring exceptions; 你忽略了异常; you aren't closing the right stream; 你没有关闭正确的流; and you're assuming that read() fills the buffer. 而你假设read()填充缓冲区。 Use readFully() . 使用readFully() You aren't writing objects so you may as well use DataInputStream and DataOutputStream for this and save a little space. 您不是在编写对象,因此您也可以使用DataInputStreamDataOutputStream来节省一点空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用连接到客户端的ObjectInputStream和ObjectOutputStream从客户端读取数据 - Read data from a client using ObjectInputStream and ObjectOutputStream connected to a client Java:尝试使用objectoutputstream / objectinputstream读取对象-崩溃 - Java: Trying to read object with objectoutputstream/objectinputstream - crashes ObjectInputStream 读线程阻塞 ObjectOutputStream 写线程 - ObjectInputStream read thread is blocking ObjectOutputStream writing thread ObjectInputStream和ObjectOutputStream - ObjectInputStream and ObjectOutputStream 使用ObjectOutputStream和ObjectInputStream从JTable打开/保存数据 - Open/Save data from JTable with ObjectOutputStream and ObjectInputStream Java从套接字获取ObjectInputStream ObjectOutputStream - Java getting ObjectInputStream ObjectOutputStream from a socket 如何使用ObjectOutputStream和ObjectInputStream正确地(反)序列化为字节数组? - How to (de)serialize correctly to a byte-array using a ObjectOutputStream & ObjectInputStream? 套接字ObjectInputStream和ObjectOutputStream不起作用 - Socket ObjectInputStream and ObjectOutputStream not working Java FileStream-ObjectOutputStream ObjectInputStream - Java FileStream - ObjectOutputStream ObjectInputStream ObjectOutputStream / ObjectInputStream的进度 - Getting Progress of ObjectOutputStream/ObjectInputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM