What about ByteBuffer.get(ByteBuffer)

I’m slightly puzzled why we have so much put/get methods in ByteBuffer, yet ByteBuffer.get(ByteBuffer) is missing.

(And slightly puzzled why I only ran into this after so many years…)

We have:


ByteBuffer.put(byte[])
ByteBuffer.put(byte[], off, len)
ByteBuffer.put(ByteBuffer)
ByteBuffer.get(byte[])
ByteBuffer.get(byte[], off, len)
----- where is ByteBuffer.get(ByteBuffer)

Here’s my own workaround:


   public static void put(ByteBuffer dst, ByteBuffer src)
   {
      dst.put(src); // oh the simplicity!
   }

   public static void get(ByteBuffer dst, ByteBuffer src)
   {
      if(src.remaining() == dst.remaining())
      {
         dst.put(src);
         return;
      }

      if (src.remaining() < dst.remaining())
         throw new BufferUnderflowException();


      // what this effectivly does:
      //
      // while (dst.hasRemaining()) // dst, not src - like BB.put(BB)
      //    dst.put(src.get());

      int pos = src.position();
      int lim = src.limit();
      int rem = dst.remaining();

      // reduce the visible bytes, to the range we are about to copy
      src.limit(pos + rem);

      // copy bytes
      dst.put(src);

      // restore the visible bytes
      src.limit(lim);
   }

So what on earth am I missing, or… why is that method missing? :slight_smile:

???

http://java.sun.com/j2se/1.4.2/docs/api/java/nio/ByteBuffer.html#get(byte[],%20int,%20int)

Yeah… I already said that in my post… ::slight_smile:

ByteBuffer.get(byte[]) != ByteBuffer.get(ByteBuffer)

I suppose ByteBuffer.slice was supposed to be good enough?

Don’t get me started about object creation in tight loops…

Appearantly slice() wasn’t good enough to get rid of get(byte[]), it simply looks like they forgot it. ?