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

C

Colin Song

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 ....
 
A

Andrew Thompson

Colin 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 ....

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
 
C

Colin Song

Colin 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 ....

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.

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.
 
A

Andrew Thompson

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
 
L

Lew

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.
 
A

Arne Vajhøj

Colin said:
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
 
L

Lew

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>
 
C

Colin Song

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;
}
}
 
C

Colin Song

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;
}
}
 
Joined
Aug 6, 2010
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top