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