1 46 47 package org.jfree.io; 48 49 import java.io.File ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.OutputStream ; 53 import java.io.Reader ; 54 import java.io.Writer ; 55 import java.net.URL ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.StringTokenizer ; 60 61 66 public class IOUtils { 67 68 69 private static IOUtils instance; 70 71 74 private IOUtils() { 75 } 76 77 82 public static IOUtils getInstance() { 83 if (instance == null) { 84 instance = new IOUtils(); 85 } 86 return instance; 87 } 88 89 95 private boolean isFileStyleProtocol(final URL url) { 96 if (url.getProtocol().equals("http")) { 97 return true; 98 } 99 if (url.getProtocol().equals("https")) { 100 return true; 101 } 102 if (url.getProtocol().equals("ftp")) { 103 return true; 104 } 105 if (url.getProtocol().equals("file")) { 106 return true; 107 } 108 if (url.getProtocol().equals("jar")) { 109 return true; 110 } 111 return false; 112 } 113 114 120 private List parseName(final String name) { 121 final ArrayList list = new ArrayList (); 122 final StringTokenizer strTok = new StringTokenizer (name, "/"); 123 while (strTok.hasMoreElements()) { 124 final String s = (String ) strTok.nextElement(); 125 if (s.length() != 0) { 126 list.add(s); 127 } 128 } 129 return list; 130 } 131 132 139 private String formatName(final List name, final String query) { 140 final StringBuffer b = new StringBuffer (); 141 final Iterator it = name.iterator(); 142 while (it.hasNext()) { 143 b.append(it.next()); 144 if (it.hasNext()) { 145 b.append("/"); 146 } 147 } 148 if (query != null) { 149 b.append('?'); 150 b.append(query); 151 } 152 return b.toString(); 153 } 154 155 163 private int startsWithUntil(final List baseName, final List urlName) { 164 final int minIdx = Math.min(urlName.size(), baseName.size()); 165 for (int i = 0; i < minIdx; i++) { 166 final String baseToken = (String ) baseName.get(i); 167 final String urlToken = (String ) urlName.get(i); 168 if (!baseToken.equals(urlToken)) { 169 return i; 170 } 171 } 172 return minIdx; 173 } 174 175 184 private boolean isSameService(final URL url, final URL baseUrl) { 185 if (!url.getProtocol().equals(baseUrl.getProtocol())) { 186 return false; 187 } 188 if (!url.getHost().equals(baseUrl.getHost())) { 189 return false; 190 } 191 if (url.getPort() != baseUrl.getPort()) { 192 return false; 193 } 194 return true; 195 } 196 197 206 public String createRelativeURL(final URL url, final URL baseURL) { 207 if (url == null) { 208 throw new NullPointerException ("content url must not be null."); 209 } 210 if (baseURL == null) { 211 throw new NullPointerException ("baseURL must not be null."); 212 } 213 if (isFileStyleProtocol(url) && isSameService(url, baseURL)) { 214 215 final List urlName = parseName(getPath(url)); 218 final List baseName = parseName(getPath(baseURL)); 219 final String query = getQuery(url); 220 221 if (!isPath(baseURL)) { 222 baseName.remove(baseName.size() - 1); 223 } 224 225 if (url.equals(baseURL)) { 227 return (String ) urlName.get(urlName.size() - 1); 228 } 229 230 int commonIndex = startsWithUntil(urlName, baseName); 231 if (commonIndex == 0) { 232 return url.toExternalForm(); 233 } 234 235 if (commonIndex == urlName.size()) { 236 commonIndex -= 1; 246 } 247 248 final ArrayList retval = new ArrayList (); 249 if (baseName.size() >= urlName.size()) { 250 final int levels = baseName.size() - commonIndex; 251 for (int i = 0; i < levels; i++) { 252 retval.add(".."); 253 } 254 } 255 256 retval.addAll(urlName.subList(commonIndex, urlName.size())); 257 return formatName(retval, query); 258 } 259 return url.toExternalForm(); 260 } 261 262 270 private boolean isPath(final URL baseURL) { 271 if (getPath(baseURL).endsWith("/")) { 272 return true; 273 } 274 else if (baseURL.getProtocol().equals("file")) { 275 final File f = new File (getPath(baseURL)); 276 try { 277 if (f.isDirectory()) { 278 return true; 279 } 280 } 281 catch (SecurityException se) { 282 } 284 } 285 return false; 286 } 287 288 295 private String getQuery (final URL url) { 296 final String file = url.getFile(); 297 final int queryIndex = file.indexOf('?'); 298 if (queryIndex == -1) { 299 return null; 300 } 301 return file.substring(queryIndex + 1); 302 } 303 304 311 private String getPath (final URL url) { 312 final String file = url.getFile(); 313 final int queryIndex = file.indexOf('?'); 314 if (queryIndex == -1) { 315 return file; 316 } 317 return file.substring(0, queryIndex); 318 } 319 320 328 public void copyStreams(final InputStream in, final OutputStream out) 329 throws IOException { 330 copyStreams(in, out, 4096); 331 } 332 333 342 public void copyStreams(final InputStream in, final OutputStream out, 343 final int buffersize) throws IOException { 344 final byte[] bytes = new byte[buffersize]; 346 347 int bytesRead = in.read(bytes); 350 while (bytesRead > -1) { 351 out.write(bytes, 0, bytesRead); 352 bytesRead = in.read(bytes); 353 } 354 } 355 356 364 public void copyWriter(final Reader in, final Writer out) 365 throws IOException { 366 copyWriter(in, out, 4096); 367 } 368 369 379 public void copyWriter(final Reader in, final Writer out, 380 final int buffersize) 381 throws IOException { 382 final char[] bytes = new char[buffersize]; 384 385 int bytesRead = in.read(bytes); 388 while (bytesRead > -1) { 389 out.write(bytes, 0, bytesRead); 390 bytesRead = in.read(bytes); 391 } 392 } 393 394 400 public String getFileName(final URL url) { 401 final String file = url.getFile(); 402 final int last = file.lastIndexOf("/"); 403 if (last < 0) { 404 return file; 405 } 406 return file.substring(last); 407 } 408 409 415 public String stripFileExtension(final String file) { 416 final int idx = file.lastIndexOf("."); 417 if (idx < 1) { 419 return file; 420 } 421 return file.substring(0, idx); 422 } 423 424 431 public String getFileExtension(final String file) { 432 final int idx = file.lastIndexOf("."); 433 if (idx < 1) { 435 return ""; 436 } 437 return file.substring(idx); 438 } 439 440 449 public boolean isSubDirectory(File base, File child) 450 throws IOException { 451 base = base.getCanonicalFile(); 452 child = child.getCanonicalFile(); 453 454 File parentFile = child; 455 while (parentFile != null) { 456 if (base.equals(parentFile)) { 457 return true; 458 } 459 parentFile = parentFile.getParentFile(); 460 } 461 return false; 462 } 463 } 464 | Popular Tags |