Android Open Source - ion File Cache Store






From Project

Back to project page ion.

License

The source code is released under:

Apache License

If you think the Android project ion listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.koushikdutta.ion;
//w ww . jav  a  2 s. com
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.async.parser.AsyncParser;
import com.koushikdutta.async.parser.DocumentParser;
import com.koushikdutta.async.parser.StringParser;
import com.koushikdutta.async.stream.FileDataSink;
import com.koushikdutta.async.util.FileCache;
import com.koushikdutta.ion.gson.GsonArrayParser;
import com.koushikdutta.ion.gson.GsonObjectParser;
import com.koushikdutta.ion.gson.GsonSerializer;

import org.w3c.dom.Document;

import java.io.File;

/**
 * Created by koush on 11/17/13.
 */
public class FileCacheStore {
    Ion ion;
    FileCache cache;
    String rawKey;
    FileCacheStore(Ion ion, FileCache cache, String rawKey) {
        this.ion = ion;
        this.cache = cache;
        this.rawKey = rawKey;
    }

    private <T> Future<T> put(final T value, final AsyncParser<T> parser) {
        final SimpleFuture<T> ret = new SimpleFuture<T>();
        Ion.getIoExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                final String key = computeKey();
                final File file = cache.getTempFile();
                final FileDataSink sink = new FileDataSink(ion.getServer(), file);
                parser.write(sink, value, new CompletedCallback() {
                    @Override
                    public void onCompleted(Exception ex) {
                        sink.end();
                        if (ex != null) {
                            file.delete();
                            ret.setComplete(ex);
                            return;
                        }
                        cache.commitTempFiles(key, file);
                        ret.setComplete(value);
                    }
                });
            }
        });
        return ret;
    }

    public Future<String> putString(String value) {
        return put(value, new StringParser());
    }

    public Future<JsonObject> putJsonObject(JsonObject value) {
        return put(value, new GsonObjectParser());
    }

    public Future<Document> putDocument(Document value) {
        return put(value, new DocumentParser());
    }

    public Future<JsonArray> putJsonArray(JsonArray value) {
        return put(value, new GsonArrayParser());
    }

    /*
    public Future<InputStream> putInputStream(InputStream value) {
        throw new AssertionError("not implemented");
    }

    public Future<byte[]> putByteArray(byte[] bytes) {
        throw new AssertionError("not implemented");
    }
    */

    public <T> Future<T> put(T value, Class<T> clazz) {
        return put(value, new GsonSerializer<T>(ion.configure().getGson(), clazz));
    }

    public <T> Future<T> put(T value, TypeToken<T> token) {
        return put(value, new GsonSerializer<T>(ion.configure().getGson(), token));
    }

    private <T> Future<T> as(final AsyncParser<T> parser) {
        final SimpleFuture<T> ret = new SimpleFuture<T>();

        Ion.getIoExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    final String key = computeKey();
                    final File file = cache.getFile(key);
                    if (!file.exists()) {
                        ret.setComplete((T)null);
                        return;
                    }
                    ion.build(ion.getContext()).load(file)
                    .as(parser)
                    .setCallback(ret.getCompletionCallback());
                }
                catch (Exception e) {
                    ret.setComplete(e);
                }
            }
        });
        
        return ret;
    }

    private <T> T get(final AsyncParser<T> parser) {
        try {
            final String key = computeKey();
            final File file = cache.getFile(key);
            return ion.build(ion.getContext()).load(file)
            .as(parser)
            .get();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String getString() {
        return get(new StringParser());
    }

    public Future<String> asString() {
        return as(new StringParser());
    }

    public Future<JsonObject> asJsonObject() {
        return as(new GsonObjectParser());
    }

    public JsonObject getJsonObject() {
        return get(new GsonObjectParser());
    }

    public Future<JsonArray> asJsonArray() {
        return as(new GsonArrayParser());
    }

    public JsonArray getJsonArray() {
        return get(new GsonArrayParser());
    }

    public Future<Document> asDocument() {
        return as(new DocumentParser());
    }

    public Document getDocument() {
        return get(new DocumentParser());
    }

    public <T> Future<T> as(Class<T> clazz) {
        return as(new GsonSerializer<T>(ion.configure().getGson(), clazz));
    }

    public <T> T get(Class<T> clazz) {
        return get(new GsonSerializer<T>(ion.configure().getGson(), clazz));
    }

    public <T> Future<T> as(TypeToken<T> token) {
        return as(new GsonSerializer<T>(ion.configure().getGson(), token));
    }

    public <T> T get(TypeToken<T> token) {
        return get(new GsonSerializer<T>(ion.configure().getGson(), token));
    }

    private String computeKey() {
        return rawKey.replace(":", "_");
    }

    public void remove() {
        final String key = computeKey();
        cache.remove(key);
    }
}




Java Source Code List

com.koushikdutta.ion.BitmapCallback.java
com.koushikdutta.ion.BitmapFetcher.java
com.koushikdutta.ion.BitmapInfoToBitmap.java
com.koushikdutta.ion.ContextReference.java
com.koushikdutta.ion.DefaultTransform.java
com.koushikdutta.ion.DeferredLoadBitmap.java
com.koushikdutta.ion.FileCacheStore.java
com.koushikdutta.ion.HeadersCallback.java
com.koushikdutta.ion.HeadersResponse.java
com.koushikdutta.ion.ImageViewBitmapInfo.java
com.koushikdutta.ion.ImageViewFutureImpl.java
com.koushikdutta.ion.InputStreamParser.java
com.koushikdutta.ion.IonBitmapRequestBuilder.java
com.koushikdutta.ion.IonDrawable.java
com.koushikdutta.ion.IonImageViewRequestBuilder.java
com.koushikdutta.ion.IonLog.java
com.koushikdutta.ion.IonRequestBuilder.java
com.koushikdutta.ion.Ion.java
com.koushikdutta.ion.LoadBitmapBase.java
com.koushikdutta.ion.LoadBitmapEmitter.java
com.koushikdutta.ion.LoadBitmapRegion.java
com.koushikdutta.ion.LoadBitmap.java
com.koushikdutta.ion.LoadDeepZoom.java
com.koushikdutta.ion.Loader.java
com.koushikdutta.ion.ProgressCallback.java
com.koushikdutta.ion.RequestBodyUploadObserver.java
com.koushikdutta.ion.Response.java
com.koushikdutta.ion.ScaleMode.java
com.koushikdutta.ion.TransformBitmap.java
com.koushikdutta.ion.bitmap.BitmapDecodeException.java
com.koushikdutta.ion.bitmap.BitmapInfo.java
com.koushikdutta.ion.bitmap.Exif.java
com.koushikdutta.ion.bitmap.IonBitmapCache.java
com.koushikdutta.ion.bitmap.LocallyCachedStatus.java
com.koushikdutta.ion.bitmap.LruBitmapCache.java
com.koushikdutta.ion.bitmap.MarkableInputStream.java
com.koushikdutta.ion.bitmap.PostProcess.java
com.koushikdutta.ion.bitmap.SoftReferenceHashtable.java
com.koushikdutta.ion.bitmap.Transform.java
com.koushikdutta.ion.builder.AnimateGifMode.java
com.koushikdutta.ion.builder.BitmapBuilder.java
com.koushikdutta.ion.builder.BitmapFutureBuilder.java
com.koushikdutta.ion.builder.Builders.java
com.koushikdutta.ion.builder.FutureBuilder.java
com.koushikdutta.ion.builder.GsonFutureBuilder.java
com.koushikdutta.ion.builder.ImageViewBuilder.java
com.koushikdutta.ion.builder.ImageViewFutureBuilder.java
com.koushikdutta.ion.builder.LoadBuilder.java
com.koushikdutta.ion.builder.LoadImageViewFutureBuilder.java
com.koushikdutta.ion.builder.MultipartBodyBuilder.java
com.koushikdutta.ion.builder.RequestBuilder.java
com.koushikdutta.ion.builder.UrlEncodedBuilder.java
com.koushikdutta.ion.conscrypt.ConscryptMiddleware.java
com.koushikdutta.ion.cookie.CookieMiddleware.java
com.koushikdutta.ion.future.ImageViewFuture.java
com.koushikdutta.ion.future.ResponseFuture.java
com.koushikdutta.ion.gif.GifDecoder.java
com.koushikdutta.ion.gif.GifFrame.java
com.koushikdutta.ion.gson.GsonArrayParser.java
com.koushikdutta.ion.gson.GsonBody.java
com.koushikdutta.ion.gson.GsonObjectParser.java
com.koushikdutta.ion.gson.GsonParser.java
com.koushikdutta.ion.gson.GsonSerializer.java
com.koushikdutta.ion.gson.PojoBody.java
com.koushikdutta.ion.loader.AssetLoader.java
com.koushikdutta.ion.loader.AsyncHttpRequestFactory.java
com.koushikdutta.ion.loader.ContentLoader.java
com.koushikdutta.ion.loader.FileLoader.java
com.koushikdutta.ion.loader.HttpLoader.java
com.koushikdutta.ion.loader.InputStreamDataEmitterFuture.java
com.koushikdutta.ion.loader.MediaFile.java
com.koushikdutta.ion.loader.MtpConstants.java
com.koushikdutta.ion.loader.PackageIconLoader.java
com.koushikdutta.ion.loader.ResourceLoader.java
com.koushikdutta.ion.loader.SimpleLoader.java
com.koushikdutta.ion.loader.StreamLoader.java
com.koushikdutta.ion.loader.VideoLoader.java
com.koushikdutta.ion.sample.DeepZoomSample.java
com.koushikdutta.ion.sample.GallerySample.java
com.koushikdutta.ion.sample.GoogleImageSearch.java
com.koushikdutta.ion.sample.ImageViewSample.java
com.koushikdutta.ion.sample.LollipopTransitionFullscreen.java
com.koushikdutta.ion.sample.LollipopTransition.java
com.koushikdutta.ion.sample.ProgressBarDownload.java
com.koushikdutta.ion.sample.ProgressBarUpload.java
com.koushikdutta.ion.sample.SampleApplication.java
com.koushikdutta.ion.sample.Samples.java
com.koushikdutta.ion.sample.SquaredImageView.java
com.koushikdutta.ion.sample.TwitterGson.java
com.koushikdutta.ion.sample.Twitter.java