public long skip(long n) throws IOException {
        long skipped = 0L;
        while (skipped < n) {
            int avail = ensureDataAvailable();
            if (avail <= 0) {
                if (skipped > 0L)
                    return skipped;
                else
                    return -1;
            }

            long toSkip = Math.min(n - skipped, avail);
            bufferPosition += toSkip;
            bufferAvailable -= toSkip;
            skipped += toSkip;
        }
        return skipped;
    }