001    /*
002      Copyright (C) 2001 Zachary Medico
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.util;
020    
021    import java.io.*;
022    import java.net.*;
023    
024    /**
025     *  Here is  an object for InputStream creation
026     *  It lets you access the URL that corresponds to the created InputStream
027     *
028     */
029    public class URLInputStream {
030    
031       private InputStream inputStream;
032       private URL url;
033        
034       public URLInputStream(String fileLocation) throws Exception {        
035    
036          try {
037             inputStream = new FileInputStream(fileLocation);
038             url=new URL("file:"+fileLocation);
039             //url = new File(fileLocation).toURL();
040          } catch ( FileNotFoundException e ) {
041             try {
042                url = getClass().getClassLoader().getResource( fileLocation );
043                inputStream = url.openStream();
044             } catch (Exception e2 ) {
045                // uncaught MalformedURLException
046                url = new URL(fileLocation);
047                inputStream = url.openStream();
048             }
049          }
050       }
051    
052       public URL getURL() {
053          return url;
054       }
055    
056       public InputStream getInputStream() {
057          return inputStream;
058       }
059        
060       public static void main(String[] args) throws Exception {
061          if ( args.length < 1 ) {
062             System.err.println("usage: java InputStreamTest <file_path>");
063          } else {
064             for ( int i=0; i<args.length; i++) {
065                try {
066                   URLInputStream urlInputStream = new URLInputStream( args[i] );
067                   System.out.println( urlInputStream.getURL().toExternalForm());
068                   urlInputStream.getInputStream().close();
069                } catch( Exception e ) {
070                   System.out.println(e.getMessage());
071                }
072                System.out.println();
073             }
074          }
075            
076          System.exit(0);        
077       }    
078    
079    }