1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.BufferedInputStream;
|
---|
5 | import java.io.BufferedOutputStream;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FileOutputStream;
|
---|
9 | import java.util.Date;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Use this class if you want to cache and store a single file that gets updated regularly.
|
---|
15 | * Unless you flush() it will be kept in memory. If you want to cache a lot of data and/or files,
|
---|
16 | * use CacheFiles
|
---|
17 | * @author xeen
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public abstract class CacheCustomContent {
|
---|
21 | /**
|
---|
22 | * Common intervals
|
---|
23 | */
|
---|
24 | final static public int INTERVAL_ALWAYS = -1;
|
---|
25 | final static public int INTERVAL_HOURLY = 60*60;
|
---|
26 | final static public int INTERVAL_DAILY = INTERVAL_HOURLY * 24;
|
---|
27 | final static public int INTERVAL_WEEKLY = INTERVAL_DAILY * 7;
|
---|
28 | final static public int INTERVAL_MONTHLY = INTERVAL_WEEKLY * 4;
|
---|
29 | final static public int INTERVAL_NEVER = Integer.MAX_VALUE;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Where the data will be stored
|
---|
33 | */
|
---|
34 | private byte[] data = null;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The ident that identifies the stored file. Includes file-ending.
|
---|
38 | */
|
---|
39 | final private String ident;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * The (file-)path where the data will be stored
|
---|
43 | */
|
---|
44 | final private File path;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * How often to update the cached version
|
---|
48 | */
|
---|
49 | final private int updateInterval;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * This function will be executed when an update is required. It has to be implemented by the
|
---|
53 | * inheriting class and should use a worker if it has a long wall time as the function is
|
---|
54 | * executed in the current thread.
|
---|
55 | * @return the data to cache
|
---|
56 | */
|
---|
57 | protected abstract byte[] updateData();
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * This function serves as a comfort hook to perform additional checks if the cache is valid
|
---|
61 | * @return True if the cached copy is still valid
|
---|
62 | */
|
---|
63 | protected boolean isCacheValid() {
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Initializes the class. Note that all read data will be stored in memory until it is flushed
|
---|
69 | * by flushData().
|
---|
70 | * @param ident
|
---|
71 | * @param updateInterval
|
---|
72 | */
|
---|
73 | public CacheCustomContent(String ident, int updateInterval) {
|
---|
74 | this.ident = ident;
|
---|
75 | this.updateInterval = updateInterval;
|
---|
76 | this.path = new File(Main.pref.getPreferencesDir(), ident);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Updates data if required
|
---|
81 | * @return Returns the data
|
---|
82 | */
|
---|
83 | public byte[] updateIfRequired() {
|
---|
84 | if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
|
---|
85 | || !isCacheValid())
|
---|
86 | return updateForce();
|
---|
87 | return getData();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Updates data if required
|
---|
92 | * @return Returns the data as string
|
---|
93 | */
|
---|
94 | public String updateIfRequiredString() {
|
---|
95 | if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
|
---|
96 | || !isCacheValid())
|
---|
97 | return updateForceString();
|
---|
98 | return getDataString();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Executes an update regardless of updateInterval
|
---|
103 | * @return Returns the data
|
---|
104 | */
|
---|
105 | public byte[] updateForce() {
|
---|
106 | this.data = updateData();
|
---|
107 | saveToDisk();
|
---|
108 | Main.pref.putInteger("cache." + ident, (int)(new Date().getTime()/1000));
|
---|
109 | return data;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Executes an update regardless of updateInterval
|
---|
114 | * @return Returns the data as String
|
---|
115 | */
|
---|
116 | public String updateForceString() {
|
---|
117 | updateForce();
|
---|
118 | return new String(data);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Returns the data without performing any updates
|
---|
123 | * @return the data
|
---|
124 | */
|
---|
125 | public byte[] getData() {
|
---|
126 | if(data == null)
|
---|
127 | loadFromDisk();
|
---|
128 | return data;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Returns the data without performing any updates
|
---|
133 | * @return the data as String
|
---|
134 | */
|
---|
135 | public String getDataString() {
|
---|
136 | return new String(getData());
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Tries to load the data using the given ident from disk. If this fails, data will be updated
|
---|
141 | */
|
---|
142 | private void loadFromDisk() {
|
---|
143 | try {
|
---|
144 | BufferedInputStream input = new BufferedInputStream(new FileInputStream(path));
|
---|
145 | this.data = new byte[input.available()];
|
---|
146 | input.read(this.data);
|
---|
147 | input.close();
|
---|
148 | } catch(Exception e) {
|
---|
149 | this.data = updateForce();
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Stores the data to disk
|
---|
155 | */
|
---|
156 | private void saveToDisk() {
|
---|
157 | try {
|
---|
158 | BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(path));
|
---|
159 | output.write(this.data);
|
---|
160 | output.flush();
|
---|
161 | output.close();
|
---|
162 | } catch(Exception e) {}
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Flushes the data from memory. Class automatically reloads it from disk or updateData() if
|
---|
167 | * required
|
---|
168 | */
|
---|
169 | public void flushData() {
|
---|
170 | data = null;
|
---|
171 | }
|
---|
172 | }
|
---|