source: josm/trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java@ 2876

Last change on this file since 2876 was 2832, checked in by Gubaer, 14 years ago

fixed NPE with new location for elemstyles.xml, see r2827

File size: 5.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedInputStream;
7import java.io.BufferedOutputStream;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.FileOutputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.net.URL;
14import java.net.URLConnection;
15
16import org.openstreetmap.josm.Main;
17
18/**
19 * Mirrors a file to a local file.
20 * <p>
21 * The file mirrored is only downloaded if it has been more than one day since last download
22 */
23public class MirroredInputStream extends InputStream {
24 InputStream fs = null;
25 File file = null;
26
27 public MirroredInputStream(String name) throws IOException {
28 this(name, null, -1L);
29 }
30
31 public MirroredInputStream(String name, long maxTime) throws IOException {
32 this(name, null, maxTime);
33 }
34
35 public MirroredInputStream(String name, String destDir) throws IOException {
36 this(name, destDir, -1L);
37 }
38
39 public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
40 URL url;
41 try {
42 url = new URL(name);
43 if (url.getProtocol().equals("file")) {
44 file = new File(name.substring("file:/".length()));
45 if (!file.exists()) {
46 file = new File(name.substring("file://".length()));
47 }
48 } else {
49 file = checkLocal(url, destDir, maxTime);
50 }
51 } catch (java.net.MalformedURLException e) {
52 if(name.startsWith("resource://")) {
53 fs = getClass().getResourceAsStream(
54 name.substring("resource:/".length()));
55 if (fs == null)
56 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
57 return;
58 }
59 file = new File(name);
60 }
61 if (file == null)
62 throw new IOException();
63 fs = new FileInputStream(file);
64 }
65
66 public File getFile()
67 {
68 return file;
69 }
70
71 static public void cleanup(String name)
72 {
73 cleanup(name, null);
74 }
75 static public void cleanup(String name, String destDir)
76 {
77 URL url;
78 try {
79 url = new URL(name);
80 if (!url.getProtocol().equals("file"))
81 {
82 String localPath = Main.pref.get("mirror." + url);
83 if (localPath != null && localPath.length() > 0)
84 {
85 String[] lp = localPath.split(";");
86 File lfile = new File(lp[1]);
87 if(lfile.exists()) {
88 lfile.delete();
89 }
90 }
91 Main.pref.put("mirror." + url, null);
92 }
93 } catch (java.net.MalformedURLException e) {}
94 }
95
96 private File checkLocal(URL url, String destDir, long maxTime) {
97 String localPath = Main.pref.get("mirror." + url);
98 File file = null;
99 if (localPath != null && localPath.length() > 0) {
100 String[] lp = localPath.split(";");
101 file = new File(lp[1]);
102 if (maxTime <= 0) {
103 maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
104 }
105 if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) {
106 if(file.exists())
107 return file;
108 }
109 }
110 if(destDir == null) {
111 destDir = Main.pref.getPreferencesDir();
112 }
113
114 File destDirFile = new File(destDir);
115 if (!destDirFile.exists()) {
116 destDirFile.mkdirs();
117 }
118
119 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
120 localPath = "mirror_" + a;
121 destDirFile = new File(destDir, localPath + ".tmp");
122 BufferedOutputStream bos = null;
123 BufferedInputStream bis = null;
124 try {
125 URLConnection conn = url.openConnection();
126 conn.setConnectTimeout(5000);
127 bis = new BufferedInputStream(conn.getInputStream());
128 bos = new BufferedOutputStream( new FileOutputStream(destDirFile));
129 byte[] buffer = new byte[4096];
130 int length;
131 while ((length = bis.read(buffer)) > -1) {
132 bos.write(buffer, 0, length);
133 }
134 } catch(IOException ioe) {
135 if (file != null)
136 return file;
137 return null;
138 } finally {
139 if (bis != null) {
140 try {
141 bis.close();
142 } catch (IOException e) {
143 e.printStackTrace();
144 }
145 }
146 if (bos != null) {
147 try {
148 bos.close();
149 } catch (IOException e) {
150 e.printStackTrace();
151 }
152 }
153 file = new File(destDir, localPath);
154 destDirFile.renameTo(file);
155 Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
156 }
157
158 return file;
159 }
160 @Override
161 public int available() throws IOException
162 { return fs.available(); }
163 @Override
164 public void close() throws IOException
165 { fs.close(); }
166 @Override
167 public int read() throws IOException
168 { return fs.read(); }
169 @Override
170 public int read(byte[] b) throws IOException
171 { return fs.read(b); }
172 @Override
173 public int read(byte[] b, int off, int len) throws IOException
174 { return fs.read(b,off, len); }
175 @Override
176 public long skip(long n) throws IOException
177 { return fs.skip(n); }
178}
Note: See TracBrowser for help on using the repository browser.