1 package fr.free.miniupnp.libnatpmp; 2 3 /** I (Leah X Schmidt) copied this code from jnaerator, because 4 JNAerator's extractor requires you to buy into the whole JNA 5 concept. 6 7 JNAErator is 8 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 9 10 JNAerator is free software: you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 14 15 JNAerator is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 22 23 */ 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.net.URL; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.jar.JarEntry; 32 import java.util.jar.JarInputStream; 33 34 public class URLUtils { 35 getResource(Class<?> cl, String path)36 public static URL getResource(Class<?> cl, String path) throws IOException { 37 String clp = cl.getName().replace('.', '/') + ".class"; 38 URL clu = cl.getClassLoader().getResource(clp); 39 String s = clu.toString(); 40 if (s.endsWith(clp)) 41 return new URL(s.substring(0, s.length() - clp.length()) + path); 42 43 if (s.startsWith("jar:")) { 44 String[] ss = s.split("!"); 45 return new URL(ss[1] + "!/" + path); 46 } 47 return null; 48 } 49 listFiles(URL directory)50 public static List<URL> listFiles(URL directory) throws IOException { 51 List<URL> ret = new ArrayList<URL>(); 52 String s = directory.toString(); 53 if (s.startsWith("jar:")) { 54 String[] ss = s.substring("jar:".length()).split("!"); 55 String path = ss[1]; 56 URL target = new URL(ss[0]); 57 InputStream tin = target.openStream(); 58 try { 59 JarInputStream jin = new JarInputStream(tin); 60 JarEntry je; 61 while ((je = jin.getNextJarEntry()) != null) { 62 String p = "/" + je.getName(); 63 if (p.startsWith(path) && p.indexOf('/', path.length() + 1) < 0) 64 65 ret.add(new URL("jar:" + target + "!" + p)); 66 } 67 } finally { 68 tin.close(); 69 } 70 } else if (s.startsWith("file:")) { 71 File f = new File(directory.getFile()); 72 File[] ffs = f.listFiles(); 73 if (ffs != null) 74 for (File ff : ffs) 75 ret.add(ff.toURI().toURL()); 76 } else 77 throw new IOException("Cannot list contents of " + directory); 78 79 return ret; 80 } 81 }