英文:
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(<ByteArrayInputStream>)
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
, "contentSerial"));
// Debug
System.out.println("Read Bytes: "+ Arrays.toString(Advapi32Util.registryGetBinaryValue(
Bot.getHkey(), path1 + filePath
, "contentSerial")));
System.out.println("Read From Stream:: "+Arrays.toString(baos.readAllBytes()));
// Where it just stops
ObjectInput ois = new ObjectInputStream(baos);
// It doesn't run the next part
System.out.println("We made it past!");
Object read = ois.readObject();
Output (Console):
Read Bytes: [-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, I won't show all bytes, but there is more]
Read From Stream:: [-84, -19, 0, 5, 115, 114, 0, 56, 120, 121, I won'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论