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.InputStream;
|
---|
10 | import java.io.IOException;
|
---|
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 | private File checkLocal(URL url, String destDir, long maxTime) {
|
---|
67 | String localPath = Main.pref.get("mirror." + url);
|
---|
68 | File file = null;
|
---|
69 | if (localPath != null && localPath.length() > 0) {
|
---|
70 | String[] lp = localPath.split(";");
|
---|
71 | file = new File(lp[1]);
|
---|
72 | if (maxTime <= 0)
|
---|
73 | maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
|
---|
74 | if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) {
|
---|
75 | if(file.exists()) {
|
---|
76 | return file;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if(destDir == null)
|
---|
81 | destDir = Main.pref.getPreferencesDir();
|
---|
82 |
|
---|
83 | File destDirFile = new File(destDir);
|
---|
84 | if (!destDirFile.exists())
|
---|
85 | destDirFile.mkdirs();
|
---|
86 |
|
---|
87 | localPath = "mirror_" + new File(url.getPath()).getName();
|
---|
88 | destDirFile = new File(destDir, localPath + ".tmp");
|
---|
89 | BufferedOutputStream bos = null;
|
---|
90 | BufferedInputStream bis = null;
|
---|
91 | try {
|
---|
92 | URLConnection conn = url.openConnection();
|
---|
93 | conn.setConnectTimeout(5000);
|
---|
94 | bis = new BufferedInputStream(conn.getInputStream());
|
---|
95 | bos = new BufferedOutputStream( new FileOutputStream(destDirFile));
|
---|
96 | byte[] buffer = new byte[4096];
|
---|
97 | int length;
|
---|
98 | while ((length = bis.read(buffer)) > -1)
|
---|
99 | bos.write(buffer, 0, length);
|
---|
100 | } catch(IOException ioe) {
|
---|
101 | if (file != null)
|
---|
102 | return file;
|
---|
103 | return null;
|
---|
104 | } finally {
|
---|
105 | if (bis != null) {
|
---|
106 | try {
|
---|
107 | bis.close();
|
---|
108 | } catch (IOException e) {
|
---|
109 | e.printStackTrace();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if (bos != null) {
|
---|
113 | try {
|
---|
114 | bos.close();
|
---|
115 | } catch (IOException e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | }
|
---|
118 | }
|
---|
119 | file = new File(destDir, localPath);
|
---|
120 | destDirFile.renameTo(file);
|
---|
121 | Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
|
---|
122 | }
|
---|
123 |
|
---|
124 | return file;
|
---|
125 | }
|
---|
126 | public int available() throws IOException
|
---|
127 | { return fs.available(); }
|
---|
128 | public void close() throws IOException
|
---|
129 | { fs.close(); }
|
---|
130 | public int read() throws IOException
|
---|
131 | { return fs.read(); }
|
---|
132 | public int read(byte[] b) throws IOException
|
---|
133 | { return fs.read(b); }
|
---|
134 | public int read(byte[] b, int off, int len) throws IOException
|
---|
135 | { return fs.read(b,off, len); }
|
---|
136 | public long skip(long n) throws IOException
|
---|
137 | { return fs.skip(n); }
|
---|
138 | }
|
---|