List of usage examples for android.support.v4.util AtomicFile AtomicFile
public AtomicFile(File file)
From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java
@Nullable @Override/* w ww . j av a 2 s . c o m*/ public InputStream get(@NonNull URI uri, @Nullable Map<String, List<String>> headers) throws IOException { final File cacheFile = new File(mCacheDir, Digest.getInstance().hash(uri.toString())); final File metaFile = new File(mCacheDir, "." + cacheFile.getName()); if (cacheFile.exists() && metaFile.exists()) { if (headers == null) { return new AtomicFile(cacheFile).openRead(); } else { final long expireTime = readMetaFile(metaFile, headers); Logger.debug("expireTime=%s, systemTime=%s", HttpDate.format(expireTime), HttpDate.format(System.currentTimeMillis())); if (expireTime > System.currentTimeMillis()) { return new AtomicFile(cacheFile).openRead(); } } } return null; }
From source file:com.nttec.everychan.cache.Serializer.java
/** * ?/*from ww w . ja v a 2 s. c om*/ * @param fileCache ? */ public Serializer(FileCache fileCache) { this.fileCache = fileCache; this.tabsStateFile = new AtomicFile(new File(fileCache.getFilesDirectory(), FileCache.TABS_FILENAME)); this.kryo = new Kryo(); this.kryo.setReferences(false); this.kryo.setDefaultSerializer(TaggedFieldSerializer.class); this.kryo.register(TabsState.class, 0); this.kryo.register(TabModel.class, 1); this.kryo.register(TabsIdStack.class, 2); this.kryo.register(SerializablePage.class, 3); this.kryo.register(SerializableBoardsList.class, 4); this.kryo.register(AttachmentModel.class, 5); this.kryo.register(BadgeIconModel.class, 6); this.kryo.register(BoardModel.class, 7); this.kryo.register(DeletePostModel.class, 8); this.kryo.register(PostModel.class, 9); this.kryo.register(SendPostModel.class, 10); this.kryo.register(SimpleBoardModel.class, 11); this.kryo.register(ThreadModel.class, 12); this.kryo.register(UrlPageModel.class, 13); this.kryo.register(AttachmentModel[].class, 14); this.kryo.register(BadgeIconModel[].class, 15); this.kryo.register(BoardModel[].class, 16); this.kryo.register(DeletePostModel[].class, 17); this.kryo.register(PostModel[].class, 18); this.kryo.register(SendPostModel[].class, 19); this.kryo.register(SimpleBoardModel[].class, 20); this.kryo.register(ThreadModel[].class, 21); this.kryo.register(UrlPageModel[].class, 22); this.kryo.register(java.util.ArrayList.class, 23); this.kryo.register(java.util.LinkedList.class, 24); this.kryo.register(java.io.File.class, new FileSerializer(), 25); this.kryo.register(java.io.File[].class, 26); }
From source file:com.oscarsalguero.smartystreetsautocomplete.history.DefaultAutocompleteHistoryManager.java
private DefaultAutocompleteHistoryManager(@NonNull final File historyFile, @NonNull final SmartyStreetsApiJsonParser parser) { savedFile = new AtomicFile(historyFile); jsonParser = parser;// w w w .ja v a 2s . com addresses = new ArrayList<>(); readPlaces(); }
From source file:io.nuclei.splice.internal.FileManager.java
@VisibleForTesting public static AtomicFile getAtomicFile(Context context, FileRef ref) throws IOException { File base = getBaseFile(context, ref.type); if (base == null) throw new IOException("Unable to read/write to type: " + ref.type); return new AtomicFile(new File(base, ref.id)); }
From source file:me.tatarka.support.internal.job.JobStore.java
/** * Construct the instance of the job store. This results in a blocking read from disk. */// w ww.ja v a2 s . co m private JobStore(Context context, File dataDir) { mContext = context; mDirtyOperations = 0; File systemDir = new File(dataDir, "system"); File jobDir = new File(systemDir, "job"); jobDir.mkdirs(); mJobsFile = new AtomicFile(new File(jobDir, "jobs.xml")); mJobSet = new ArraySet<JobStatus>(); readJobMapFromDisk(mJobSet); }
From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java
@NonNull @Override/*from w w w.ja v a2 s .c o m*/ public InputStream update(@NonNull URI uri, @NonNull Map<String, List<String>> headers, long maxAge) throws IOException { final File cacheFile = new File(mCacheDir, Digest.getInstance().hash(uri.toString())); final File metaFile = new File(mCacheDir, "." + cacheFile.getName()); if (cacheFile.exists() && metaFile.exists()) { saveMetaFile(metaFile, getMetaHeaders(headers), maxAge); return new AtomicFile(cacheFile).openRead(); } throw new FileNotFoundException(cacheFile.getAbsolutePath()); }
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 {/*from w ww .j a v a 2s.co 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; } }
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 . j a v a2s . com*/ 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 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 {// w ww . j ava2 s .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: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. */// ww w. ja v a2s. c om 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()); } } }