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

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

Introduction

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

Prototype

public FileInputStream openRead() throws FileNotFoundException 

Source Link

Usage

From source file:io.nuclei.splice.internal.FileManager.java

public static void copy(Context context, FileRef ref, OutputStream out) throws IOException {
    AtomicFile file = getAtomicFile(context, ref);
    FileInputStream in = file.openRead();
    try {//ww w . j a v a  2 s.c o m
        copy(in, out);
    } finally {
        in.close();
    }
}

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

private long readMetaFile(@NonNull File metaFile, @NonNull Map<String, List<String>> headers)
        throws IOException {
    final AtomicFile af = new AtomicFile(metaFile);
    final FileInputStream fis = af.openRead();
    final DataInputStream dat = new DataInputStream(new BufferPoolInputStream(fis));
    try {/*from w ww.j  av a2s.  co m*/
        final long expireTime = dat.readLong();
        int headersCount = dat.readInt();
        while (headersCount-- > 0) {
            final String name = dat.readUTF();
            int valuesCount = dat.readInt();
            final List<String> values = new ArrayList<>(valuesCount);
            while (valuesCount-- > 0) {
                values.add(dat.readUTF());
            }
            headers.put(name, values);
        }
        return expireTime;
    } finally {
        IOUtils.closeQuietly(dat);
    }
}

From source file:io.nuclei.splice.ApplicationTest.java

void validateFile(String id, int type) throws Exception {
    FileRef ref = FileManager.newRef(id);
    assertTrue(ref.type == type);/*  w w  w  .  ja v  a 2 s .  c  o m*/

    AtomicFile file = FileManager.getAtomicFile(getContext(), ref);
    assertTrue(file.getBaseFile().exists());

    FileInputStream in = file.openRead();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileManager.copy(in, out);
        assertTrue(CONTENT.equals(out.toString("UTF-8")));
    } finally {
        in.close();
    }
}

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  ww  w. ja v  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();
}