Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package nextrek.imagecache;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import nextrek.lib.HTTPHelper;
import nextrek.lib.SIC;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class DiskLruCache {

    private DiskLruCacheBase mDiskCache;
    private static final int APP_VERSION = 1;
    private static final int VALUE_COUNT = 1;
    private static final String TAG = "DiskLruImageCache";

    public DiskLruCache(Context context, String uniqueName, int diskCacheSize) {
        try {
            enableDiskCache(context, DISK_CACHE_SDCARD);
            final File diskCacheDir = getDiskCacheDir(context, uniqueName);
            mDiskCache = DiskLruCacheBase.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean writeToFile(byte[] data, DiskLruCacheBase.Editor editor) throws IOException, FileNotFoundException {
        OutputStream fs = editor.newOutputStream(0);
        // BufferedOutputStream ostream = null;
        try {
            // ostream = new BufferedOutputStream(fs, 8192);
            fs.write(data);
            fs.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            /*
             * if (ostream != null) {
             * ostream.close();
             * ostream = null;
             * }
             */
            if (fs != null) {
                fs.close();
                fs = null;
            }
        }
    }

    /**
     * Enable caching to the phone's internal storage or SD card.
     * 
     * @param context
     *            the current context
     * @param storageDevice
     *            where to store the cached files, either {@link #DISK_CACHE_INTERNAL} or {@link #DISK_CACHE_SDCARD})
     * @return
     */
    //private static final int DISK_CACHE_INTERNAL = 0;
    private static final int DISK_CACHE_SDCARD = 1;
    private boolean isDiskCacheEnabled = false;
    protected String diskCacheDirectory;

    private boolean enableDiskCache(Context context, final int storageDevice) {
        final Context appContext = context.getApplicationContext();

        String rootDir = null;
        if ((storageDevice == DISK_CACHE_SDCARD) && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            // SD-card available
            rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + appContext.getPackageName() + "/cache";
        } else {
            final File internalCacheDir = appContext.getCacheDir();
            // apparently on some configurations this can come back as null
            if (internalCacheDir == null) {
                return (isDiskCacheEnabled = false);
            }
            rootDir = internalCacheDir.getAbsolutePath();
        }

        this.diskCacheDirectory = rootDir;

        final File outFile = new File(diskCacheDirectory);
        if (outFile.mkdirs()) {
            final File nomedia = new File(diskCacheDirectory, ".nomedia");
            try {
                nomedia.createNewFile();
            } catch (final IOException e) {
                Log.e(TAG, "Failed creating .nomedia file");
            }
        }

        isDiskCacheEnabled = outFile.exists();

        if (!isDiskCacheEnabled) {
            if (SIC.DEBUG)
                Log.w(TAG, "Failed creating disk cache directory " + diskCacheDirectory);
        } else {
            if (SIC.DEBUG)
                Log.d(TAG, "enabled write through to " + diskCacheDirectory);
        }

        return isDiskCacheEnabled;
    }

    private File getDiskCacheDir(Context context, String uniqueName) {
        return new File(this.diskCacheDirectory + File.separator + uniqueName);
    }
    
    public String getTempDir() {
        return this.diskCacheDirectory;
    }

    public void put(String key, byte[] data) {

        DiskLruCacheBase.Editor editor = null;
        try {
            editor = mDiskCache.edit(key);
            if (editor == null) {
                return;
            }

            if (writeToFile(data, editor)) {
                mDiskCache.flush();
                editor.commit();
                if (SIC.DEBUG)
                    Log.d(TAG, "put " + key);
            } else {
                editor.abort();
                Log.e(TAG, "ERROR on: image put on disk cache " + key);
            }
        } catch (IOException e) {
            Log.e(TAG, "ERROR on: image put on disk cache " + key);

            try {
                if (editor != null) {
                    editor.abort();
                }
            } catch (IOException ignored) {
            }
        } finally {
            editor = null;
        }

    }

    public byte[] get(String key) {

        DiskLruCacheBase.Snapshot snapshot = null;
        try {

            snapshot = mDiskCache.get(key);
            if (snapshot == null) {
                return null;
            }

            InputStream in = snapshot.getInputStream(0);
            if (in != null) {
                try {
                    if (SIC.DEBUG)
                        Log.d(TAG, "get " + key);
                    return HTTPHelper.toByteArray(in);
                } finally {
                    in.close();
                    in = null;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (snapshot != null) {
                snapshot.close();
            }
        }

        return null;
    }

    public void close() {
        try {
            mDiskCache.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void flush() {
        try {
            mDiskCache.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public boolean containsKey(String key) {

        boolean contained = false;
        DiskLruCacheBase.Snapshot snapshot = null;
        try {
            snapshot = mDiskCache.get(key);
            contained = snapshot != null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (snapshot != null) {
                snapshot.close();
                snapshot = null;
            }
        }

        return contained;

    }

    public void clearCache() {
        if (SIC.DEBUG)
            Log.d(TAG, "disk cache CLEARED");

        try {
            mDiskCache.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public File getCacheFolder() {
        return mDiskCache.getDirectory();
    }

}

Commits for Nextrek/Android/LibrerieNextrek/src/nextrek/imagecache/DiskLruCache.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000