Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

how to Serialize a Object to a String(or byte[])

Thread view: 
Colin Song - 17 Dec 2007 09:16 GMT
we can writeObject to a file. but if i want Serialize a Object to a
String(or byte[]) and i don't like to write the object to a file and
then read it ....
Andrew Thompson - 17 Dec 2007 09:30 GMT
>we can writeObject to a file. but if i want Serialize a Object to a
>String(or byte[]) and i don't like to write the object to a file and
>then read it ....

Write the Object to a ByteArrayOutputStream (which wraps
an ObjectOutputStream), then use baos.toString().

Why are you serializing Object(s) to a String?

A couple of notes while I'm here.  The first letter of
each sentence, as well as the word 'I', should be
Upper Case.

Signature

Andrew Thompson
http://www.physci.org/

Colin Song - 17 Dec 2007 09:51 GMT
> >we can writeObject to a file. but if i want Serialize a Object to a
> >String(or byte[]) and i don't like to write the object to a file and
[quoted text clipped - 13 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1

I want to transfer Object by webservice,and I want write a generic
webservice to transfer some difierent Objects.
So, I need to change Object to String and in webservice client return
to Object.
Thank you.
Andrew Thompson - 17 Dec 2007 11:18 GMT
...
>I want to transfer Object by webservice,and I want write a generic
>webservice to transfer some difierent Objects.
>So, I need to change Object to String and in webservice client return
>to Object.

That sounds logical.  I'll leave it to others to
identify any other strategies worth mentioning.

>Thank you.

You are welcome.

Signature

Andrew Thompson
http://www.physci.org/

Lew - 17 Dec 2007 15:33 GMT
Colin Song wrote:
>> I want to transfer Object by webservice,and I want write a generic
>> webservice to transfer some difierent Objects.
>> So, I need to change Object to String and in webservice client return
>> to Object.

The trouble with serializing to String is that you have to deal with character
encoding.  Serializing to byte [] eliminates that issue.

Base64 encoding might be the answer you're looking for.  java.io serialization
is a complicated matter and one fraught with peril.  Study all the
implications (e.g., that it creates another public "back-door" interface to
your class that breaks encapsulation) before using the object serialization
mechanism.

And avoid using String as the intermediate format.

Signature

Lew

Arne Vajhøj - 18 Dec 2007 02:34 GMT
> I want to transfer Object by webservice,and I want write a generic
> webservice to transfer some difierent Objects.
> So, I need to change Object to String and in webservice client return
> to Object.

Don't.

It is rather pointless to spend  a lot of CPU cycles and network
bandwith to use a technology neutral format and then send a
Java object that can only be understood by Java.

Arne
Lew - 18 Dec 2007 03:50 GMT
Colin Song wrote:
>> I want to transfer Object by webservice,and I want write a generic
>> webservice to transfer some difierent Objects.
>> So, I need to change Object to String and in webservice client return
>> to Object.

> Don't.
>
> It is rather pointless to spend  a lot of CPU cycles and network
> bandwith to use a technology neutral format and then send a
> Java object that can only be understood by Java.

Stepping back one level, one sees a number of solutions to the general problem
of communicating information, including sometimes object models, between
heterogeneous systems.  One popular solution is XML, albeit in multiple
incompatible formats.  Depending on how multi-platform your user base is,
SOAP-based web services offer a reasonable solution to how to glue together a
service-oriented architecture (SOA).  Even within the supposedly interoperable
world of SOAP-based web services over HTTP, there are nuances (XML-RPC vs.
document-literal) that can break compatibility, but at least there they're
fairly well documented.

Happily, Java lives very well with the world of SOAP-based web services, and
the two converge more and more all the time.

<http://java.sun.com/developer/technicalArticles/J2EE/intro_ee5/>
<http://java.sun.com/developer/technicalArticles/J2EE/intro_ee5/#support>

Signature

Lew

Andrew Thompson - 18 Dec 2007 04:21 GMT
>Colin Song wrote:
>>> I want to transfer Object by webservice,and I want write a generic
>>> webservice to transfer some difierent Objects.

(Arne)
>> Don't.  ..
...
>Stepping back one level, ...

This thread has become a lot more interesting since the
arrival of Lew and Arne.  I hope the OP is still reading it.

Signature

Andrew Thompson
http://www.physci.org/

Colin Song - 18 Dec 2007 04:43 GMT
Hi,all
I serialize Object to byte[] and then use base64 encode.It works well
now,I need more test on unix server(now I tested on my pc,win
XP).btw,the web service client and server both run on unix server and
writen by java.
Thanks for your help.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class WSHelper {
    static private BASE64Encoder encode = new BASE64Encoder();
    static private BASE64Decoder decode = new BASE64Decoder();

    static public String OToS(Object obj) {
        long start=System.currentTimeMillis();
        String out = null;
        if (obj != null) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                out = encode.encode(baos.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("Encode:"+(end-start));
        return out;
    }

    static public Object SToO(String str) {
        long start=System.currentTimeMillis();
        Object out = null;
        if (str != null) {
            try {

                ByteArrayInputStream bios = new ByteArrayInputStream(decode
                        .decodeBuffer(str));
                ObjectInputStream ois = new ObjectInputStream(bios);
                out = ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("Decode:"+(end-start));
        return out;
    }
}
Colin Song - 18 Dec 2007 04:50 GMT
Hi,all
I serialize Object to byte[] and then use base64 encode.It works well
now,I need more test on unix server(now I tested on my pc,win
XP).btw,the web service client and server both run on unix server and
writen by java.
Thanks for your help.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class WSHelper {
    static private BASE64Encoder encode = new BASE64Encoder();
    static private BASE64Decoder decode = new BASE64Decoder();

    static public String OToS(Object obj) {
        long start=System.currentTimeMillis();
        String out = null;
        if (obj != null) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                out = encode.encode(baos.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("Encode:"+(end-start));
        return out;
    }

    static public Object SToO(String str) {
        long start=System.currentTimeMillis();
        Object out = null;
        if (str != null) {
            try {

                ByteArrayInputStream bios = new ByteArrayInputStream(decode
                        .decodeBuffer(str));
                ObjectInputStream ois = new ObjectInputStream(bios);
                out = ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("Decode:"+(end-start));
        return out;
    }
}
Roedy Green - 18 Dec 2007 01:42 GMT
On Mon, 17 Dec 2007 01:16:56 -0800 (PST), Colin Song
<song6295@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>we can writeObject to a file. but if i want Serialize a Object to a
>String(or byte[]) and i don't like to write the object to a file and
>then read it ....

see http://mindprod.com/applet/fileio.html

it will generate you sample code.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.