Example usage for android.support.v4.util AtomicFile delete

List of usage examples for android.support.v4.util AtomicFile delete

Introduction

In this page you can find the example usage for android.support.v4.util AtomicFile delete.

Prototype

public void delete() 

Source Link

Usage

From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java

private InputStream saveCacheFile(@NonNull File cacheFile, @NonNull InputStream content) throws IOException {
    final AtomicFile af = new AtomicFile(cacheFile);
    final FileOutputStream fos = af.startWrite();
    try {//from www . j av a 2s . c o  m
        final OutputStream out = new BufferPoolOutputStream(fos);
        final byte[] buffer = ByteBufferPool.getInstance().obtain();
        try {
            IOUtils.copyLarge(content, out, buffer);
            IOUtils.closeQuietly(out);
            af.finishWrite(fos);
        } finally {
            ByteBufferPool.getInstance().free(buffer);
        }
    } catch (IOException e) {
        af.failWrite(fos);
        af.delete();
        throw e;
    }
    return af.openRead();
}

From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java

private void saveMetaFile(@NonNull File metaFile, @NonNull Map<String, List<String>> metaHeaders, long maxAge)
        throws IOException {
    final AtomicFile af = new AtomicFile(metaFile);
    final FileOutputStream fos = af.startWrite();
    try {/*w w w  . j ava  2s .c  o  m*/
        final DataOutputStream dat = new DataOutputStream(new BufferPoolOutputStream(fos));
        dat.writeLong(System.currentTimeMillis() + maxAge);
        dat.writeInt(metaHeaders.size());
        for (final Map.Entry<String, List<String>> header : metaHeaders.entrySet()) {
            dat.writeUTF(header.getKey());
            dat.writeInt(header.getValue().size());
            for (final String value : header.getValue()) {
                dat.writeUTF(value);
            }
        }
        IOUtils.closeQuietly(dat);
        af.finishWrite(fos);
    } catch (IOException e) {
        af.failWrite(fos);
        af.delete();
        throw e;
    }
}