• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stream to String and vice versa

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into ByteArrayInputStream and ByteArrayOutputStream. Do they do what you need?
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No i don't think so, the byteXX stuff chops my beautiful UTF-8 characters to pieces.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out StringReader and StringWriter (which uses a StringBuffer despite the name). These classes will correctly handle character conversion.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic