From: Paul G. <pau...@so...> - 2002-05-13 07:20:48
|
> The problem I'm having if anyone has read this far is with the > ByteBuffer->String conversion required by Jython. If it's just textual > data then I have no problem, but I can't get it to work properly with > binary data, say downloading an image. I'm no expert on this stuff so > if there are any please contact me. Here is what I know about ByteBuffer to String conversion. Check http://www.jugs.ch/html/events/2002/NIO_Presentation/Slide_D02.html. The intended way to go from a ByteBuffer to a String is via a CharsetDecoder, which assumes that the data in the ByteBufer has been encoded with a particular format. The CharsetDecoder converts the encoded bytes into Unicode. This will clearly mess up any binary data (e.g. images) that you want to store in a String. (Storing binary data in Strings is of course normal in Python, but very unothodox in Java.) In this case, it sounds like you want to use the method labeled "WRONG" in the slide: ByteBuffer.asCharBuffer(). This overlays the memory used by the ByteBuffer with a CharBuffer, without doing any conversion on the contents. In fact both objects, ByteBuffer and CharBuffer, can manipulate the same segement of memory at the same time. This is basically the equivalent of having a char pointer and a byte pointer to the same address in a C program. (Pretty radical for Java, IMHO) If you have already gotten this far and it does not work, my appologies. Then I am out of ideas. -Paul -- Paul Giotta Software Architect Technoparkstrasse 1, CH-8005 Zurich. Email: paul.giotta@Softwired-inc.com Home Page WWW: http://www.softwired-inc.com Office: +41 1 4452370 | Fax: +41 1 4452372 | Mobile: +41 76 389 1180 |