[jdom-interest] XMLOutputter & StringWriter

Jason Hunter jhunter at collab.net
Wed Jul 5 18:55:14 PDT 2000

> Yuck. Here's the problem I see:
> 1. You want to write an XML document using a Writer you yourself did
> not create.
> 2. The Writer uses some encoding, maybe UTF-8, maybe UTF-16, maybe
> something else. You don't know what.

You're assuming you don't know what encoding the writer uses.  Most
often you do know.  You do in the servlets case at least; the API is
just trying to play it safe and ensure once you start writing chars you
don't try to sneak out bytes.  If we provide an output(Document, Writer,
String) and the String is defined as the encoding used to construct the
writer (or perhaps just the encoding to write in the declaration), then
if you don't know the encoding of your writer it's pretty clear to you
that you're stuck.  :-)

> 3. If the Writer is using some Unicode variant, then you can just
> write it out, leave off the encoding declaration, and the processor
> that eventually reads the document will probably be able to
> auto-detect the encoding, especially if we always include a byte
> order mark.
> 4. However, if the Writer is writing anything other than Unicode, we
> MUST include an encoding declaration, but we don't know what encoding
> to declare! And the Writer won't tell us!
> I just don't see how we can do the right thing given the existing API
> of the Writer class. I agree that servlets are an important use case.
> Would this use case be satisfied if we had a method that was declared
> to take OutputStreamWriter rather than Writer so that we could find
> out what encoding it  was using?

Heck, if the writer's not a Unicode variant, then you're stuck already
should you try to write out any chars that may not be represented in
that encoding's character set.

Accepting an OSW is no help since to create an OSW you need an
underlying output stream.  With my servlet case you would only have
access to a PrintWriter.


More information about the jdom-interest mailing list