use of okio.BufferedSource in project okhttp by square.
the class RelayTest method closeBeforeExhaustLeavesDirtyFile.
@Test
public void closeBeforeExhaustLeavesDirtyFile() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghij");
Relay relay1 = Relay.edit(file, upstream, metadata, 5);
BufferedSource source1 = Okio.buffer(relay1.newSource());
assertEquals("abcdefghij", source1.readUtf8(10));
// Not exhausted!
source1.close();
assertTrue(relay1.isClosed());
try {
Relay.read(file);
fail();
} catch (IOException expected) {
assertEquals("unreadable cache file", expected.getMessage());
}
assertFile(Relay.PREFIX_DIRTY, -1L, -1, null, null);
}
use of okio.BufferedSource in project okhttp by square.
the class RelayTest method multipleSources.
@Test
public void multipleSources() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghijklm");
Relay relay = Relay.edit(file, upstream, metadata, 1024);
BufferedSource source1 = Okio.buffer(relay.newSource());
BufferedSource source2 = Okio.buffer(relay.newSource());
assertEquals("abcdefghijklm", source1.readUtf8());
assertEquals("abcdefghijklm", source2.readUtf8());
source1.close();
source2.close();
assertTrue(relay.isClosed());
assertFile(Relay.PREFIX_CLEAN, 13L, metadata.size(), "abcdefghijklm", metadata);
}
use of okio.BufferedSource in project okhttp by square.
the class RelayTest method readAfterEdit.
@Test
public void readAfterEdit() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghij");
Relay relay1 = Relay.edit(file, upstream, metadata, 5);
BufferedSource source1 = Okio.buffer(relay1.newSource());
assertEquals("abcdefghij", source1.readUtf8(10));
assertTrue(source1.exhausted());
source1.close();
assertTrue(relay1.isClosed());
// Since relay1 is closed, new sources cannot be created.
assertNull(relay1.newSource());
Relay relay2 = Relay.read(file);
assertEquals(metadata, relay2.metadata());
BufferedSource source2 = Okio.buffer(relay2.newSource());
assertEquals("abcdefghij", source2.readUtf8(10));
assertTrue(source2.exhausted());
source2.close();
assertTrue(relay2.isClosed());
// Since relay2 is closed, new sources cannot be created.
assertNull(relay2.newSource());
assertFile(Relay.PREFIX_CLEAN, 10L, metadata.size(), "abcdefghij", metadata);
}
use of okio.BufferedSource in project okhttp by square.
the class DiskLruCacheTest method readAndWriteOverlapsMaintainConsistency.
/**
* Each read sees a snapshot of the file at the time read was called. This means that two reads of
* the same key can see different data.
*/
@Test
public void readAndWriteOverlapsMaintainConsistency() throws Exception {
DiskLruCache.Editor v1Creator = cache.edit("k1");
setString(v1Creator, 0, "AAaa");
setString(v1Creator, 1, "BBbb");
v1Creator.commit();
DiskLruCache.Snapshot snapshot1 = cache.get("k1");
BufferedSource inV1 = Okio.buffer(snapshot1.getSource(0));
assertEquals('A', inV1.readByte());
assertEquals('A', inV1.readByte());
DiskLruCache.Editor v1Updater = cache.edit("k1");
setString(v1Updater, 0, "CCcc");
setString(v1Updater, 1, "DDdd");
v1Updater.commit();
DiskLruCache.Snapshot snapshot2 = cache.get("k1");
assertSnapshotValue(snapshot2, 0, "CCcc");
assertSnapshotValue(snapshot2, 1, "DDdd");
snapshot2.close();
assertEquals('a', inV1.readByte());
assertEquals('a', inV1.readByte());
assertSnapshotValue(snapshot1, 1, "BBbb");
snapshot1.close();
}
use of okio.BufferedSource in project okhttp by square.
the class DiskLruCacheTest method readFile.
private String readFile(File file) throws Exception {
BufferedSource source = Okio.buffer(fileSystem.source(file));
String result = source.readUtf8();
source.close();
return result;
}
Aggregations