<div class="gmail_quote">I retract that in its entirety!<div><br></div><div><div><div><div><span style="white-space:pre-wrap">You are 100% correct. Bytes in does not equal bytes out.        </span></div>
<div><br></div>Wilf</div><div>(slightly humbled at this point)</div><div><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Canadian Wilf</b> <span dir="ltr"><<a href="mailto:canwilf@gmail.com" target="_blank">canwilf@gmail.com</a>></span><br>
Date: Fri, Sep 7, 2012 at 3:47 PM<br>
Subject: Re: [jdom-interest] Fwd: XML 1.1 -- Please stab me with a dull knife and trample my dead body<br>To: Bjorn Roche <<a href="mailto:bjorn@xowave.com" target="_blank">bjorn@xowave.com</a>><br>Cc: <a href="mailto:jdom-interest@jdom.org" target="_blank">jdom-interest@jdom.org</a><br>

<br><br>Java strings are not zero terminated. In Java, you can do this just fine:<div><br></div><div><div><font face="courier new, monospace">String string = new String(new byte[]{(byte) 0});</font></div><div><font face="courier new, monospace">System.out.println("text is [" + string + "]");</font></div>


<div><font face="courier new, monospace">System.out.println("bytes are: " + Arrays.toString(string.getBytes()));</font></div><div><font face="courier new, monospace">System.out.println("chars are: " + Arrays.toString(string.toCharArray()));</font></div>


<div><br></div><div>and you get ...</div><div><br></div><div><div><font face="courier new, monospace">text is [ ]</font></div><div><font face="courier new, monospace">bytes are: [0]</font></div><div><font face="courier new, monospace">chars are: [ ]</font></div>


</div><div><br></div><div>Whatever bytes you put into a String in java you always get out the same. Only change would be if you add in one charset and extract in a different one.</div><div><br></div><div>However, for binary data, like u said, Base 64 is best (it seems) since that is the only way to ensure wysiwyg.</div>


<div><br></div><div>Wilf</div><div><br></div><div>On Fri, Sep 7, 2012 at 3:27 PM, Bjorn Roche <span dir="ltr"><<a href="mailto:bjorn@xowave.com" target="_blank">bjorn@xowave.com</a>></span> wrote:</div><div class="gmail_quote">


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
On Sep 7, 2012, at 4:43 PM, Canadian Wilf wrote:<br>
<br>
> I can do this:<br>
><br>
> String random = new String(someRandomByte[])<br>
<br>
Let me address this by pointing out a degenerate case. Strings in java are terminated by the null char (er, I think. Wow, it's been a while since I learned this insanely basic thing). If your someRandomBytes contains two consecutive zero bytes (= a single zero char), then the string "random" will obviously not be what you wanted, because it will end early -- if you are lucky. Another example is if the "someRandomByte" ends in the first half of a unicode codepoint. What happens then? So, yes you can construct a string from a byte array like you did here but please don't! RTFM: "The behavior of this constructor when the given bytes are not valid in the default charset is unspecified." Unspecified. As in "it might delete your hard drive, log on to facebook and unfriend your wife." That's what unspecified means, so those bytes need to be "sanitized" too.<br>



<br>
If that's the kind of data you want to put in XML (raw, random-assed binary), use Base64!<br>
<br>
> However, the string cannot be passed to the Text of an XML Element since it may contain illegal characters (<= 0X20 ascii, vertical tab, etc.) This will fail:<br>
><br>
> new Element("test").setText(random)<br>
><br>
> XOM and JDOM both restrict the access and will throw IllegalDataException if one of the characters (0x--0xFFFF) is not in XML Unicode specs.<br>
<br>
First off, I think maybe you should read this because we are not talking about 0x0 to 0xFFFF: <a href="http://www.joelonsoftware.com/articles/Unicode.html" target="_blank">http://www.joelonsoftware.com/articles/Unicode.html</a><br>



<br>
Secondly, yes there are values that must be escaped in XML. For example < and > for obvious reasons, but the library does this for you. Then there are values you can't put into XML at all. These fall into other categories. "not valid in a string" (eg the NULL character usually used as a string terminator) is one. Yes, that's right, you can't put 0x00 in an XML string, 'cause you can't put it in a string! OMG! Stop the presses! I also find this annoying, and have been bitten by it (I think it was 0x17 or something), but that's life.<br>



<br>
I agree, however, it would be nice to have some clarity on exactly what's allowed.<br>
<br>
When in doubt, use Base64!<br>
<br>
Or create sub elements for the weird chars, just like html does for, say, newlines: <br /><br>
<br>
        bjorn<br>
<br>
-----------------------------<br>
Bjorn Roche<br>
<a href="http://www.xonami.com" target="_blank">http://www.xonami.com</a><br>
Audio Collaboration<br>
<a href="http://blog.bjornroche.com" target="_blank">http://blog.bjornroche.com</a><br>
<br>
<br>
<br>
<br>
</blockquote></div><br></div>
</div><br></div></div></div>
</div><br>