ObjectInputStream fails initialization with ByteArrayInputStream

81 Views Asked by At

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

There are 1 best solutions below

0
On

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.