private boolean compareLines(
     final String refText, final String text, final CompareStatistics statistics)
     throws IOException {
   log("running comparison");
   final LineNumberReader lnrRef = new LineNumberReader(new StringReader(refText));
   final LineNumberReader lnrOther = new LineNumberReader(new StringReader(text));
   String lineRef;
   String lineOther;
   while ((lineRef = lnrRef.readLine()) != null) {
     lineOther = lnrOther.readLine();
     if (lineOther == null) {
       statistics.failedRefIsLonger();
       return false;
     }
     if (!lineRef.equals(lineOther)) {
       statistics.failedDontMatch(lineRef, lineOther);
       return false;
     }
   }
   if (lnrOther.readLine() != null) {
     statistics.failedOtherIsLonger();
     return false;
   }
   return true;
 }
  public void compareFile(
      final File source,
      final File referenceDir,
      final File failedDirectory,
      final CompareStatistics stat)
      throws IOException {
    log("Comparing " + source.getPath());
    stat.checking(source);
    // TODO: reuse code from Regress
    final BoxRenderer renderer = new BoxRenderer(source, width);
    Box box;
    try {
      log("rendering");
      box = renderer.render();
      log("rendered");
    } catch (final Exception e) {
      e.printStackTrace();
      stat.failedToRender(e);
      storeFailed(failedDirectory, source);
      log("Could not render input file, skipping: " + source + " err: " + e.getMessage());
      return;
    }
    final LayoutContext layoutContext = renderer.getLayoutContext();
    final String inputFileName = source.getName();
    final String refRendered =
        trimTrailingLS(readReference(referenceDir, inputFileName, Regress.RENDER_SFX));
    final String rendered = trimTrailingLS(box.dump(layoutContext, "", Box.DUMP_RENDER));
    if (!compareLines(refRendered, rendered, stat)) {
      storeFailed(
          failedDirectory, new File(referenceDir, inputFileName), Regress.RENDER_SFX, rendered);
    }

    final String refLaidOut =
        trimTrailingLS(readReference(referenceDir, inputFileName, Regress.LAYOUT_SFX));
    final String laidOut = trimTrailingLS(box.dump(layoutContext, "", Box.DUMP_LAYOUT));
    if (!compareLines(refLaidOut, laidOut, stat)) {
      storeFailed(
          failedDirectory, new File(referenceDir, inputFileName), Regress.LAYOUT_SFX, laidOut);
    }
  }
  public void compareDirectory(
      final File sourceDirectory, final File referenceDir, final File failedDirectory)
      throws IOException {
    checkDirectories(sourceDirectory, referenceDir, failedDirectory);
    log("Starting comparison using width " + width);
    IOUtil.deleteAllFiles(failedDirectory);

    final boolean wasEnabled = enableLogging(false);
    try {
      final Iterator<File> fileIt = listSourceFiles(sourceDirectory);
      final CompareStatistics stats = new CompareStatistics();
      while (fileIt.hasNext()) {
        final File file = (File) fileIt.next();
        try {
          compareFile(file, referenceDir, failedDirectory, stats);
        } catch (final IOException e) {
          stats.failedIOException(e);
        }
      }
      stats.report();
    } finally {
      enableLogging(wasEnabled);
    }
  }