public static final int getDirectBufferByteOffset(Buffer buffer) {
        if (buffer == null) {
            return 0;
        }
        
        if (!(buffer instanceof ByteBuffer)) {
            if (!(buffer instanceof FloatBuffer)) {
                if (buffer instanceof IntBuffer) {
                    return buffer.position() * 4;
                }
                if (buffer instanceof ShortBuffer) {
                    return buffer.position() * 2;
                }
                if (buffer instanceof DoubleBuffer) {
                    return buffer.position() * 8;
                }
                if (!(buffer instanceof LongBuffer)) {
                    if (buffer instanceof CharBuffer) {
                        return buffer.position() * 2;
                    } else {
                        throw new InvalidParameterException("Buffer " + buffer.getClass() + " is invalid");
                    }
                } else {
                    return 8 * buffer.position();
                }
            } else {
                return 4 * buffer.position();
            }
        } else {
            return buffer.position();
        }
    }