ResourceUtilities.java
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
** Tim Endres' utilities package.
** Copyright (c) 1997 by Tim Endres
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package com.sitech.ismp.ice.util;
import java.io.*;
import java.net.*;
public class
ResourceUtilities
{
/**
* Copies a named resource to a File.
*
* @param resourceURL The name of the resource to copy.
* @param destFile The File to copy the resource's contents into.
*/
public static void
copyResourceToFile( String resourceURL, File destFile )
throws IOException
{
BufferedInputStream in =
new BufferedInputStream(
ResourceUtilities.openNamedResource
( resourceURL ) );
BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream( destFile ) );
byte[] buf = new byte[ 4096 ];
for ( ; ; )
{
int numRead = in.read( buf, 0, buf.length );
if ( numRead == -1 )
break;
out.write( buf, 0, numRead );
}
in.close();
out.close();
}
/**
* Opens a resource and return an InputStream that will read
* the contents of the resource. A resource URL is used to name
* the resource. The URL can be any valid URL to which you can
* establish a connect, including web pages, ftp site files, and
* files in the CLASSPATH including JAR files.
* <p>
* To open a file on the CLASSPATH, use a full class name, with
* the slash syntax, such "/com/ice/util/ResourceUtilities.class".
* Note the leading slash.
*
* @param path The properties resource's name.
* @param props The system properties to add properties into.
* @return The InputStream that will read the resource's contents.
*/
public static InputStream
openNamedResource( String resourceURL )
throws java.io.IOException
{
InputStream in = null;
boolean result = false;
boolean httpURL = false;
URL propsURL = null;
//
// UNDONE REVIEW
// I really should be getting the URL's protocol, when it
// is a "full" URL, and checking for the different possible
// error returns for http, ftp, et.al.
//
try { propsURL = new URL( resourceURL ); }
catch ( MalformedURLException ex )
{ propsURL = null; }
if ( propsURL == null )
{
propsURL =
ResourceUtilities.class.getResource( resourceURL );
if ( propsURL == null
&& resourceURL.startsWith( "FILE:" ) )
{
try {
in = new FileInputStream
( resourceURL.substring( 5 ) );
return in;
}
catch ( FileNotFoundException ex )
{
in = null;
propsURL = null;
}
}
}
else
{
String protocol = propsURL.getProtocol();
httpURL = protocol.equals( "http" );
}
if ( propsURL != null )
{
URLConnection urlConn =
propsURL.openConnection();
if ( httpURL )
{
String hdrVal = urlConn.getHeaderField(0);
if ( hdrVal != null )
{
String code =
HTTPUtilities.getResultCode( hdrVal );
if ( code != null )
{
if ( ! code.equals( "200" ) )
{
throw new java.io.IOException
( "status code = " + code );
}
}
}
}
in = urlConn.getInputStream();
}
if ( in == null )
throw new java.io.IOException
( "could not locate resource '"
+ resourceURL + "'" );
return in;
}
}