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

Discussion in 'Java' started by Colin Song, Dec 17, 2007.

  1. Colin Song

    Colin Song Guest

    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 ....
    Colin Song, Dec 17, 2007
    #1
    1. Advertising

  2. Colin Song wrote:
    >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.

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

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 17, 2007
    #2
    1. Advertising

  3. Colin Song

    Colin Song Guest

    On 12ÔÂ17ÈÕ, ÏÂÎç5ʱ30·Ö, "Andrew Thompson" <u32984@uwe> wrote:
    > Colin Song wrote:
    > >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.
    >
    > --
    > Andrew Thompsonhttp://www.physci.org/
    >
    > 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.
    Colin Song, Dec 17, 2007
    #3
  4. 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.


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

    >Thank you.


    You are welcome.

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

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 17, 2007
    #4
  5. Colin Song

    Lew Guest

    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.

    --
    Lew
    Lew, Dec 17, 2007
    #5
  6. Colin Song

    Roedy Green Guest

    On Mon, 17 Dec 2007 01:16:56 -0800 (PST), Colin Song
    <> 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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 18, 2007
    #6
  7. 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.

    Arne
    Arne Vajhøj, Dec 18, 2007
    #7
  8. Colin Song

    Lew Guest

    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.


    Arne Vajhøj wrote:
    > 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>

    --
    Lew
    Lew, Dec 18, 2007
    #8
  9. Lew wrote:
    >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.

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

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 18, 2007
    #9
  10. Colin Song

    Colin Song Guest

    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, Dec 18, 2007
    #10
  11. Colin Song

    Colin Song Guest

    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, Dec 18, 2007
    #11
  12. Colin Song

    mitumix

    Joined:
    Aug 6, 2010
    Messages:
    1
    hi

    does the above work for webservices?

    I was also trying to send an object to to web file so i can later reuse it. This seems to be a very useful code. :D

    mitesh
    mitumix, Aug 6, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordz
    Replies:
    3
    Views:
    3,926
    John Oakes
    Jun 7, 2004
  2. Peter
    Replies:
    3
    Views:
    716
    Michael Borgwardt
    Aug 5, 2004
  3. Kirby
    Replies:
    3
    Views:
    632
    Kirby
    Oct 8, 2004
  4. DaBeef
    Replies:
    1
    Views:
    608
    Matt Humphrey
    Jul 21, 2006
  5. john harkin
    Replies:
    1
    Views:
    753
    john harkin
    Sep 23, 2004
Loading...

Share This Page