KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > URLInputStream


1 /*
2   Copyright (C) 2001 Zachary Medico
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.util;
20
21 import java.io.*;
22 import java.net.*;
23
24 /**
25  * Here is an object for InputStream creation
26  * It lets you access the URL that corresponds to the created InputStream
27  *
28  */

29 public class URLInputStream {
30
31    private InputStream inputStream;
32    private URL url;
33     
34    public URLInputStream(String JavaDoc fileLocation) throws Exception JavaDoc {
35
36       try {
37          inputStream = new FileInputStream(fileLocation);
38          url=new URL("file:"+fileLocation);
39          //url = new File(fileLocation).toURL();
40
} catch ( FileNotFoundException e ) {
41          try {
42             url = getClass().getClassLoader().getResource( fileLocation );
43             inputStream = url.openStream();
44          } catch (Exception JavaDoc e2 ) {
45             // uncaught MalformedURLException
46
url = new URL(fileLocation);
47             inputStream = url.openStream();
48          }
49       }
50    }
51
52    public URL getURL() {
53       return url;
54    }
55
56    public InputStream getInputStream() {
57       return inputStream;
58    }
59     
60    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
61       if ( args.length < 1 ) {
62          System.err.println("usage: java InputStreamTest <file_path>");
63       } else {
64          for ( int i=0; i<args.length; i++) {
65             try {
66                URLInputStream urlInputStream = new URLInputStream( args[i] );
67                System.out.println( urlInputStream.getURL().toExternalForm());
68                urlInputStream.getInputStream().close();
69             } catch( Exception JavaDoc e ) {
70                System.out.println(e.getMessage());
71             }
72             System.out.println();
73          }
74       }
75         
76       System.exit(0);
77    }
78
79 }
80
Popular Tags