001: /*
002: ** Tim Endres' utilities package.
003: ** Copyright (c) 1997 by Tim Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.util;
024:
025: import java.io.*;
026: import java.net.*;
027:
028: public class ResourceUtilities {
029: /**
030: * Copies a named resource to a File.
031: *
032: * @param resourceURL The name of the resource to copy.
033: * @param destFile The File to copy the resource's contents into.
034: */
035:
036: public static void copyResourceToFile(String resourceURL,
037: File destFile) throws IOException {
038: BufferedInputStream in = new BufferedInputStream(
039: ResourceUtilities.openNamedResource(resourceURL));
040:
041: BufferedOutputStream out = new BufferedOutputStream(
042: new FileOutputStream(destFile));
043:
044: byte[] buf = new byte[4096];
045:
046: for (;;) {
047: int numRead = in.read(buf, 0, buf.length);
048:
049: if (numRead == -1)
050: break;
051:
052: out.write(buf, 0, numRead);
053: }
054:
055: in.close();
056: out.close();
057: }
058:
059: /**
060: * Opens a resource and return an InputStream that will read
061: * the contents of the resource. A resource URL is used to name
062: * the resource. The URL can be any valid URL to which you can
063: * establish a connect, including web pages, ftp site files, and
064: * files in the CLASSPATH including JAR files.
065: * <p>
066: * To open a file on the CLASSPATH, use a full class name, with
067: * the slash syntax, such "/com/ice/util/ResourceUtilities.class".
068: * Note the leading slash.
069: *
070: * @param path The properties resource's name.
071: * @param props The system properties to add properties into.
072: * @return The InputStream that will read the resource's contents.
073: */
074:
075: public static InputStream openNamedResource(String resourceURL)
076: throws java.io.IOException {
077: InputStream in = null;
078: boolean result = false;
079: boolean httpURL = false;
080: URL propsURL = null;
081:
082: //
083: // UNDONE REVIEW
084: // I really should be getting the URL's protocol, when it
085: // is a "full" URL, and checking for the different possible
086: // error returns for http, ftp, et.al.
087: //
088: try {
089: propsURL = new URL(resourceURL);
090: } catch (MalformedURLException ex) {
091: propsURL = null;
092: }
093:
094: if (propsURL == null) {
095: propsURL = ResourceUtilities.class.getResource(resourceURL);
096:
097: if (propsURL == null && resourceURL.startsWith("FILE:")) {
098: try {
099: in = new FileInputStream(resourceURL.substring(5));
100: return in;
101: } catch (FileNotFoundException ex) {
102: in = null;
103: propsURL = null;
104: }
105: }
106: } else {
107: String protocol = propsURL.getProtocol();
108: httpURL = protocol.equals("http");
109: }
110:
111: if (propsURL != null) {
112: URLConnection urlConn = propsURL.openConnection();
113:
114: if (httpURL) {
115: String hdrVal = urlConn.getHeaderField(0);
116: if (hdrVal != null) {
117: String code = HTTPUtilities.getResultCode(hdrVal);
118:
119: if (code != null) {
120: if (!code.equals("200")) {
121: throw new java.io.IOException(
122: "status code = " + code);
123: }
124: }
125: }
126: }
127:
128: in = urlConn.getInputStream();
129: }
130:
131: if (in == null)
132: throw new java.io.IOException("could not locate resource '"
133: + resourceURL + "'");
134:
135: return in;
136: }
137:
138: }
|