Thread: serialized objects and JDBC driver

serialized objects and JDBC driver

From
Stuart Barlow
Date:
I am attempting to serialize objects into Postgresql using the
JDBC driver. I am attempting to do this the standard way
using Object and Byte streams.

To store the object to the database in a text (blob) field I use the
following code to turn my object into a byte[].

             // create the serialized object and store in the database
             EncryptionInfo serObj = new EncryptionInfo(32, 32);
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream p = new ObjectOutputStream(bos);
                 p.writeObject(serObj);
                 p.flush();
             byte[] data = bos.toByteArray();


This seems to go into the database with no errors.
However when I retieeve the data using the following code...

ByteArrayInputStream bis = new ByteArrayInputStream(data);
         ObjectInputStream p = new ObjectInputStream(bis);
         EncryptionInfo ei = (EncryptionInfo)p.readObject();

I get the following error...
InputStream does not contain a serialized object


Thanks.
Stuart Barlow.

*************************************************************************
Information in this email is confidential and may be privileged. It is
intended for the named addressee(s) only. If you have received it in
error please notify the sender immediately and delete it from your
system. You should not otherwise copy, retransmit, use or disclose its
contents to anyone.
*************************************************************************


Re: serialized objects and JDBC driver

From
"David Wall"
Date:
> ByteArrayInputStream bis = new ByteArrayInputStream(data);
>          ObjectInputStream p = new ObjectInputStream(bis);
>          EncryptionInfo ei = (EncryptionInfo)p.readObject();

Are you using setBytes/getBytes in JDBC?  That's worked for me when the SQL
type is OID on 7.1beta4.

David


Re: serialized objects and JDBC driver

From
jlemcke@netspace.net.au
Date:
It might be worth your while having a look at JSX as an alternative to standard
Java serialization. JSX serializes Java objects to XML which, being plain text,
may be easier to manage.
I haven't done any serious work with JSX yet but it seems to work well in the
trials I've done.

Check out http://www.csse.monash.edu.au/~bren/JSX/

Have Fun,
John L.

Quoting Stuart Barlow <sbarlow@peopledoc.com>:

>
> I am attempting to serialize objects into Postgresql using the
> JDBC driver. I am attempting to do this the standard way
> using Object and Byte streams.
>
> To store the object to the database in a text (blob) field I use the
> following code to turn my object into a byte[].
>
>              // create the serialized object and store in the database
>              EncryptionInfo serObj = new EncryptionInfo(32, 32);
>              ByteArrayOutputStream bos = new ByteArrayOutputStream();
>              ObjectOutputStream p = new ObjectOutputStream(bos);
>                  p.writeObject(serObj);
>                  p.flush();
>              byte[] data = bos.toByteArray();
>
>
> This seems to go into the database with no errors.
> However when I retieeve the data using the following code...
>
> ByteArrayInputStream bis = new ByteArrayInputStream(data);
>          ObjectInputStream p = new ObjectInputStream(bis);
>          EncryptionInfo ei = (EncryptionInfo)p.readObject();
>
> I get the following error...
> InputStream does not contain a serialized object
>
>
> Thanks.
> Stuart Barlow.
>
> *************************************************************************
> Information in this email is confidential and may be privileged. It is
> intended for the named addressee(s) only. If you have received it in
> error please notify the sender immediately and delete it from your
> system. You should not otherwise copy, retransmit, use or disclose its
> contents to anyone.
> *************************************************************************
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>


-------------------------------------------------
This mail sent through IMP: www.netspace.net.au