private void setEnd(int newDataIndex, int newEnd) {
    if (newDataIndex < dataStart || data == null) {
        /* New data is before the start of what we have. Just throw it
           all away and start again */
        dataStart = newDataIndex;
        data = new int[INCREMENT];
    }
    if (newEnd > dataStart + data.length) {
        /* Try extending first */
        if (data.length < MAX_SIZE) {
        int newLength = (newEnd - dataStart + INCREMENT - 1) / INCREMENT * INCREMENT;
        if (newLength >= MAX_SIZE) {
            newLength = MAX_SIZE;
        }

        int[] newData = new int[newLength];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;

        }
        if (newEnd > dataStart + data.length) {
        /* Still doesn't fit. Squish.
           We assume INCREMENT >= (newEnd - newDataIndex), and ensure
           that dataStart + data.length - INCREMENT = newDataIndex */
        int newStart = newDataIndex + INCREMENT - data.length;

        if (dataStart + data.length > newStart) {
            System.arraycopy(data, newStart - dataStart, data, 0,
                     data.length - (newStart - dataStart));
        }
        dataStart = newStart;
        }
    }
    /* Mark any missing data as invalid */
    for (int i = dataEnd < dataStart ? dataStart : dataEnd;
         i < newDataIndex; i++) {
        data[i - dataStart] = -1;
    }

    /* If we receive a count less than the old count, we assume the old
       data is invalid */
    dataEnd = newEnd;

    }