Hello,

I'm having a problem with either ByteBuffer.wrap().slice() or
CharsetDecoder.decode() under Android.  This is from a third-party
(LGPLed) library that, in this code, should be decoding a byte array
into a Unicode string:

        //arr is a parameter and is a byte[] filled with raw UTF-8
bytes
        //offset is a parameter and is = 1
        [...]
        CharsetDecoder decoder =
Charset.forName(charSetName).newDecoder();

        ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length
- offset).slice();
        CharBuffer outBuffer = CharBuffer.allocate(arr.length -
offset);

        decoder.reset();
        CoderResult coderResult = decoder.decode(inBuffer, outBuffer,
true);
        decoder.flush(outBuffer);
        outBuffer.flip();
        value=outBuffer.toString();
        [...]

However, when I run this, the decoding does not start at offset but
rather at the beginning of arr (as if offset = 0, which it is most
definitely not).  I believe this because the following (very ugly
hack) code creates a new byte [] with just the offset data and it
works properly:

        [...]
        CharsetDecoder decoder =
Charset.forName(charSetName).newDecoder();

        byte [] newarr = new byte [arr.length - offset];
        for (int iIndex = 0; iIndex < (arr.length - offset); iIndex++)
                newarr[iIndex] = arr[iIndex+offset];
        ByteBuffer inBuffer = ByteBuffer.wrap(newarr, 0,
newarr.length).slice();
        CharBuffer outBuffer = CharBuffer.allocate(newarr);

        decoder.reset();
        CoderResult coderResult = decoder.decode(inBuffer, outBuffer,
true);
        decoder.flush(outBuffer);
        outBuffer.flip();
        value=outBuffer.toString();
        [...]

Can anyone explain why the original piece of code does not work?

Thank you,
Greg

Not sure if this is necessary but probably best to be sure since it's
not my code:
The above code is licensed under the GNU Lesser General Public License
[http://www.gnu.org/licenses/lgpl.html]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to