class BsDiff {

  static int lengthOfMatch(
      final RandomAccessObject oldData,
      final int oldStart,
      final RandomAccessObject newData,
      final int newStart)
      throws IOException {
    final int max = Math.min((int) oldData.length() - oldStart, (int) newData.length() - newStart);
    if (max > 0) {
      // If max is 0, it's sometimes possible for this seek to seek to length + 1 and throw an
      // exception unnecessarily.
      oldData.seek(oldStart);
      newData.seek(newStart);
      for (int offset = 0; offset < max; offset++) {
        if (oldData.readByte() != newData.readByte()) {
          return offset;
        }
      }
    }

    return max;
  }

}