private void writeXRefTable() throws IOException {
        // Link each deleted entry to the next deleted entry by iterating
        // backwards through the entries, which are sorted by object number.

        // End chain by pointing to head. If none del, chain back on itself.
        int nextDeletedObjectNumber = 0;
        for(int i = entries.size()-1; i >= 0; i--) {
            Entry entry = entries.get(i);
            if (entry.isDeleted()) {
                entry.setNextDeletedObjectNumber(nextDeletedObjectNumber);
                nextDeletedObjectNumber =
                    entry.getReference().getObjectNumber();
            }
        }

        // Insert pseudo-entry for object number zero - the head of freed list
        // The generation number is 65535, but we increment it when writing
        Entry zero = new Entry(new Reference(0, 65534));
        zero.setNextDeletedObjectNumber(nextDeletedObjectNumber);
        entries.add(0, zero);

        output.write(NEWLINE);
        xrefPosition = startingPosition + output.getCount();
        output.write(XREF);
        for(int i = 0; i < entries.size();) {
            i += writeXrefSubSection(i);
        }
        output.write(NEWLINE);
    }