code: private static GCHandle createHandle (Buffer buffer)
    {
      Object array;
      if (buffer instanceof ByteBuffer)
        array = ((ByteBuffer)buffer).array();
      else if (buffer instanceof ShortBuffer)
        array = ((ShortBuffer)buffer).array();
      else if (buffer instanceof IntBuffer)
        array = ((IntBuffer)buffer).array();
      else if (buffer instanceof FloatBuffer)
        array = ((FloatBuffer)buffer).array();
      else if (buffer instanceof DoubleBuffer)
        array = ((DoubleBuffer)buffer).array();
      else
        throw new IllegalArgumentException();
      return GCHandle.Alloc(array, GCHandleType.wrap(GCHandleType.Pinned));
    }
docs: Allocates a pinned GCHandle pointing at a buffer's data. Remember to free it!