org.openstreetmap.josm.io
Class MirroredInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.openstreetmap.josm.io.MirroredInputStream
All Implemented Interfaces:
java.io.Closeable

public class MirroredInputStream
extends java.io.InputStream

Mirrors a file to a local file.

The file mirrored is only downloaded if it has been more than 7 days since last download


Field Summary
static long DEFAULT_MAXTIME
           
(package private)  java.io.File file
           
(package private)  java.io.InputStream fs
           
 
Constructor Summary
MirroredInputStream(java.lang.String name)
           
MirroredInputStream(java.lang.String name, long maxTime)
           
MirroredInputStream(java.lang.String name, java.lang.String destDir)
           
MirroredInputStream(java.lang.String name, java.lang.String destDir, long maxTime)
          Get an inputstream from a given filename, url or internal resource.
 
Method Summary
 int available()
           
private  java.io.File checkLocal(java.net.URL url, java.lang.String destDir, long maxTime)
           
static void cleanup(java.lang.String name)
           
static void cleanup(java.lang.String name, java.lang.String destDir)
           
 void close()
           
protected  java.net.HttpURLConnection connectFollowingRedirect(java.net.URL downloadUrl)
          Opens a connection for downloading a resource.
 java.io.File getFile()
           
private static java.lang.String getPrefKey(java.net.URL url, java.lang.String destDir)
          get preference key to store the location and age of the cached file.
 java.io.InputStream getZipEntry(java.lang.String extension, java.lang.String namepart)
          Replies an input stream for a file in a ZIP-file.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 long skip(long n)
           
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fs

java.io.InputStream fs

file

java.io.File file

DEFAULT_MAXTIME

public static final long DEFAULT_MAXTIME
See Also:
Constant Field Values
Constructor Detail

MirroredInputStream

public MirroredInputStream(java.lang.String name)
                    throws java.io.IOException
Throws:
java.io.IOException

MirroredInputStream

public MirroredInputStream(java.lang.String name,
                           long maxTime)
                    throws java.io.IOException
Throws:
java.io.IOException

MirroredInputStream

public MirroredInputStream(java.lang.String name,
                           java.lang.String destDir)
                    throws java.io.IOException
Throws:
java.io.IOException

MirroredInputStream

public MirroredInputStream(java.lang.String name,
                           java.lang.String destDir,
                           long maxTime)
                    throws java.io.IOException
Get an inputstream from a given filename, url or internal resource.

Parameters:
name - can be - relative or absolute file name - file:///SOME/FILE the same as above - resource://SOME/FILE file from the classpath (usually in the current *.jar) - http://... a url. It will be cached on disk.
destDir - the destination directory for the cache file. only applies for urls.
maxTime - the maximum age of the cache file (in seconds)
Throws:
java.io.IOException - when the resource with the given name could not be retrieved
Method Detail

getZipEntry

public java.io.InputStream getZipEntry(java.lang.String extension,
                                       java.lang.String namepart)
Replies an input stream for a file in a ZIP-file. Replies a file in the top level directory of the ZIP file which has an extension extension. If more than one files have this extension, the last file whose name includes namepart is opened.

Parameters:
extension - the extension of the file we're looking for
namepart - the name part
Returns:
an input stream. Null if this mirrored input stream doesn't represent a zip file or if there was no matching file in the ZIP file

getFile

public java.io.File getFile()

cleanup

public static void cleanup(java.lang.String name)

cleanup

public static void cleanup(java.lang.String name,
                           java.lang.String destDir)

getPrefKey

private static java.lang.String getPrefKey(java.net.URL url,
                                           java.lang.String destDir)
get preference key to store the location and age of the cached file. 2 resources that point to the same url, but that are to be stored in different directories will not share a cache file.


checkLocal

private java.io.File checkLocal(java.net.URL url,
                                java.lang.String destDir,
                                long maxTime)
                         throws java.io.IOException
Throws:
java.io.IOException

connectFollowingRedirect

protected java.net.HttpURLConnection connectFollowingRedirect(java.net.URL downloadUrl)
                                                       throws java.net.MalformedURLException,
                                                              java.io.IOException
Opens a connection for downloading a resource.

Manually follows redirects because HttpURLConnection.setFollowRedirects(boolean) fails if the redirect is going from a http to a https URL, see bug report.

This can causes problems when downloading from certain GitHub URLs.

Throws:
java.net.MalformedURLException
java.io.IOException

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException


JOSM