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

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

Introduction

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

Prototype

public void failWrite(FileOutputStream fileOutputStream) 

Source Link

Usage

From source file:org.chromium.chrome.browser.tabmodel.TabPersistentStore.java

/**
 * Atomically writes the given serialized data out to disk.
 * @param stateDirectory Directory to save TabModel data into.
 * @param stateFileName  File name to save TabModel data into.
 * @param listData       TabModel data in the form of a serialized byte array.
 *//*from  w w w  . j  ava  2 s .co  m*/
public static void saveListToFile(File stateDirectory, String stateFileName, byte[] listData) {
    synchronized (SAVE_LIST_LOCK) {
        // Save the index file containing the list of tabs to restore.
        File metadataFile = new File(stateDirectory, stateFileName);

        AtomicFile file = new AtomicFile(metadataFile);
        FileOutputStream stream = null;
        try {
            stream = file.startWrite();
            stream.write(listData, 0, listData.length);
            file.finishWrite(stream);
        } catch (IOException e) {
            if (stream != null)
                file.failWrite(stream);
            Log.e(TAG, "Failed to write file: " + metadataFile.getAbsolutePath());
        }
    }
}

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 {//ww w .j  a v  a  2 s .  c  om
        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  ww. j  a v  a 2 s .  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;
    }
}