com.devexperts.io
Class URLInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by com.devexperts.io.URLInputStream
All Implemented Interfaces:
Closeable

public class URLInputStream
extends FilterInputStream

Convenient class that opens specified URL for reading. It supports all URL formats supported by Java and also understands file names using both local and absolute paths. It properly configures outgoing connections and releases all resources when closed.

To open HTTP resources use standard HTTP URL syntax:

To open FTP resources use standard FTP URL syntax:

To open file use either local file name or standard file URL syntax:


Field Summary
protected  URLConnection connection
           
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
URLInputStream(String url)
          Creates a URLInputStream for specified URL.
 
Method Summary
 void close()
           
protected  void finalize()
           
 long getLastModified()
          This method returns last modification time from this URLInputStream.
static long getLastModified(String url)
          Returns last modification time for a specified URL.
static byte[] readURL(String url)
          Reads content for specified URL and returns it as a byte array.
 
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected final URLConnection connection
Constructor Detail

URLInputStream

public URLInputStream(String url)
               throws IOException
Creates a URLInputStream for specified URL.

Parameters:
url - the URL to open
Throws:
IOException - if an I/O error occurs
Method Detail

readURL

public static byte[] readURL(String url)
                      throws IOException
Reads content for specified URL and returns it as a byte array.

Parameters:
url - the URL to read
Throws:
IOException - if an I/O error occurs

getLastModified

public static long getLastModified(String url)
                            throws IOException
Returns last modification time for a specified URL. This method never returns 0.

Throws:
IOException - if there is some problem retrieving last modification time or it is not known.

getLastModified

public long getLastModified()
This method returns last modification time from this URLInputStream. Returns 0 when last modification time is not known.


close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class FilterInputStream
Throws:
IOException

finalize

protected void finalize()
                 throws IOException
Overrides:
finalize in class Object
Throws:
IOException


Copyright © 2013 Devexperts. All Rights Reserved.