Journal { @SuppressLint("EmptyCatchBlock") LinkedHashMap<String, Entry> retrieveEntriesFromJournal() { maybeSwitchToBackupJournalFile(mDirectory); File journalFile = new File(mDirectory, JOURNAL_FILE); if (journalFile.exists()) { LinkedHashMap<String, Entry> lruEntries = new LinkedHashMap<>(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(journalFile)); boolean journalIsCorrupted = false; Set<String> dirtyEntryKeySet = new HashSet<>(); String line; while ((line = reader.readLine()) != null) { String[] lineParts = line.split(" "); String state = lineParts[0]; String key = lineParts[1]; if (CLEAN_ENTRY_PREFIX.equals(state) && lineParts.length == 3) { Entry entry = lruEntries.get(key); if (entry == null) { entry = new Entry(mDirectory, key); lruEntries.put(key, entry); } entry.markAsPublished(Long.parseLong(lineParts[2])); dirtyEntryKeySet.remove(key); } else if (DIRTY_ENTRY_PREFIX.equals(state) && lineParts.length == 2) { dirtyEntryKeySet.add(key); } else { journalIsCorrupted = true; break; } mLineCount++; } if (!journalIsCorrupted) { for (String key : dirtyEntryKeySet) { Entry entry = lruEntries.get(key); if (entry != null) { deleteFileIfExists(entry.getCleanFile()); deleteFileIfExists(entry.getDirtyFile()); } lruEntries.remove(key); } createJournalWriter(); return lruEntries; } } catch (IOException | IndexOutOfBoundsException | NumberFormatException ignored) { } finally { closeQuietly(reader); } } deleteUntrackedFiles(mDirectory); return null; } Journal(File directory, IgDiskCache cache, Executor executor);   }
@Test public void retrieveEntriesFromJournal() throws Exception { FileWriter writer = new FileWriter(mJournalFile); writer.write("DIRTY k1\nCLEAN k1 12\n"); writer.close(); LinkedHashMap<String, Entry> entries = mJournal.retrieveEntriesFromJournal(); assertThat(entries.size()).isEqualTo(1); assertThat(entries.get("k1")).isNotEqualTo(null); assertThat(entries.get("k1").isReadable()); }
@Test public void retrieveEntriesFromJournalBkp() throws Exception { mJournalFile.delete(); FileWriter writer = new FileWriter(mJournalBkpFile); writer.write("DIRTY k1\nCLEAN k1 12\n"); writer.close(); LinkedHashMap<String, Entry> entries = mJournal.retrieveEntriesFromJournal(); assertThat(entries.size()).isEqualTo(1); assertThat(entries.get("k1")).isNotEqualTo(null); assertThat(entries.get("k1").isReadable()); }