`ObjectInputStream`无法与`ByteArrayInputStream`成功初始化。

huangapple go评论66阅读模式
英文:

ObjectInputStream fails initialization with ByteArrayInputStream

问题

以下是你要翻译的内容:

我正在使用Java(JDA库)编写一个Discord机器人,并且正在制作一个数据库系统...

我首先只是将YML和JSON编写到文件中,然后我开始使用ObjectInputStream和输出版本来序列化对象。

然后我想到可以使用Windows注册表作为数据库,因为它占用的存储空间较小,而且效果更好,所以我重新编写了所有内容以使用注册表。对于JSON的读写,没有问题,它可以写入和读取包含JSON文本的字符串值。

对于序列化的对象,写入是可以的,但读取不行。
我将序列化的数据写入ByteArrayOutputStream,然后使用一个方法将字节写入注册表...

在读取方面,我使用从注册表读取的字节创建了一个ByteArrayInputStream,我在控制台中记录了字节,所以我知道它成功读取了字节并将它们传递给了ByteArrayInputStream
然后就是ObjectInputStream(<ByteArrayInputStream>),但它就是...停顿了下来,它不会抛出错误,在readObject()处也不会停顿,它只是在初始化ObjectInputStream的那一行停顿了下来。它不会反序列化任何内容,也不会返回任何内容,甚至不会打印出初始化之后的那一行...

代码:(用于序列化读取方法,不是写入)

            ByteArrayInputStream baos = new ByteArrayInputStream(Advapi32Util.registryGetBinaryValue(
                    Bot.getHkey(), path1+filePath
                    , "contentSerial"));

            // 调试
            System.out.println("读取的字节:" + Arrays.toString(Advapi32Util.registryGetBinaryValue(
                    Bot.getHkey(), path1 + filePath
                    , "contentSerial")));

            System.out.println("从流中读取: " + Arrays.toString(baos.readAllBytes()));

            // 停在这里
            ObjectInput ois = new ObjectInputStream(baos);
            // 不会执行下面的部分

            System.out.println("我们继续执行!");

            Object read = ois.readObject();

输出(控制台):

读取的字节:[-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, 不会显示所有字节,但还有更多]
从流中读取:[-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, 不会显示所有字节,但还有更多]
英文:

So I am writing a discord bot using Java (JDA Library) and I'm making a database system...

I start of with just writing YML and JSON to files and then I start serializing objects using ObjectInputStream and the Output version.

Then I got the idea to use the Windows registry as a database as it takes up less storage and it works nicer, so I re-write everything to use the registry. With the JSON reader and writer there isn't a problem, it can write and read to string values containing JSON text.

With the serialized objects the writing works but the reading doesn't.
I write the serialized data to a ByteArrayOutputStream and from there I use a method to write the bytes to the registry...

With the reading, I create a ByteArrayInputStream with the bytes I read from the registry, I logged the bytes in the console so I know that it successfully read the bytes and passed them over to the ByteArrayInputStream.
Then it comes to the ObjectInputStream(&lt;ByteArrayInputStream&gt;) and it just... hangs, it doesn't throw an error, it doesn't hang at readObject(), it just hangs at the line where I initialize the ObjectInputStream. It doesn't deserialize anything, it doesn't return anything, it doesn't print the line after the initialization...

Code: (For the serial reading method, not the writing)

            ByteArrayInputStream baos = new ByteArrayInputStream(Advapi32Util.registryGetBinaryValue(
                    Bot.getHkey(), path1+filePath
                    , &quot;contentSerial&quot;));

            // Debug
            System.out.println(&quot;Read Bytes: &quot;+ Arrays.toString(Advapi32Util.registryGetBinaryValue(
                    Bot.getHkey(), path1 + filePath
                    , &quot;contentSerial&quot;)));

            System.out.println(&quot;Read From Stream:: &quot;+Arrays.toString(baos.readAllBytes()));

            // Where it just stops
            ObjectInput ois = new ObjectInputStream(baos);
            // It doesn&#39;t run the next part

            System.out.println(&quot;We made it past!&quot;);

            Object read = ois.readObject();

Output (Console):

Read Bytes: [-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, I won&#39;t show all bytes, but there is more]
Read From Stream:: [-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, I won&#39;t show all bytes, but there is more]

答案1

得分: 1

你正在从“baos”流中读取所有字节,以将它们转储到控制台,因此该流为空。在下一行中,ObjectInputStream构造函数尝试读取流头,由于流为空,它会阻塞等待数据。

英文:

You are reading all bytes from the baos stream to dump them to the console, so the stream is empty. In the next line, the ObjectInputStream constructor tries to read the stream header and, as the stream is empty, it blocks waiting for data.

huangapple
  • 本文由 发表于 2020年9月21日 02:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63982564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定