Changeset 7248 in josm for trunk/src/org
- Timestamp:
- 2014-06-14T12:40:50+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 19 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r7140 r7248 51 51 import org.openstreetmap.josm.Main; 52 52 import org.openstreetmap.josm.data.preferences.ColorProperty; 53 import org.openstreetmap.josm.io. MirroredInputStream;53 import org.openstreetmap.josm.io.CachedFile; 54 54 import org.openstreetmap.josm.io.XmlWriter; 55 55 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 1390 1390 public void validateXML(Reader in) throws Exception { 1391 1391 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 1392 try (InputStream xsdStream = new MirroredInputStream("resource://data/preferences.xsd")) {1392 try (InputStream xsdStream = new CachedFile("resource://data/preferences.xsd").getInputStream()) { 1393 1393 Schema schema = factory.newSchema(new StreamSource(xsdStream)); 1394 1394 Validator validator = schema.newValidator(); -
trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java
r7203 r7248 16 16 import org.openstreetmap.josm.Main; 17 17 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; 18 import org.openstreetmap.josm.io. MirroredInputStream;18 import org.openstreetmap.josm.io.CachedFile; 19 19 import org.openstreetmap.josm.io.imagery.ImageryReader; 20 20 import org.xml.sax.SAXException; … … 78 78 for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) { 79 79 if (clearCache) { 80 MirroredInputStream.cleanup(source);80 CachedFile.cleanup(source); 81 81 } 82 82 try { -
trunk/src/org/openstreetmap/josm/data/projection/Projections.java
r7082 r7248 32 32 import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice; 33 33 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 34 import org.openstreetmap.josm.io. MirroredInputStream;34 import org.openstreetmap.josm.io.CachedFile; 35 35 import org.openstreetmap.josm.tools.Pair; 36 36 … … 133 133 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>"); 134 134 try ( 135 InputStream in = new MirroredInputStream("resource://data/projection/epsg");135 InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream(); 136 136 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); 137 137 ) { -
trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java
r7033 r7248 4 4 import java.io.InputStream; 5 5 6 import org.openstreetmap.josm.io. MirroredInputStream;6 import org.openstreetmap.josm.io.CachedFile; 7 7 8 8 /** … … 48 48 public NTV2GridShiftFile getShiftFile() { 49 49 if (instance == null) { 50 try (InputStream is = new MirroredInputStream(gridFileName)) {50 try (InputStream is = new CachedFile(gridFileName).getInputStream()) { 51 51 instance = new NTV2GridShiftFile(); 52 52 instance.loadGridShiftFile(is, false); -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r7240 r7248 6 6 import java.io.BufferedReader; 7 7 import java.io.IOException; 8 import java.io.InputStream; 8 9 import java.io.Reader; 9 10 import java.util.ArrayList; … … 45 46 import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference; 46 47 import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference; 47 import org.openstreetmap.josm.io. MirroredInputStream;48 import org.openstreetmap.josm.io.CachedFile; 48 49 import org.openstreetmap.josm.io.UTFInputStreamReader; 49 50 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 533 534 Main.info(tr("Adding {0} to tag checker", i)); 534 535 } 535 try ( MirroredInputStream s = newMirroredInputStream(i)) {536 try (InputStream s = new CachedFile(i).getInputStream()) { 536 537 addMapCSS(new BufferedReader(UTFInputStreamReader.create(s))); 537 538 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
r7082 r7248 24 24 import org.openstreetmap.josm.data.validation.Test; 25 25 import org.openstreetmap.josm.data.validation.TestError; 26 import org.openstreetmap.josm.io. MirroredInputStream;26 import org.openstreetmap.josm.io.CachedFile; 27 27 28 28 /** … … 53 53 if (ENGINE != null) { 54 54 try (Reader reader = new InputStreamReader( 55 new MirroredInputStream("resource://data/validator/opening_hours.js"), StandardCharsets.UTF_8)) {55 new CachedFile("resource://data/validator/opening_hours.js").getInputStream(), StandardCharsets.UTF_8)) { 56 56 ENGINE.eval(reader); 57 57 // fake country/state to not get errors on holidays -
trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
r7100 r7248 11 11 import java.io.FileNotFoundException; 12 12 import java.io.IOException; 13 import java.io.InputStream; 13 14 import java.text.MessageFormat; 14 15 import java.util.ArrayList; … … 50 51 import org.openstreetmap.josm.gui.tagging.TaggingPresets; 51 52 import org.openstreetmap.josm.gui.widgets.EditableList; 52 import org.openstreetmap.josm.io. MirroredInputStream;53 import org.openstreetmap.josm.io.CachedFile; 53 54 import org.openstreetmap.josm.io.UTFInputStreamReader; 54 55 import org.openstreetmap.josm.tools.GBC; … … 165 166 for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) { 166 167 try ( 167 MirroredInputStream s = newMirroredInputStream(source);168 InputStream s = new CachedFile(source).getInputStream(); 168 169 BufferedReader reader = new BufferedReader(UTFInputStreamReader.create(s)); 169 170 ) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r7198 r7248 30 30 import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference.MapPaintPrefHelper; 31 31 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 32 import org.openstreetmap.josm.io. MirroredInputStream;32 import org.openstreetmap.josm.io.CachedFile; 33 33 import org.openstreetmap.josm.tools.ImageProvider; 34 34 import org.openstreetmap.josm.tools.Utils; … … 228 228 229 229 private static StyleSource fromSourceEntry(SourceEntry entry) { 230 MirroredInputStream in= null;230 CachedFile cf = null; 231 231 try { 232 232 Set<String> mimes = new HashSet<>(); 233 233 mimes.addAll(Arrays.asList(XmlStyleSource.XML_STYLE_MIME_TYPES.split(", "))); 234 234 mimes.addAll(Arrays.asList(MapCSSStyleSource.MAPCSS_STYLE_MIME_TYPES.split(", "))); 235 in= newMirroredInputStream(entry.url, null,Utils.join(", ", mimes));236 String zipEntryPath = in.findZipEntryPath("mapcss", "style");235 cf = new CachedFile(entry.url).setHttpAccept(Utils.join(", ", mimes)); 236 String zipEntryPath = cf.findZipEntryPath("mapcss", "style"); 237 237 if (zipEntryPath != null) { 238 238 entry.isZip = true; … … 240 240 return new MapCSSStyleSource(entry); 241 241 } 242 zipEntryPath = in.findZipEntryPath("xml", "style");242 zipEntryPath = cf.findZipEntryPath("xml", "style"); 243 243 if (zipEntryPath != null) 244 244 return new XmlStyleSource(entry); … … 248 248 return new XmlStyleSource(entry); 249 249 else { 250 try (InputStreamReader reader = new InputStreamReader( in, StandardCharsets.UTF_8)) {250 try (InputStreamReader reader = new InputStreamReader(cf.getInputStream(), StandardCharsets.UTF_8)) { 251 251 WHILE: while (true) { 252 252 int c = reader.read(); … … 272 272 Main.warn(tr("Failed to load Mappaint styles from ''{0}''. Exception was: {1}", entry.url, e.toString())); 273 273 Main.error(e); 274 } finally {275 Utils.close(in);276 274 } 277 275 return null; -
trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java
r7185 r7248 18 18 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 19 19 import org.openstreetmap.josm.gui.preferences.SourceEntry; 20 import org.openstreetmap.josm.io. MirroredInputStream;20 import org.openstreetmap.josm.io.CachedFile; 21 21 import org.openstreetmap.josm.tools.ImageProvider; 22 22 import org.openstreetmap.josm.tools.Utils; … … 82 82 83 83 /** 84 * Returns a new {@code MirroredInputStream} to the local file containing style source (can be a text file or an archive).85 * @return A new {@code MirroredInputStream} to the local file containing style source84 * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive). 85 * @return A new {@code CachedFile} to the local file containing style source 86 86 * @throws IOException if any I/O error occurs. 87 87 * @since 7081 88 88 */ 89 public abstract MirroredInputStream getMirroredInputStream() throws IOException;89 public abstract CachedFile getCachedFile() throws IOException; 90 90 91 91 /** -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java
r7200 r7248 42 42 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.TokenMgrError; 43 43 import org.openstreetmap.josm.gui.preferences.SourceEntry; 44 import org.openstreetmap.josm.io. MirroredInputStream;44 import org.openstreetmap.josm.io.CachedFile; 45 45 import org.openstreetmap.josm.tools.CheckParameterUtil; 46 46 import org.openstreetmap.josm.tools.LanguageInfo; … … 275 275 return new ByteArrayInputStream(css.getBytes(StandardCharsets.UTF_8)); 276 276 } 277 MirroredInputStream in = getMirroredInputStream();277 CachedFile cf = getCachedFile(); 278 278 if (isZip) { 279 File file = in.getFile(); 280 Utils.close(in); 279 File file = cf.getFile(); 281 280 zipFile = new ZipFile(file, StandardCharsets.UTF_8); 282 281 zipIcons = file; … … 286 285 zipFile = null; 287 286 zipIcons = null; 288 return in;289 } 290 } 291 292 @Override 293 public MirroredInputStream getMirroredInputStream() throws IOException {294 return new MirroredInputStream(url, null,MAPCSS_STYLE_MIME_TYPES);287 return cf.getInputStream(); 288 } 289 } 290 291 @Override 292 public CachedFile getCachedFile() throws IOException { 293 return new CachedFile(url).setHttpAccept(MAPCSS_STYLE_MIME_TYPES); 295 294 } 296 295 -
trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java
r7083 r7248 29 29 import org.openstreetmap.josm.gui.mappaint.StyleSource; 30 30 import org.openstreetmap.josm.gui.preferences.SourceEntry; 31 import org.openstreetmap.josm.io. MirroredInputStream;31 import org.openstreetmap.josm.io.CachedFile; 32 32 import org.openstreetmap.josm.tools.Utils; 33 33 import org.openstreetmap.josm.tools.XmlObjectParser; … … 104 104 @Override 105 105 public InputStream getSourceInputStream() throws IOException { 106 MirroredInputStream in = getMirroredInputStream();107 InputStream zip = in.findZipEntryInputStream("xml", "style");106 CachedFile cf = getCachedFile(); 107 InputStream zip = cf.findZipEntryInputStream("xml", "style"); 108 108 if (zip != null) { 109 zipIcons = in.getFile();109 zipIcons = cf.getFile(); 110 110 return zip; 111 111 } else { 112 112 zipIcons = null; 113 return in;114 } 115 } 116 117 @Override 118 public MirroredInputStream getMirroredInputStream() throws IOException {119 return new MirroredInputStream(url, null,XML_STYLE_MIME_TYPES);113 return cf.getInputStream(); 114 } 115 } 116 117 @Override 118 public CachedFile getCachedFile() throws IOException { 119 return new CachedFile(url).setHttpAccept(XML_STYLE_MIME_TYPES); 120 120 } 121 121 -
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r7092 r7248 22 22 import java.io.File; 23 23 import java.io.IOException; 24 import java.io.InputStream; 24 25 import java.io.InputStreamReader; 25 26 import java.net.MalformedURLException; … … 89 90 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 90 91 import org.openstreetmap.josm.gui.widgets.JosmTextField; 91 import org.openstreetmap.josm.io. MirroredInputStream;92 import org.openstreetmap.josm.io.CachedFile; 92 93 import org.openstreetmap.josm.io.OsmTransferException; 93 94 import org.openstreetmap.josm.tools.GBC; … … 1044 1045 @Override 1045 1046 public void actionPerformed(ActionEvent e) { 1046 MirroredInputStream.cleanup(url);1047 CachedFile.cleanup(url); 1047 1048 reloadAvailableSources(url, sourceProviders); 1048 1049 } … … 1280 1281 } 1281 1282 1282 MirroredInputStream stream = newMirroredInputStream(url);1283 InputStream stream = new CachedFile(url).getInputStream(); 1283 1284 reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); 1284 1285 -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java
r7082 r7248 25 25 import org.openstreetmap.josm.Main; 26 26 import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; 27 import org.openstreetmap.josm.io. MirroredInputStream;27 import org.openstreetmap.josm.io.CachedFile; 28 28 import org.openstreetmap.josm.tools.XmlObjectParser; 29 29 import org.xml.sax.SAXException; … … 225 225 public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException { 226 226 Collection<TaggingPreset> tp; 227 CachedFile cf = new CachedFile(source).setHttpAccept(PRESET_MIME_TYPES); 227 228 try ( 228 MirroredInputStream s = new MirroredInputStream(source, null, PRESET_MIME_TYPES);229 229 // zip may be null, but Java 7 allows it: https://blogs.oracle.com/darcy/entry/project_coin_null_try_with 230 InputStream zip = s.findZipEntryInputStream("xml", "preset")230 InputStream zip = cf.findZipEntryInputStream("xml", "preset") 231 231 ) { 232 232 if (zip != null) { 233 zipIcons = s.getFile();234 } 235 try (InputStreamReader r = new InputStreamReader(zip == null ? s: zip, StandardCharsets.UTF_8)) {233 zipIcons = cf.getFile(); 234 } 235 try (InputStreamReader r = new InputStreamReader(zip == null ? cf.getInputStream() : zip, StandardCharsets.UTF_8)) { 236 236 tp = readAll(new BufferedReader(r), validate); 237 237 } -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r7247 r7248 28 28 29 29 /** 30 * Mirrors a file to a local file. 30 * Downloads a file and caches it on disk in order to reduce network load. 31 * 32 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get 33 * resources from the current *.jar file. (Local caching is only done for URLs.) 31 34 * <p> 32 * The file mirrored is only downloaded if it has been more than 7 days since last download 35 * The mirrored file is only downloaded if it has been more than 7 days since 36 * last download. (Time can be configured.) 37 * <p> 38 * The file content is normally accessed with {@link #getInputStream()}, but 39 * you can also get the mirrored copy with {@link #getFile()}. 33 40 */ 34 public class MirroredInputStream extends InputStream{35 41 public class CachedFile { 42 36 43 /** 37 44 * Caching strategy. … … 51 58 IfModifiedSince 52 59 } 60 protected String name; 61 protected long maxAge; 62 protected String destDir; 63 protected String httpAccept; 64 protected CachingStrategy cachingStrategy; 53 65 54 InputStream fs= null;55 File file = null;66 protected File cacheFile = null; 67 boolean initialized = false; 56 68 57 69 public static final long DEFAULT_MAXTIME = -1L; … … 59 71 60 72 /** 61 * Constructs a n input streamfrom a given filename, URL or internal resource.73 * Constructs a CachedFile object from a given filename, URL or internal resource. 62 74 * 63 75 * @param name can be:<ul> 64 76 * <li>relative or absolute file name</li> 65 77 * <li>{@code file:///SOME/FILE} the same as above</li> 78 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 66 79 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 67 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 68 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 69 * @throws IOException when the resource with the given name could not be retrieved 70 */ 71 public MirroredInputStream(String name) throws IOException { 72 this(name, null, DEFAULT_MAXTIME, null); 73 } 74 75 /** 76 * Constructs an input stream from a given filename, URL or internal resource. 77 * 80 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li></ul> 81 */ 82 public CachedFile(String name) { 83 this.name = name; 84 } 85 86 /** 87 * Set the name of the resource. 78 88 * @param name can be:<ul> 79 89 * <li>relative or absolute file name</li> 80 90 * <li>{@code file:///SOME/FILE} the same as above</li> 91 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 81 92 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 82 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 83 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 84 * @param maxTime the maximum age of the cache file (in seconds) 93 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li></ul> 94 * @return this object 95 */ 96 public CachedFile setName(String name) { 97 this.name = name; 98 return this; 99 } 100 101 /** 102 * Set maximum age of cache file. Only applies to URLs. 103 * When this time has passed after the last download of the file, the 104 * cache is considered stale and a new download will be attempted. 105 * @param maxAge the maximum cache age in seconds 106 * @return this object 107 */ 108 public CachedFile setMaxAge(long maxAge) { 109 this.maxAge = maxAge; 110 return this; 111 } 112 113 /** 114 * Set the destination directory for the cache file. Only applies to URLs. 115 * @param destDir the destination directory 116 * @return this object 117 */ 118 public CachedFile setDestDir(String destDir) { 119 this.destDir = destDir; 120 return this; 121 } 122 123 /** 124 * Set the accepted MIME types sent in the HTTP Accept header. Only applies to URLs. 125 * @param httpAccept the accepted MIME types 126 * @return this object 127 */ 128 public CachedFile setHttpAccept(String httpAccept) { 129 this.httpAccept = httpAccept; 130 return this; 131 } 132 133 /** 134 * Set the caching strategy. Only applies to URLs. 135 * @param cachingStrategy 136 * @return this object 137 */ 138 public CachedFile setCachingStrategy(CachingStrategy cachingStrategy) { 139 this.cachingStrategy = cachingStrategy; 140 return this; 141 } 142 143 public String getName() { 144 return name; 145 } 146 147 public long getMaxAge() { 148 return maxAge; 149 } 150 151 public String getDestDir() { 152 return destDir; 153 } 154 155 public String getHttpAccept() { 156 return httpAccept; 157 } 158 159 public CachingStrategy getCachingStrategy() { 160 return cachingStrategy; 161 } 162 163 /** 164 * Get InputStream to the requested resource. 165 * @return the InputStream 85 166 * @throws IOException when the resource with the given name could not be retrieved 86 167 */ 87 public MirroredInputStream(String name, long maxTime) throws IOException { 88 this(name, null, maxTime, null); 89 } 90 91 /** 92 * Constructs an input stream from a given filename, URL or internal resource. 93 * 94 * @param name can be:<ul> 95 * <li>relative or absolute file name</li> 96 * <li>{@code file:///SOME/FILE} the same as above</li> 97 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 98 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 99 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 100 * @param destDir the destination directory for the cache file. Only applies for URLs. 168 public InputStream getInputStream() throws IOException { 169 File file = getFile(); 170 if (file == null) { 171 if (name.startsWith("resource://")) { 172 InputStream is = getClass().getResourceAsStream( 173 name.substring("resource:/".length())); 174 if (is == null) 175 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name)); 176 return is; 177 } 178 } 179 return new FileInputStream(file); 180 } 181 182 /** 183 * Get local file for the requested resource. 184 * @return The local cache file for URLs. If the resource is a local file, 185 * returns just that file. 101 186 * @throws IOException when the resource with the given name could not be retrieved 102 187 */ 103 public MirroredInputStream(String name, String destDir) throws IOException { 104 this(name, destDir, DEFAULT_MAXTIME, null); 105 } 106 107 /** 108 * Constructs an input stream from a given filename, URL or internal resource. 109 * 110 * @param name can be:<ul> 111 * <li>relative or absolute file name</li> 112 * <li>{@code file:///SOME/FILE} the same as above</li> 113 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 114 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 115 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 116 * @param destDir the destination directory for the cache file. Only applies for URLs. 117 * @param maxTime the maximum age of the cache file (in seconds) 118 * @throws IOException when the resource with the given name could not be retrieved 119 */ 120 public MirroredInputStream(String name, String destDir, long maxTime) throws IOException { 121 this(name, destDir, maxTime, null); 122 } 123 124 /** 125 * Constructs an input stream from a given filename, URL or internal resource. 126 * 127 * @param name can be:<ul> 128 * <li>relative or absolute file name</li> 129 * <li>{@code file:///SOME/FILE} the same as above</li> 130 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 131 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 132 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 133 * @param destDir the destination directory for the cache file. Only applies for URLs. 134 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs. 135 * @throws IOException when the resource with the given name could not be retrieved 136 * @since 6867 137 */ 138 public MirroredInputStream(String name, String destDir, String httpAccept) throws IOException { 139 this(name, destDir, DEFAULT_MAXTIME, httpAccept); 140 } 141 142 /** 143 * Constructs an input stream from a given filename, URL or internal resource. 144 * 145 * @param name can be:<ul> 146 * <li>relative or absolute file name</li> 147 * <li>{@code file:///SOME/FILE} the same as above</li> 148 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 149 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 150 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 151 * @param destDir the destination directory for the cache file. Only applies for URLs. 152 * @param maxTime the maximum age of the cache file (in seconds) 153 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs. 154 * @throws IOException when the resource with the given name could not be retrieved 155 * @since 6867 156 */ 157 public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept) throws IOException { 158 this(name, destDir, maxTime, httpAccept, CachingStrategy.MaxAge); 159 } 160 161 /** 162 * Constructs an input stream from a given filename, URL or internal resource. 163 * 164 * @param name can be:<ul> 165 * <li>relative or absolute file name</li> 166 * <li>{@code file:///SOME/FILE} the same as above</li> 167 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 168 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 169 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 170 * @param destDir the destination directory for the cache file. Only applies for URLs. 171 * @param maxTime the maximum age of the cache file (in seconds) 172 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs. 173 * @param caching the caching strategy 174 * @throws IOException when the resource with the given name could not be retrieved 175 * @since 6867 176 */ 177 public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException { 188 public File getFile() throws IOException { 189 if (initialized) 190 return cacheFile; 191 initialized = true; 178 192 URL url; 179 193 try { 180 194 url = new URL(name); 181 195 if ("file".equals(url.getProtocol())) { 182 file = new File(name.substring("file:/".length()));183 if (! file.exists()) {184 file = new File(name.substring("file://".length()));196 cacheFile = new File(name.substring("file:/".length())); 197 if (!cacheFile.exists()) { 198 cacheFile = new File(name.substring("file://".length())); 185 199 } 186 200 } else { 187 file = checkLocal(url, destDir, maxTime, httpAccept, caching);201 cacheFile = checkLocal(url); 188 202 } 189 203 } catch (java.net.MalformedURLException e) { 190 204 if (name.startsWith("resource://")) { 191 fs = getClass().getResourceAsStream( 192 name.substring("resource:/".length())); 193 if (fs == null) 194 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name)); 195 return; 205 return null; 196 206 } else if (name.startsWith("josmdir://")) { 197 file = new File(Main.pref.getPreferencesDir(), name.substring("josmdir://".length()));207 cacheFile = new File(Main.pref.getPreferencesDir(), name.substring("josmdir://".length())); 198 208 } else { 199 file = new File(name);200 } 201 } 202 if ( file == null)209 cacheFile = new File(name); 210 } 211 } 212 if (cacheFile == null) 203 213 throw new IOException(); 204 fs = new FileInputStream(file);205 } 206 214 return cacheFile; 215 } 216 207 217 /** 208 218 * Looks for a certain entry inside a zip file and returns the entry path. … … 215 225 * @param extension the extension of the file we're looking for 216 226 * @param namepart the name part 217 * @return The zip entry path of the matching file. Null if this mirrored218 * input streamdoesn't represent a zip file or if there was no matching227 * @return The zip entry path of the matching file. Null if this cached file 228 * doesn't represent a zip file or if there was no matching 219 229 * file in the ZIP file. 220 230 */ … … 227 237 /** 228 238 * Like {@link #findZipEntryPath}, but returns the corresponding InputStream. 239 * @param extension the extension of the file we're looking for 240 * @param namepart the name part 241 * @return InputStream to the matching file. Null if this cached file 242 * doesn't represent a zip file or if there was no matching 243 * file in the ZIP file. 229 244 * @since 6148 230 245 */ … … 237 252 @SuppressWarnings("resource") 238 253 private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) { 254 File file = null; 255 try { 256 file = getFile(); 257 } catch (IOException ex) { 258 } 239 259 if (file == null) 240 260 return null; … … 270 290 271 291 /** 272 * Replies the local file. 273 * @return The local file on disk 274 */ 275 public File getFile() { 276 return file; 277 } 278 292 * Clear the cache for the given resource. 293 * This forces a fresh download. 294 * @param name the URL 295 */ 279 296 public static void cleanup(String name) { 280 297 cleanup(name, null); 281 298 } 282 299 300 /** 301 * Clear the cache for the given resource. 302 * This forces a fresh download. 303 * @param name the URL 304 * @param destDir the destination directory (see {@link #setDestDir(java.lang.String)}) 305 */ 283 306 public static void cleanup(String name, String destDir) { 284 307 URL url; … … 302 325 303 326 /** 304 * get preference key to store the location and age of the cached file.327 * Get preference key to store the location and age of the cached file. 305 328 * 2 resources that point to the same url, but that are to be stored in different 306 329 * directories will not share a cache file. … … 316 339 } 317 340 318 private File checkLocal(URL url , String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException {341 private File checkLocal(URL url) throws IOException { 319 342 String prefKey = getPrefKey(url, destDir); 320 343 long age = 0L; 344 long lMaxAge = maxAge; 321 345 Long ifModifiedSince = null; 322 346 File localFile = null; … … 327 351 localFile = null; 328 352 else { 329 if ( max Time == DEFAULT_MAXTIME330 || max Time <= 0 // arbitrary value <= 0 is deprecated353 if ( maxAge == DEFAULT_MAXTIME 354 || maxAge <= 0 // arbitrary value <= 0 is deprecated 331 355 ) { 332 maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); // one week356 lMaxAge = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); // one week 333 357 } 334 358 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0)); 335 if (age < maxTime*1000) {359 if (age < lMaxAge*1000) { 336 360 return localFile; 337 361 } 338 if (caching == CachingStrategy.IfModifiedSince) { 362 if (cachingStrategy == CachingStrategy.IfModifiedSince) { 339 363 ifModifiedSince = Long.parseLong(localPathEntry.get(0)); 340 364 } … … 382 406 } 383 407 } catch (IOException e) { 384 if (age >= maxTime*1000 && age <maxTime*1000*2) {408 if (age >= lMaxAge*1000 && age < lMaxAge*1000*2) { 385 409 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", url, e)); 386 410 return localFile; … … 463 487 } 464 488 465 @Override466 public int available() throws IOException467 { return fs.available(); }468 @Override469 public void close() throws IOException470 { Utils.close(fs); }471 @Override472 public int read() throws IOException473 { return fs.read(); }474 @Override475 public int read(byte[] b) throws IOException476 { return fs.read(b); }477 @Override478 public int read(byte[] b, int off, int len) throws IOException479 { return fs.read(b,off, len); }480 @Override481 public long skip(long n) throws IOException482 { return fs.skip(n); }483 489 } -
trunk/src/org/openstreetmap/josm/io/FileWatcher.java
r7244 r7248 59 59 throw new IllegalStateException("File watcher is not available"); 60 60 } 61 try (MirroredInputStream mis = style.getMirroredInputStream()) { 62 // Get underlying file 63 File file = mis.getFile(); 64 if (file == null) { 65 throw new IllegalArgumentException("Style "+style+" does not have a local file"); 66 } 67 // Get parent directory as WatchService allows only to monitor directories, not single files 68 File dir = file.getParentFile(); 69 if (dir == null) { 70 throw new IllegalArgumentException("Style "+style+" does not have a parent directory"); 71 } 72 synchronized(this) { 73 // Register directory. Can be called several times for a same directory without problem 74 // (it returns the same key so it should not send events several times) 75 dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); 76 styleMap.put(file.toPath(), style); 77 } 61 CachedFile cf = style.getCachedFile(); 62 // Get underlying file 63 File file = cf.getFile(); 64 if (file == null) { 65 throw new IllegalArgumentException("Style "+style+" does not have a local file"); 66 } 67 // Get parent directory as WatchService allows only to monitor directories, not single files 68 File dir = file.getParentFile(); 69 if (dir == null) { 70 throw new IllegalArgumentException("Style "+style+" does not have a parent directory"); 71 } 72 synchronized(this) { 73 // Register directory. Can be called several times for a same directory without problem 74 // (it returns the same key so it should not send events several times) 75 dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); 76 styleMap.put(file.toPath(), style); 78 77 } 79 78 } -
trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java
r7242 r7248 18 18 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 19 19 import org.openstreetmap.josm.data.imagery.Shape; 20 import org.openstreetmap.josm.io. MirroredInputStream;20 import org.openstreetmap.josm.io.CachedFile; 21 21 import org.openstreetmap.josm.io.UTFInputStreamReader; 22 22 import org.xml.sax.Attributes; … … 50 50 SAXParserFactory factory = SAXParserFactory.newInstance(); 51 51 factory.setNamespaceAware(true); 52 try (InputStream in = new MirroredInputStream(source, null, 1*MirroredInputStream.DAYS, null, 53 MirroredInputStream.CachingStrategy.IfModifiedSince)) { 52 try (InputStream in = new CachedFile(source) 53 .setMaxAge(1*CachedFile.DAYS) 54 .setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince) 55 .getInputStream()) { 54 56 InputSource is = new InputSource(UTFInputStreamReader.create(in)); 55 57 factory.newSAXParser().parse(is, parser); -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r7242 r7248 22 22 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 23 23 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 24 import org.openstreetmap.josm.io. MirroredInputStream;24 import org.openstreetmap.josm.io.CachedFile; 25 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 26 import org.xml.sax.SAXException; … … 125 125 URL url = new URL(pi.downloadlink); 126 126 synchronized(this) { 127 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);127 downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null); 128 128 } 129 129 try ( -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r7134 r7248 59 59 import org.openstreetmap.josm.Main; 60 60 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 61 import org.openstreetmap.josm.io.MirroredInputStream;62 61 import org.openstreetmap.josm.plugins.PluginHandler; 63 62 import org.w3c.dom.Element; … … 75 74 import com.kitfox.svg.SVGException; 76 75 import com.kitfox.svg.SVGUniverse; 76 import org.openstreetmap.josm.io.CachedFile; 77 77 78 78 /** … … 533 533 534 534 private static ImageResource getIfAvailableHttp(String url, ImageType type) { 535 try (MirroredInputStream is = new MirroredInputStream(url, 536 new File(Main.pref.getCacheDirectory(), "images").getPath())) { 535 CachedFile cf = new CachedFile(url) 536 .setDestDir(new File(Main.pref.getCacheDirectory(), "images").getPath()); 537 try (InputStream is = cf.getInputStream()) { 537 538 switch (type) { 538 539 case SVG: 539 URI uri = getSvgUniverse().loadSVG(is, Utils.fileToURL( is.getFile()).toString());540 URI uri = getSvgUniverse().loadSVG(is, Utils.fileToURL(cf.getFile()).toString()); 540 541 SVGDiagram svg = getSvgUniverse().getDiagram(uri); 541 542 return svg == null ? null : new ImageResource(svg); … … 543 544 BufferedImage img = null; 544 545 try { 545 img = read(Utils.fileToURL( is.getFile()), false, false);546 img = read(Utils.fileToURL(cf.getFile()), false, false); 546 547 } catch (IOException e) { 547 548 Main.warn("IOException while reading HTTP image: "+e.getMessage()); … … 800 801 }); 801 802 802 try (InputStream is = new MirroredInputStream( 803 base + fn, 804 new File(Main.pref.getPreferencesDir(), "images").toString()) 805 ) { 803 CachedFile cf = new CachedFile(base + fn).setDestDir(new File(Main.pref.getPreferencesDir(), "images").toString()); 804 try (InputStream is = cf.getInputStream()) { 806 805 parser.parse(new InputSource(is)); 807 806 } -
trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java
r7193 r7248 11 11 import org.openstreetmap.josm.data.osm.DataSet; 12 12 import org.openstreetmap.josm.data.osm.Way; 13 import org.openstreetmap.josm.io.CachedFile; 13 14 import org.openstreetmap.josm.io.IllegalDataException; 14 import org.openstreetmap.josm.io.MirroredInputStream;15 15 import org.openstreetmap.josm.io.OsmReader; 16 16 import org.openstreetmap.josm.tools.GeoPropertyIndex.GeoProperty; … … 67 67 private static void initialize() { 68 68 leftHandTrafficPolygons = new ArrayList<>(); 69 try (InputStream is = new MirroredInputStream("resource://data/left-right-hand-traffic.osm")) {69 try (InputStream is = new CachedFile("resource://data/left-right-hand-traffic.osm").getInputStream()) { 70 70 DataSet data = OsmReader.parseDataSet(is, null); 71 71 for (Way w : data.getWays()) { -
trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java
r7033 r7248 28 28 29 29 import org.openstreetmap.josm.Main; 30 import org.openstreetmap.josm.io. MirroredInputStream;30 import org.openstreetmap.josm.io.CachedFile; 31 31 import org.xml.sax.Attributes; 32 32 import org.xml.sax.ContentHandler; … … 281 281 public Iterable<Object> startWithValidation(final Reader in, String namespace, String schemaSource) throws SAXException { 282 282 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 283 try (InputStream mis = new MirroredInputStream(schemaSource)) {283 try (InputStream mis = new CachedFile(schemaSource).getInputStream()) { 284 284 Schema schema = factory.newSchema(new StreamSource(mis)); 285 285 ValidatorHandler validator = schema.newValidatorHandler();
Note:
See TracChangeset
for help on using the changeset viewer.