private long recoverPendingTransactions(
      TransactionSubscriber subscriber, long initialTransaction, File initialJournal)
      throws IOException {
    long recoveringTransaction = PrevaylerDirectory.journalVersion(initialJournal);
    File journal = initialJournal;
    DurableInputStream input = new DurableInputStream(journal, _monitor);

    while (true) {
      try {
        Chunk chunk = input.readChunk();

        if (recoveringTransaction >= initialTransaction) {
          if (!journal.getName().endsWith(_journalSuffix)) {
            throw new IOException(
                "There are transactions needing to be recovered from "
                    + journal
                    + ", but only "
                    + _journalSuffix
                    + " files are supported");
          }

          TransactionTimestamp entry = TransactionTimestamp.fromChunk(chunk);

          if (entry.systemVersion() != recoveringTransaction) {
            throw new IOException(
                "Expected " + recoveringTransaction + " but was " + entry.systemVersion());
          }

          subscriber.receive(entry);
        }

        recoveringTransaction++;

      } catch (EOFException eof) {
        File nextFile = _directory.journalFile(recoveringTransaction, _journalSuffix);
        if (journal.equals(nextFile))
          PrevaylerDirectory.renameUnusedFile(
              journal); // The first transaction in this log file is incomplete. We need to reuse
                        // this file name.
        journal = nextFile;
        if (!journal.exists()) break;
        input = new DurableInputStream(journal, _monitor);
      }
    }
    return recoveringTransaction;
  }