1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedInputStream;
|
---|
7 | import java.io.BufferedOutputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.net.URL;
|
---|
14 | import java.net.URLConnection;
|
---|
15 | import java.util.Enumeration;
|
---|
16 | import java.util.zip.ZipEntry;
|
---|
17 | import java.util.zip.ZipFile;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Mirrors a file to a local file.
|
---|
23 | * <p>
|
---|
24 | * The file mirrored is only downloaded if it has been more than one day since last download
|
---|
25 | */
|
---|
26 | public class MirroredInputStream extends InputStream {
|
---|
27 | InputStream fs = null;
|
---|
28 | File file = null;
|
---|
29 |
|
---|
30 | public MirroredInputStream(String name) throws IOException {
|
---|
31 | this(name, null, -1L);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public MirroredInputStream(String name, long maxTime) throws IOException {
|
---|
35 | this(name, null, maxTime);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public MirroredInputStream(String name, String destDir) throws IOException {
|
---|
39 | this(name, destDir, -1L);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
|
---|
43 | URL url;
|
---|
44 | try {
|
---|
45 | url = new URL(name);
|
---|
46 | if (url.getProtocol().equals("file")) {
|
---|
47 | file = new File(name.substring("file:/".length()));
|
---|
48 | if (!file.exists()) {
|
---|
49 | file = new File(name.substring("file://".length()));
|
---|
50 | }
|
---|
51 | } else {
|
---|
52 | file = checkLocal(url, destDir, maxTime);
|
---|
53 | }
|
---|
54 | } catch (java.net.MalformedURLException e) {
|
---|
55 | if(name.startsWith("resource://")) {
|
---|
56 | fs = getClass().getResourceAsStream(
|
---|
57 | name.substring("resource:/".length()));
|
---|
58 | if (fs == null)
|
---|
59 | throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | file = new File(name);
|
---|
63 | }
|
---|
64 | if (file == null)
|
---|
65 | throw new IOException();
|
---|
66 | fs = new FileInputStream(file);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Replies an input stream for a file in a ZIP-file. Replies a file in the top
|
---|
71 | * level directory of the ZIP file which has an extension <code>extension</code>. If more
|
---|
72 | * than one files have this extension, the last file whose name includes <code>namepart</code>
|
---|
73 | * is opened.
|
---|
74 | *
|
---|
75 | * @param extension the extension of the file we're looking for
|
---|
76 | * @param namepart the name part
|
---|
77 | * @return an input stream. Null if this mirrored input stream doesn't represent a zip file or if
|
---|
78 | * there was no matching file in the ZIP file
|
---|
79 | */
|
---|
80 | public InputStream getZipEntry(String extension, String namepart) {
|
---|
81 | if (file == null)
|
---|
82 | return null;
|
---|
83 | InputStream res = null;
|
---|
84 | try {
|
---|
85 | ZipFile zipFile = new ZipFile(file);
|
---|
86 | ZipEntry resentry = null;
|
---|
87 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
---|
88 | while (entries.hasMoreElements()) {
|
---|
89 | ZipEntry entry = entries.nextElement();
|
---|
90 | if (entry.getName().endsWith("." + extension)) {
|
---|
91 | /* choose any file with correct extension. When more than
|
---|
92 | one file, prefer the one which matches namepart */
|
---|
93 | if (resentry == null || entry.getName().indexOf(namepart) >= 0) {
|
---|
94 | resentry = entry;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | if (resentry != null) {
|
---|
99 | res = zipFile.getInputStream(resentry);
|
---|
100 | } else {
|
---|
101 | zipFile.close();
|
---|
102 | }
|
---|
103 | } catch (Exception e) {
|
---|
104 | if(file.getName().endsWith(".zip")) {
|
---|
105 | System.err.println(tr("Warning: failed to open file with extension ''{2}'' and namepart ''{3}'' in zip file ''{0}''. Exception was: {1}",
|
---|
106 | file.getName(), e.toString(), extension, namepart));
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return res;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public File getFile()
|
---|
113 | {
|
---|
114 | return file;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static public void cleanup(String name)
|
---|
118 | {
|
---|
119 | cleanup(name, null);
|
---|
120 | }
|
---|
121 | static public void cleanup(String name, String destDir)
|
---|
122 | {
|
---|
123 | URL url;
|
---|
124 | try {
|
---|
125 | url = new URL(name);
|
---|
126 | if (!url.getProtocol().equals("file"))
|
---|
127 | {
|
---|
128 | String localPath = Main.pref.get("mirror." + url);
|
---|
129 | if (localPath != null && localPath.length() > 0)
|
---|
130 | {
|
---|
131 | String[] lp = localPath.split(";");
|
---|
132 | File lfile = new File(lp[1]);
|
---|
133 | if(lfile.exists()) {
|
---|
134 | lfile.delete();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | Main.pref.put("mirror." + url, null);
|
---|
138 | }
|
---|
139 | } catch (java.net.MalformedURLException e) {}
|
---|
140 | }
|
---|
141 |
|
---|
142 | private File checkLocal(URL url, String destDir, long maxTime) {
|
---|
143 | String localPath = Main.pref.get("mirror." + url);
|
---|
144 | File file = null;
|
---|
145 | if (localPath != null && localPath.length() > 0) {
|
---|
146 | String[] lp = localPath.split(";");
|
---|
147 | file = new File(lp[1]);
|
---|
148 | if (maxTime <= 0) {
|
---|
149 | maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
|
---|
150 | }
|
---|
151 | if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) {
|
---|
152 | if(file.exists())
|
---|
153 | return file;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if(destDir == null) {
|
---|
157 | destDir = Main.pref.getPreferencesDir();
|
---|
158 | }
|
---|
159 |
|
---|
160 | File destDirFile = new File(destDir);
|
---|
161 | if (!destDirFile.exists()) {
|
---|
162 | destDirFile.mkdirs();
|
---|
163 | }
|
---|
164 |
|
---|
165 | String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
|
---|
166 | localPath = "mirror_" + a;
|
---|
167 | destDirFile = new File(destDir, localPath + ".tmp");
|
---|
168 | BufferedOutputStream bos = null;
|
---|
169 | BufferedInputStream bis = null;
|
---|
170 | try {
|
---|
171 | URLConnection conn = url.openConnection();
|
---|
172 | conn.setConnectTimeout(5000);
|
---|
173 | bis = new BufferedInputStream(conn.getInputStream());
|
---|
174 | bos = new BufferedOutputStream( new FileOutputStream(destDirFile));
|
---|
175 | byte[] buffer = new byte[4096];
|
---|
176 | int length;
|
---|
177 | while ((length = bis.read(buffer)) > -1) {
|
---|
178 | bos.write(buffer, 0, length);
|
---|
179 | }
|
---|
180 | } catch(IOException ioe) {
|
---|
181 | if (file != null)
|
---|
182 | return file;
|
---|
183 | return null;
|
---|
184 | } finally {
|
---|
185 | if (bis != null) {
|
---|
186 | try {
|
---|
187 | bis.close();
|
---|
188 | } catch (IOException e) {
|
---|
189 | e.printStackTrace();
|
---|
190 | }
|
---|
191 | }
|
---|
192 | if (bos != null) {
|
---|
193 | try {
|
---|
194 | bos.close();
|
---|
195 | } catch (IOException e) {
|
---|
196 | e.printStackTrace();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | file = new File(destDir, localPath);
|
---|
200 | destDirFile.renameTo(file);
|
---|
201 | Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
|
---|
202 | }
|
---|
203 |
|
---|
204 | return file;
|
---|
205 | }
|
---|
206 | @Override
|
---|
207 | public int available() throws IOException
|
---|
208 | { return fs.available(); }
|
---|
209 | @Override
|
---|
210 | public void close() throws IOException
|
---|
211 | { fs.close(); }
|
---|
212 | @Override
|
---|
213 | public int read() throws IOException
|
---|
214 | { return fs.read(); }
|
---|
215 | @Override
|
---|
216 | public int read(byte[] b) throws IOException
|
---|
217 | { return fs.read(b); }
|
---|
218 | @Override
|
---|
219 | public int read(byte[] b, int off, int len) throws IOException
|
---|
220 | { return fs.read(b,off, len); }
|
---|
221 | @Override
|
---|
222 | public long skip(long n) throws IOException
|
---|
223 | { return fs.skip(n); }
|
---|
224 | }
|
---|