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

Last change on this file since 2986 was 2889, checked in by stoecker, 14 years ago

close #4418 - support zip files for styles and presets

File size: 7.0 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;
15import java.util.Enumeration;
16import java.util.zip.ZipEntry;
17import java.util.zip.ZipFile;
18
19import 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 */
26public 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 public InputStream getZipEntry(String extension, String namepart)
70 {
71 InputStream res = null;
72 try {
73 if(file != null && (file.getName().endsWith(".zip")
74 || file.getName().endsWith(".ZIP")))
75 {
76 ZipFile zipFile = new ZipFile(file);
77 ZipEntry resentry = null;
78 Enumeration entries = zipFile.entries();
79 while(entries.hasMoreElements()) {
80 ZipEntry entry = (ZipEntry)entries.nextElement();
81 if(entry.getName().endsWith("."+extension)) {
82 /* choose any file with correct extension. When more than
83 one file, prefer the one which matches namepart */
84 if(resentry == null || entry.getName().indexOf(namepart) >= 0) {
85 resentry = entry;
86 }
87 }
88 }
89 if(resentry != null) {
90 res = zipFile.getInputStream(resentry);
91 }
92 else {
93 zipFile.close();
94 }
95 }
96 } catch (Exception e) {
97 System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", file.getName(), e.toString()));
98 }
99 return res;
100 }
101
102 public File getFile()
103 {
104 return file;
105 }
106
107 static public void cleanup(String name)
108 {
109 cleanup(name, null);
110 }
111 static public void cleanup(String name, String destDir)
112 {
113 URL url;
114 try {
115 url = new URL(name);
116 if (!url.getProtocol().equals("file"))
117 {
118 String localPath = Main.pref.get("mirror." + url);
119 if (localPath != null && localPath.length() > 0)
120 {
121 String[] lp = localPath.split(";");
122 File lfile = new File(lp[1]);
123 if(lfile.exists()) {
124 lfile.delete();
125 }
126 }
127 Main.pref.put("mirror." + url, null);
128 }
129 } catch (java.net.MalformedURLException e) {}
130 }
131
132 private File checkLocal(URL url, String destDir, long maxTime) {
133 String localPath = Main.pref.get("mirror." + url);
134 File file = null;
135 if (localPath != null && localPath.length() > 0) {
136 String[] lp = localPath.split(";");
137 file = new File(lp[1]);
138 if (maxTime <= 0) {
139 maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
140 }
141 if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) {
142 if(file.exists())
143 return file;
144 }
145 }
146 if(destDir == null) {
147 destDir = Main.pref.getPreferencesDir();
148 }
149
150 File destDirFile = new File(destDir);
151 if (!destDirFile.exists()) {
152 destDirFile.mkdirs();
153 }
154
155 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
156 localPath = "mirror_" + a;
157 destDirFile = new File(destDir, localPath + ".tmp");
158 BufferedOutputStream bos = null;
159 BufferedInputStream bis = null;
160 try {
161 URLConnection conn = url.openConnection();
162 conn.setConnectTimeout(5000);
163 bis = new BufferedInputStream(conn.getInputStream());
164 bos = new BufferedOutputStream( new FileOutputStream(destDirFile));
165 byte[] buffer = new byte[4096];
166 int length;
167 while ((length = bis.read(buffer)) > -1) {
168 bos.write(buffer, 0, length);
169 }
170 } catch(IOException ioe) {
171 if (file != null)
172 return file;
173 return null;
174 } finally {
175 if (bis != null) {
176 try {
177 bis.close();
178 } catch (IOException e) {
179 e.printStackTrace();
180 }
181 }
182 if (bos != null) {
183 try {
184 bos.close();
185 } catch (IOException e) {
186 e.printStackTrace();
187 }
188 }
189 file = new File(destDir, localPath);
190 destDirFile.renameTo(file);
191 Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
192 }
193
194 return file;
195 }
196 @Override
197 public int available() throws IOException
198 { return fs.available(); }
199 @Override
200 public void close() throws IOException
201 { fs.close(); }
202 @Override
203 public int read() throws IOException
204 { return fs.read(); }
205 @Override
206 public int read(byte[] b) throws IOException
207 { return fs.read(b); }
208 @Override
209 public int read(byte[] b, int off, int len) throws IOException
210 { return fs.read(b,off, len); }
211 @Override
212 public long skip(long n) throws IOException
213 { return fs.skip(n); }
214}
Note: See TracBrowser for help on using the repository browser.