Quantcast
Channel: How to retrieve object from key value database into Map, then convert back to object in Java - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to retrieve object from key value database into Map, then convert back to object in Java

$
0
0

I am storing an object as a value in LevelDB. Both key and value must be in bytes for LevelDB.

I am receiving an object via a socket and casting it to MyObject:

MyObject myObject = (MyObject) (objectInput.readObject());

Then I am serialising my object when storing the value in LevelDB:

myLevelDb().put(bytes((publicKey)), Serializer.serialize(myObject));

The serializer code is as follows:

public static byte[] serialize(Object object) {        ByteArrayOutputStream bos = new ByteArrayOutputStream();        ObjectOutputStream out = null;        try {            out = new ObjectOutputStream(bos);            out.writeObject(object);            out.flush();            byte[] yourBytes = bos.toByteArray();            return yourBytes;        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                bos.close();            } catch (IOException ex) {                // ignore close exception            }        } return null;    }

Then I am trying to iterate through the LevelDB and store each object into a Map. Here is where I am trying to deserialize the bytes back into MyObject and save them to Map:

private void iterateBytes() {    DBIterator iterator = myLevelDb().iterator();    while (iterator.hasNext()) {        Map.Entry<byte[], byte[]> next = iterator.next();        String keyString = new String(next.getKey());        MyObject myObject = (MyObject) Serializer.deserialize(next.getValue());        Map<String, MyObject> myMap = new HashMap<>();        myMap().put(keyString, myObject);    }}

However, Java will not let me cast the deserialized bytes back to MyObject after it has been deserialized using the following code:

public static Object deserialize(byte[] bytes) {        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);        ObjectInput in = null;        try {            in = new ObjectInputStream(bis);            Object o = in.readObject();            return o;        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } finally {            try {                if (in != null) {                    in.close();                }            } catch (IOException ex) {                // ignore close exception            }        } return null;    }

I don't understand why I cannot convert the object back from a byte[] when I have followed the exact same method of serializing and deserializing. MyObject implements Serializable and the SUID is correct, as it works on API calls between devices. I just cannot add it to a Map as the original object, nor will Java let me cast it.

This is the line where an error is thrown, no matter where I try to cast it back to myObject:

    MyObject myObject = (MyObject) Serializer.deserialize(next.getValue());

Error:

class java.lang.String cannot be cast to class myPackage.MyObject (java.lang.String is in module java.base of loader 'bootstrap';

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>