• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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've gotta fight it! Don't give in! Read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic