No, the Android behavior is correct. I would say that JRE6 is not implementing this incorrectly.
>From the ByteBuffer.getShortBuffer() documentation "The new buffer shares content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent." While the JRE6 behavior may be convenient, it is not correct, in that it does not enforce the limit and mark independence of the two buffers. An easy way to get this to behave correctly on both platforms is: ShortBuffer sb = byteBuffer.asShortBuffer(); sb.position(byteBuffer.position() * Byte.size / Short.size); Cheers, Justin Android Team @ Google On Oct 19, 10:12 am, mbarbeaux <[EMAIL PROTECTED]> wrote: > Hello, > > I'm currently trying to port a Java JRE6 program into Google Android > platform. This program uses NIO buffers to read data from a binary > file. It needs to read byte and short from the file. > > With standard JRE6, I could "navigate" throught the memory mapped data > using thoses kind of code : > > final FileInputStream fis = .... > final FileChannel channel = fis.getChannel(); > byteBuffer = channel.map(MapMode.READ_ONLY, 0, channel.size()); > > // Read some bytes. > byteBuffer.get(); > byteBuffer.get(array); > > OK, this seems to work well too on Google Android. But problems start > to happen when I need to read short values after reading byte values > from the buffer. > > When, in JRE6 platform, I do : > > // Read a short value > byteBuffer.asShortBuffer().get(); > > I successfully retrieve the first short value at the corresponding > position from byteBuffer. Eg, if byteBuffer is at position 10, then it > reads two bytes (pos 11 and 12) and computes then as a short. That's > the standard functionnality. > > But with Google Android, it doesn't seem to work that way. The same > code doesn't produce the same output : > > // Read a short value > byteBuffer.asShortBuffer().get(); > > The "byteBuffer.asShortBuffer()" successfully returns a ShortBuffer, > but position seems to be lost in the creation of the ShortBuffer. Eg, > if position in byteBuffer was 10, the position of ShortBuffer is > actually 0. So when I read a short value from this ShortBuffer, it > actually read the two first bytes of the stream (0 and 1) instead of > reading bytes at position 11 and 12 as the normal JRE6 does. > > Thus, each time I want to read a short value, either I must use the > ByteBuffer and computes by myself two bytes into one short, either I > use the "asShortBuffer" method but then I have to compute myself the > right position into the new buffer, thing I didn't have to make with > JRE6. > > Is that a bug or a normal thing with Android ? > Thanks for your help, > Mikael --~--~---------~--~----~------------~-------~--~----~ 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] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

