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