public static int getIndirectBufferByteOffset(Buffer buffer) {
        if (buffer == null) {
            return 0;
        }
        int i = buffer.position();
        
        if (!(buffer instanceof ByteBuffer)) {
            if (buffer instanceof FloatBuffer) {
                return 4 * (((FloatBuffer) buffer).arrayOffset() + i);
            }
            if (!(buffer instanceof IntBuffer)) {
                if (buffer instanceof ShortBuffer) {
                    return (((ShortBuffer) buffer).arrayOffset() + i) * 2;
                }
                if (buffer instanceof DoubleBuffer) {
                    return (((DoubleBuffer) buffer).arrayOffset() + i) * 8;
                }
                if (buffer instanceof LongBuffer) {
                    return (((LongBuffer) buffer).arrayOffset() + i) * 8;
                }
                if (buffer instanceof CharBuffer) {
                    return 2 * (((CharBuffer) buffer).arrayOffset() + i);
                } else {
                    throw new RuntimeException("Unknown buffer type " + buffer.getClass().getName());
                }
            } else {
                return 4 * (((IntBuffer) buffer).arrayOffset() + i);
            }
        } else {
            return ((ByteBuffer) buffer).arrayOffset() + i;
        }
    }