Source code: com/eireneh/util/NetUtil.java
1
2 package com.eireneh.util;
3
4 import java.util.*;
5 import java.io.*;
6 import java.net.*;
7
8 /**
9 * The NetUtil class looks after general utility stuff around the
10 * java.net package.
11 *
12 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
13 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
14 * Distribution Licence:<br />
15 * Project B is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General Public License,
17 * version 2 as published by the Free Software Foundation.<br />
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.<br />
22 * The License is available on the internet
23 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
24 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
26 * The copyright to this program is held by it's authors.
27 * </font></td></tr></table>
28 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
29 * @see docs.Licence
30 * @author Joe Walker
31 * @version D0.I0.T0
32 */
33 public class NetUtil
34 {
35 /**
36 * Basic constructor - ensure that we can't be instansiated
37 */
38 private NetUtil()
39 {
40 }
41
42 /**
43 * If the directory does not exist, create it.
44 * Note this currently only works with file: type URLs
45 * @param orig The URL to check
46 */
47 public static void makeDirectory(URL orig) throws MalformedURLException
48 {
49 if (!orig.getProtocol().equals("file"))
50 throw new MalformedURLException("The given URL '"+orig+"' is not a file: URL.");
51
52 File file = new File(orig.getFile());
53
54 // If it is a file, except
55 if (file.isFile())
56 throw new MalformedURLException("The given URL '"+orig+"' is a file.");
57
58 // Is it already a directory ?
59 if (!file.isDirectory())
60 {
61 file.mkdirs();
62
63 // Did that work?
64 if (!file.isDirectory())
65 throw new MalformedURLException("The given URL '"+orig+"' could not be created as a directory.");
66 }
67 }
68
69 /**
70 * If the file does not exist, create it.
71 * Note this currently only works with file: type URLs
72 * @param orig The URL to check
73 */
74 public static void makeFile(URL orig) throws MalformedURLException, IOException
75 {
76 if (!orig.getProtocol().equals("file"))
77 throw new MalformedURLException("The given URL '"+orig+"' is not a file: URL.");
78
79 File file = new File(orig.getFile());
80
81 // If it is a file, except
82 if (file.isDirectory())
83 throw new MalformedURLException("The given URL '"+orig+"' is a directory.");
84
85 // Is it already a directory ?
86 if (!file.isFile())
87 {
88 FileOutputStream fout = new FileOutputStream(file);
89 fout.close();
90
91 // Did that work?
92 if (!file.isFile())
93 throw new MalformedURLException("The given URL '"+orig+"' could not be created as a file.");
94 }
95 }
96
97 /**
98 * If there is a file at the other end of this URL return true.
99 * Note this currently only works with file: type URLs
100 * @param orig The URL to check
101 * @return true if the URL points at a file
102 */
103 public static boolean isFile(URL orig) throws MalformedURLException
104 {
105 if (!orig.getProtocol().equals("file"))
106 throw new MalformedURLException("The given URL '"+orig+"' is not a file: URL.");
107
108 File file = new File(orig.getFile());
109 return file.isFile();
110 }
111
112 /**
113 * If there is a directory at the other end of this URL return true.
114 * Note this currently only works with file: type URLs
115 * @param orig The URL to check
116 * @return true if the URL points at a directory
117 */
118 public static boolean isDirectory(URL orig) throws MalformedURLException
119 {
120 if (!orig.getProtocol().equals("file"))
121 throw new MalformedURLException("The given URL '"+orig+"' is not a file: URL.");
122
123 File file = new File(orig.getFile());
124 return file.isDirectory();
125 }
126
127 /**
128 * Move a URL from one place to another. Currently this only works for
129 * file: URLs, however the interface should not need to change to
130 * handle more complex URLs
131 * @param old_url The URL to move
132 * @param new_url The desitination URL
133 */
134 public static boolean move(URL old_url, URL new_url) throws IOException
135 {
136 if (!old_url.getProtocol().equals("file"))
137 throw new MalformedURLException("The given source URL '"+old_url+"' is not a file: URL.");
138
139 if (!new_url.getProtocol().equals("file"))
140 throw new MalformedURLException("The given destination URL '"+new_url+"' is not a file: URL.");
141
142 File old_file = new File(old_url.getFile());
143 File new_file = new File(new_url.getFile());
144 return old_file.renameTo(new_file);
145 }
146
147 /**
148 * Delete a URL. Currently this only works for file: URLs, however
149 * the interface should not need to change to handle more complex URLs
150 * @param url The URL to delete
151 */
152 public static boolean delete(URL orig) throws IOException
153 {
154 if (!orig.getProtocol().equals("file"))
155 throw new MalformedURLException("The given URL '"+orig+"' is not a file: URL.");
156
157 File file = new File(orig.getFile());
158 return file.delete();
159 }
160
161 /**
162 * Utility to strip a string from the end of a URL.
163 * @param orig The URL to strip
164 * @param strip The text to strip from the end of the URL
165 * @return The stripped URL
166 * @exception MalformedURLException If the URL does not end in the given text
167 */
168 public static URL shortenURL(URL orig, String strip) throws MalformedURLException
169 {
170 String file = orig.getFile();
171 if (file.endsWith("/")) file = file.substring(0, file.length()-1);
172 if (file.endsWith("\\")) file = file.substring(0, file.length()-1);
173
174 String test = file.substring(file.length() - strip.length());
175
176 if (!test.equals(strip))
177 throw new MalformedURLException("The URL '"+orig+"' does not end in '"+strip+"'");
178
179 String new_file = file.substring(0, file.length() - strip.length());
180
181 return new URL(orig.getProtocol(),
182 orig.getHost(),
183 orig.getPort(),
184 new_file);
185 }
186
187 /**
188 * Utility to add a string to the end of a URL.
189 * @param orig The URL to strip
190 * @param extra The text to add to the end of the URL
191 * @return The stripped URL
192 * @exception MalformedURLException If the URL is not valid
193 */
194 public static URL lengthenURL(URL orig, String extra) throws MalformedURLException
195 {
196 if (orig.getProtocol().equals("file"))
197 {
198 return new URL(orig.getProtocol(),
199 orig.getHost(),
200 orig.getPort(),
201 orig.getFile()+File.separator+extra);
202 }
203 else
204 {
205 return new URL(orig.getProtocol(),
206 orig.getHost(),
207 orig.getPort(),
208 orig.getFile()+"/"+extra);
209 }
210 }
211
212 /**
213 * Utility to add a string to the end of a URL.
214 * @param orig The URL to strip
215 * @param extra1 The text to add to the end of the URL
216 * @param extra2 The next bit of text to add to the end of the URL
217 * @return The stripped URL
218 * @exception MalformedURLException If the URL is not valid
219 */
220 public static URL lengthenURL(URL orig, String extra1, String extra2) throws MalformedURLException
221 {
222 if (orig.getProtocol().equals("file"))
223 {
224 return new URL(orig.getProtocol(),
225 orig.getHost(),
226 orig.getPort(),
227 orig.getFile()+File.separator+extra1+File.separator+extra2);
228 }
229 else
230 {
231 return new URL(orig.getProtocol(),
232 orig.getHost(),
233 orig.getPort(),
234 orig.getFile()+"/"+extra1+"/"+extra2);
235 }
236 }
237
238 /**
239 * Utility to add a string to the end of a URL.
240 * @param orig The URL to strip
241 * @param extra1 The text to add to the end of the URL
242 * @param extra2 The next bit of text to add to the end of the URL
243 * @param extra3 The next bit of text to add to the end of the URL
244 * @return The stripped URL
245 * @exception MalformedURLException If the URL is not valid
246 */
247 public static URL lengthenURL(URL orig, String extra1, String extra2, String extra3) throws MalformedURLException
248 {
249 if (orig.getProtocol().equals("file"))
250 {
251 return new URL(orig.getProtocol(),
252 orig.getHost(),
253 orig.getPort(),
254 orig.getFile()+
255 File.separator+extra1+
256 File.separator+extra2+
257 File.separator+extra3);
258 }
259 else
260 {
261 return new URL(orig.getProtocol(),
262 orig.getHost(),
263 orig.getPort(),
264 orig.getFile()+"/"+extra1+"/"+extra2+"/"+extra3);
265 }
266 }
267
268 /**
269 * Attempt to obtain an OutputStream from a URL. The simple case will
270 * just call url.openConnection().getOutputStream(), however in some
271 * JVMs (MS at least this fails where new FileOutputStream(url) works.
272 * So if openConnection().getOutputStream() fails and the protocol is
273 * file, then the alternate version is used.
274 * @param url The URL to attempt to write to
275 * @return An OutputStream connection
276 */
277 public static OutputStream getOutputStream(URL url) throws IOException
278 {
279 return getOutputStream(url, false);
280 }
281
282 /**
283 * Attempt to obtain an OutputStream from a URL. The simple case will
284 * just call url.openConnection().getOutputStream(), however in some
285 * JVMs (MS at least this fails where new FileOutputStream(url) works.
286 * So if openConnection().getOutputStream() fails and the protocol is
287 * file, then the alternate version is used.
288 * @param url The URL to attempt to write to
289 * @param append Do we write to the end of the file instead of the beginning
290 * @return An OutputStream connection
291 */
292 public static OutputStream getOutputStream(URL url, boolean append) throws IOException
293 {
294 // We favour the FileOutputStream method here because append
295 // is not well defined for the openConnection method
296
297 if (url.getProtocol().equals("file"))
298 {
299 return new FileOutputStream(url.getFile(), append);
300 }
301 else
302 {
303 return url.openConnection().getOutputStream();
304 }
305 }
306 }