2

Well, I am creating dumped objects using a ByteArrayOutputStream. And then will write them to MongoDB as an attribute in my documents. When I read it back how can I convert it to a byte array or a Binary without using a toString in the middle?

This is the way I do it now, and I dont think it is safe:

BasicBSONObject temp = (BasicBSONObject) models.get("0");
model = null;
    if (temp != null) {
        // TODO fix this serialization!
        try {
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(temp.toString().getBytes()));

        } catch (IOException e) {
            L.error("unable to read model from string!");
            L.error(e.getStackTrace().toString());
        }
    }
4
  • Did you try the temp.get("<key>") and casting to your target object? If that doesn't work, I guess, you might not have the option other than toString Commented Dec 19, 2012 at 17:53
  • 2
    I suggest you convert the byte array to a base64 string before save it in mongodb. In read operation, first decode the base64 to a byte array. Commented Dec 19, 2012 at 19:10
  • @MiguelCartagena Thank you but I guess that will be too much overhead.... Commented Jan 7, 2013 at 15:18
  • 1
    @HRahimi I store some small files in MongoDB in that way ... without overheads. Commented Jan 7, 2013 at 15:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.