ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream

2019-09-01 17:03:23  阅读:222  来源: 互联网

标签:objectinputstream objectoutputstream java bytearray stream


我对java有一个奇怪的问题.我想在ObjectOutputStream中写一个byte []并从那里写到一个新文件.该字节数组表示从磁盘读取的另一个文件.

之后,在写入新创建的文件后,我想从该文件中读取.但是现在从ObjectInputStream读取的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;
    }
}

我希望你能帮帮我!

解决方法:

你忽略了异常;你没有关闭正确的流;而你假设read()填充缓冲区.使用readFully().您不是在编写对象,因此您也可以使用DataInputStream和DataOutputStream来节省一点空间.

标签:objectinputstream,objectoutputstream,java,bytearray,stream
来源: https://codeday.me/bug/20190901/1784700.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有