code: public static byte[] getArray(ByteBuffer buffer)
      {
          int length = buffer.remaining();
  
          if (buffer.hasArray())
          {
              int boff = buffer.arrayOffset() + buffer.position();
              return Arrays.copyOfRange(buffer.array(), boff, boff + length);
          }
          // else, DirectByteBuffer.get() is the fastest route
          byte[] bytes = new byte[length];
          buffer.duplicate().get(bytes);
  
          return bytes;
      }
docs: You should almost never use this.  Instead, use the write* methods to avoid copies.