Fix leftover bytes in cached content index file

Extra calls to CipherOutputStream.close() causes each time extra 16 bytes written to the
underlying OutputStream. Prevented close() is called more than once and also discarded any
data in ReusableBufferedOutputStream buffer on reset().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=144063120
This commit is contained in:
eguven 2017-01-10 02:53:21 -08:00 committed by Oliver Woodman
parent deefe50abc
commit 5c89bbedb7
3 changed files with 11 additions and 1 deletions

View File

@ -181,7 +181,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
// Assert file content is different
FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
for (int b; (b = fis1.read()) == fis2.read();) {
for (int b; (b = fis1.read()) == fis2.read(); ) {
assertTrue(b != -1);
}
@ -205,6 +205,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
// Non encrypted index file can be read even when encryption key provided.
assertStoredAndLoadedEqual(new CachedContentIndex(cacheDir),
new CachedContentIndex(cacheDir, key));
// Test multiple store() calls
CachedContentIndex index = new CachedContentIndex(cacheDir, key);
index.addNew(new CachedContent(15, "key3", 110));
index.store();
assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir, key));
}
private void assertStoredAndLoadedEqual(CachedContentIndex index, CachedContentIndex index2)

View File

@ -302,6 +302,9 @@ import javax.crypto.spec.SecretKeySpec;
}
output.writeInt(hashCode);
atomicFile.endWrite(output);
// Avoid calling close twice. Duplicate CipherOutputStream.close calls did
// not used to be no-ops: https://android-review.googlesource.com/#/c/272799/
output = null;
} catch (IOException e) {
throw new CacheException(e);
} finally {

View File

@ -67,6 +67,7 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
public void reset(OutputStream out) {
Assertions.checkState(closed);
this.out = out;
count = 0;
closed = false;
}
}