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

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

cosmetics

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