Changeset 2791 in osm
- Timestamp:
- 2007-05-06T19:51:00+02:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/util/Util.java
r2453 r2791 4 4 5 5 import java.awt.event.ActionListener; 6 import java.io.BufferedReader; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 6 import java.io.*; 9 7 import java.net.URL; 8 import java.util.StringTokenizer; 10 9 import java.util.regex.Matcher; 11 10 import java.util.regex.Pattern; … … 164 163 } 165 164 } 165 166 /** 167 * Mirrors a file to a local file. 168 * <p> 169 * The file mirrored is only downloaded if it has been more than one day since last download 170 * 171 * @param url The URL of the remote file 172 * @param destDir The destionation dir of the mirrored file 173 * @return The local file 174 * @throws IOException If any error reading or writing the file 175 */ 176 public static File mirror(URL url, String destDir) throws IOException 177 { 178 if( url.getProtocol().equals("file") ) 179 return new File(url.toString() ) ; 180 181 String localPath = Main.pref.get("tests.mirror." + url); 182 File oldFile = null; 183 if( localPath != null && localPath.length() > 0) 184 { 185 StringTokenizer st = new StringTokenizer(localPath, ";"); 186 long checkDate = Long.parseLong(st.nextToken()); 187 localPath = st.nextToken(); 188 oldFile = new File(localPath); 189 if( System.currentTimeMillis() - checkDate < 24 * 60 * 60 * 1000 ) 190 { 191 if( oldFile.exists() ) 192 return oldFile; 193 } 194 } 195 196 localPath = destDir + System.currentTimeMillis() + "-" + new File(url.getPath()).getName(); 197 BufferedOutputStream bos = null; 198 BufferedInputStream bis = null; 199 try 200 { 201 bis = new BufferedInputStream(url.openStream()); 202 bos = new BufferedOutputStream( new FileOutputStream(localPath) ); 203 byte[] buffer = new byte[4096]; 204 int length; 205 while( (length = bis.read( buffer )) > -1 ) 206 { 207 bos.write( buffer, 0, length ); 208 } 209 } 210 finally 211 { 212 if( bis != null ) 213 { 214 try { 215 bis.close(); 216 } catch (IOException e) { 217 e.printStackTrace(); 218 } 219 } 220 if( bos != null ) 221 { 222 try { 223 bos.close(); 224 } catch (IOException e) { 225 e.printStackTrace(); 226 } 227 } 228 } 229 230 Main.pref.put("tests.mirror." + url, System.currentTimeMillis() + ";" + localPath); 231 232 if( oldFile != null ) 233 oldFile.delete(); 234 235 return new File(localPath); 236 } 166 237 }
Note:
See TracChangeset
for help on using the changeset viewer.