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 |
|
---|
24 | public MirroredInputStream(String name) throws IOException
|
---|
25 | {
|
---|
26 | this(name, null, -1L);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public MirroredInputStream(String name, long maxTime) throws IOException
|
---|
30 | {
|
---|
31 | this(name, null, maxTime);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public MirroredInputStream(String name, String destDir, long maxTime) throws IOException
|
---|
35 | {
|
---|
36 | URL url;
|
---|
37 | File file = null;
|
---|
38 | try
|
---|
39 | {
|
---|
40 | url = new URL(name);
|
---|
41 | if(url.getProtocol().equals("file"))
|
---|
42 | {
|
---|
43 | file = new File(name.substring("file:/".length()));
|
---|
44 | if(!file.exists())
|
---|
45 | file = new File(name.substring("file://".length()));
|
---|
46 | }
|
---|
47 | else
|
---|
48 | file = checkLocal(url, destDir, maxTime);
|
---|
49 | }
|
---|
50 | catch(java.net.MalformedURLException e)
|
---|
51 | {
|
---|
52 | if(name.startsWith("resource://"))
|
---|
53 | {
|
---|
54 | fs = getClass().getResourceAsStream(
|
---|
55 | name.substring("resource:/".length()));
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | else
|
---|
59 | file = new File(name);
|
---|
60 | }
|
---|
61 | if(file == null)
|
---|
62 | throw new IOException();
|
---|
63 | fs = new FileInputStream(file);
|
---|
64 | }
|
---|
65 |
|
---|
66 | private File checkLocal(URL url, String destDir, long maxTime)
|
---|
67 | {
|
---|
68 | String localPath = Main.pref.get("mirror." + url);
|
---|
69 | File file = null;
|
---|
70 | if(localPath != null && localPath.length() > 0)
|
---|
71 | {
|
---|
72 | String[] lp = localPath.split(";");
|
---|
73 | file = new File(lp[1]);
|
---|
74 | if(maxTime <= 0)
|
---|
75 | maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
|
---|
76 | if(System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000)
|
---|
77 | {
|
---|
78 | if(file.exists())
|
---|
79 | {
|
---|
80 | return file;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if(destDir == null)
|
---|
85 | destDir = Main.pref.getPreferencesDir();
|
---|
86 |
|
---|
87 | File destDirFile = new File(destDir);
|
---|
88 | if(!destDirFile.exists() )
|
---|
89 | destDirFile.mkdirs();
|
---|
90 |
|
---|
91 | localPath = "mirror_" + new File(url.getPath()).getName();
|
---|
92 | destDirFile = new File(destDir, localPath + ".tmp");
|
---|
93 | BufferedOutputStream bos = null;
|
---|
94 | BufferedInputStream bis = null;
|
---|
95 | try
|
---|
96 | {
|
---|
97 | URLConnection conn = url.openConnection();
|
---|
98 | conn.setConnectTimeout(5000);
|
---|
99 | bis = new BufferedInputStream(conn.getInputStream());
|
---|
100 | bos = new BufferedOutputStream( new FileOutputStream(destDirFile));
|
---|
101 | byte[] buffer = new byte[4096];
|
---|
102 | int length;
|
---|
103 | while((length = bis.read(buffer)) > -1)
|
---|
104 | bos.write(buffer, 0, length);
|
---|
105 | }
|
---|
106 | catch(IOException ioe)
|
---|
107 | {
|
---|
108 | if(file != null)
|
---|
109 | return file;
|
---|
110 | else
|
---|
111 | return null;
|
---|
112 | }
|
---|
113 | finally
|
---|
114 | {
|
---|
115 | if(bis != null)
|
---|
116 | {
|
---|
117 | try
|
---|
118 | {
|
---|
119 | bis.close();
|
---|
120 | }
|
---|
121 | catch (IOException e)
|
---|
122 | {
|
---|
123 | e.printStackTrace();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | if(bos != null)
|
---|
127 | {
|
---|
128 | try
|
---|
129 | {
|
---|
130 | bos.close();
|
---|
131 | }
|
---|
132 | catch (IOException e)
|
---|
133 | {
|
---|
134 | e.printStackTrace();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | file = new File(destDir, localPath);
|
---|
138 | destDirFile.renameTo(file);
|
---|
139 | Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
|
---|
140 | }
|
---|
141 |
|
---|
142 | return file;
|
---|
143 | }
|
---|
144 | public int available() throws IOException
|
---|
145 | { return fs.available(); }
|
---|
146 | public void close() throws IOException
|
---|
147 | { fs.close(); }
|
---|
148 | public int read() throws IOException
|
---|
149 | { return fs.read(); }
|
---|
150 | public int read(byte[] b) throws IOException
|
---|
151 | { return fs.read(b); }
|
---|
152 | public int read(byte[] b, int off, int len) throws IOException
|
---|
153 | { return fs.read(b,off, len); }
|
---|
154 | public long skip(long n) throws IOException
|
---|
155 | { return fs.skip(n); }
|
---|
156 | }
|
---|