private long readUntil(long pos) throws IOException {
        // We've already got enough data cached
        if (pos < length) {
            return pos;
        }
        // pos >= length but length isn't getting any bigger, so return it
        if (foundEOS) {
            return length;
        }

        int sector = (int)(pos >> SECTOR_SHIFT);

        // First unread sector
        int startSector = length >> SECTOR_SHIFT;

        // Read sectors until the desired sector
        for (int i = startSector; i <= sector; i++) {
            byte[] buf = new byte[SECTOR_SIZE];
            data.add(buf);

            // Read up to SECTOR_SIZE bytes
            int len = SECTOR_SIZE;
            int off = 0;
            while (len > 0) {
                int nbytes = src.read(buf, off, len);
                // Found the end-of-stream
                if (nbytes == -1) {
                    foundEOS = true;
                    return length;
                }
                off += nbytes;
                len -= nbytes;

                // Record new data length
                length += nbytes;
            }
        }

        return length;
    }
Ensures that at least <code>pos</code> bytes are cached,
or the end of the source is reached.  The return value
is equal to the smaller of <code>pos</code> and the
length of the source stream.
plugins/svg-plugin/batik-codec-fix/src/main/java/org/apache/batik/ext/awt/image/codec/util/MemoryCacheSeekableStream.java