On Fri, Aug 27, 2010 at 01:15, Bryan Duxbury <[email protected]> wrote: > On Thu, Aug 26, 2010 at 3:03 PM, Mathias Herberts < > [email protected]> wrote: > >> The changes you intend to do to your code are not correct. >> ByteBuffer.capacity() does not return the actual size of the data >> stored in a ByteBuffer. >> >> The size is actually ByteBuffer.limit() - ByteBuffer.arrayOffset(). >> > > Actually, I think it's bb.limit() - bb.arrayOffset() - bb.position(). (I > would welcome a patch that added a helper method for this somewhere.)
position will move as you add/read data. From what I see in the trunk t_java_generator.cc, limit()-arrayOffset() is used but not position (see generate_deep_copy_non_container), which I think might be closer to the semantics of a native byte[]. Relying on ByteBuffer's 'get' method is from my POV very dangerous as 'get' copies data from 'position' and then increments 'position', which means that 'get' is not idempotent. I foresee lots of hard to find bugs resulting from code converted from byte[] to ByteBuffer :-( Mathias.
