File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stream to String and vice versa

 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i need to do some object-serializing from/to String variables.

I'm not very familiar with this stream stuff so please could someone
post some code particles.

I use the XMLEncoder/XMLDecoder Classes from java.beans

I either have an Object and want its XML representation written to a String.
Or i have the String with the XML representation and want to recreate the original Object.

java.beans gives me these two Classes (See API Doc Quotes) that work like a charm if i read/write from/to files.

So how would i go on with Strings insted. A surplus would be if we could have the Strings zip compressed/decompressed on reading/writing.

(This XML is HUGE)

Only thing i could find was some half-cooked stuff that changed all the beautiful UTF-8 characters in the XML to ??. Dang!


XMLEncoder

public XMLEncoder(OutputStream out)

Creates a new output stream for sending JavaBeans to the stream out using an XML encoding.

Parameters:
out - The stream to which the XML representation of the objects will be sent.
See Also:
XMLDecoder.XMLDecoder(InputStream)




XMLDecoder

public XMLDecoder(InputStream in)

Creates a new input stream for reading archives created by the XMLEncoder class.

Parameters:
in - The underlying stream.
See Also:
XMLEncoder.XMLEncoder(OutputStream)



You really could make my day here.

Thank you,

J.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into ByteArrayInputStream and ByteArrayOutputStream. Do they do what you need?
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i don't think so, the byteXX stuff chops my beautiful UTF-8 characters to pieces.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out StringReader and StringWriter (which uses a StringBuffer despite the name). These classes will correctly handle character conversion.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic