/** * Converts the given {@link URL} to its corresponding {@link File}. * <p> * This method is similar to calling {@code new File(url.toURI())} except that * it also handles "jar:file:" URLs, returning the path to the JAR file. * </p> * * @param url The URL to convert. * @return A file path suitable for use with e.g. {@link FileInputStream} * @throws IllegalArgumentException if the URL does not correspond to a file. */ public static File urlToFile(final URL url) { return url == null ? null : urlToFile(url.toString()); }
/** * Converts the given {@link URL} to its corresponding {@link File}. * <p> * This method is similar to calling {@code new File(url.toURI())} except that * it also handles "jar:file:" URLs, returning the path to the JAR file. * </p> * * @param url The URL to convert. * @return A file path suitable for use with e.g. {@link FileInputStream} * @throws IllegalArgumentException if the URL does not correspond to a file. */ public static File urlToFile(final URL url) { return url == null ? null : urlToFile(url.toString()); }
final String protocol = directory.getProtocol(); if (protocol.equals("file")) { final File dir = urlToFile(directory); final File[] list = dir.listFiles(); if (list != null) {
/** * Gets the base file system directory containing the given class file. * * @param c The class from which the base directory should be derived. * @param baseSubdirectory A hint for what to expect for a directory structure * beneath the application base directory. * @see #getBaseDirectory(File, String) */ public static File getBaseDirectory(final Class<?> c, final String baseSubdirectory) { // see: http://stackoverflow.com/a/12733172/1207769 // step 1: convert Class to URL final URL location = Types.location(c); // step 2: convert URL to File File baseFile; try { baseFile = FileUtils.urlToFile(location); } catch (final IllegalArgumentException exc) { // URL can't be converted to a file baseFile = null; } // step 3: get the file's base directory return getBaseDirectory(baseFile, baseSubdirectory); }
final String protocol = directory.getProtocol(); if (protocol.equals("file")) { final File dir = urlToFile(directory); final File[] list = dir.listFiles(); if (list != null) {
return; File file = FileUtils.urlToFile(url); if (autoDetectEclipse && !file.isAbsolute()) { debug("Not an absolute file URL: " + url);
/** * Gets the base file system directory containing the given class file. * * @param c The class from which the base directory should be derived. * @param baseSubdirectory A hint for what to expect for a directory structure * beneath the application base directory. * @see #getBaseDirectory(File, String) */ public static File getBaseDirectory(final Class<?> c, final String baseSubdirectory) { // see: http://stackoverflow.com/a/12733172/1207769 // step 1: convert Class to URL final URL location = Types.location(c); // step 2: convert URL to File File baseFile; try { baseFile = FileUtils.urlToFile(location); } catch (final IllegalArgumentException exc) { // URL can't be converted to a file baseFile = null; } // step 3: get the file's base directory return getBaseDirectory(baseFile, baseSubdirectory); }
return; File file = FileUtils.urlToFile(url); if (autoDetectEclipse && !file.isAbsolute()) { debug("Not an absolute file URL: " + url);
final File file = FileUtils.urlToFile(location); final File baseDir = AppUtils.getBaseDirectory(file, null); final File pomFile = new File(baseDir, "pom.xml");
final File file = FileUtils.urlToFile(location); final File baseDir = AppUtils.getBaseDirectory(file, null); final File pomFile = new File(baseDir, "pom.xml");
final File file = FileUtils.urlToFile(location); final File baseDir = AppUtils.getBaseDirectory(file, null); final File pomFile = new File(baseDir, "pom.xml");
final File file = FileUtils.urlToFile(location); final File baseDir = AppUtils.getBaseDirectory(file, null); final File pomFile = new File(baseDir, "pom.xml");
@Test public void testClassInJar() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final File jar = new File(tmpDir, "test.jar"); final JarOutputStream out = new JarOutputStream(new FileOutputStream(jar)); final String path = getClass().getName().replace('.', '/') + ".class"; out.putNextEntry(new ZipEntry(path)); copy(getClass().getResource("/" + path).openStream(), out, true); final ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }, null); final URL location = ClassUtils.getLocation(getClass().getName(), classLoader); assertEquals(jar, FileUtils.urlToFile(location)); jar.deleteOnExit(); }
if (column == 5) { final URL location = ClassUtils.getLocation(info.getDelegateClassName()); final File file = FileUtils.urlToFile(location); final String path = file == null ? null : file.getAbsolutePath(); if (path != null && path.startsWith(baseDir)) {
/** Tests {@link Types#location} with a class in a JAR file. */ @Test public void testLocationClassInJar() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final File jar = new File(tmpDir, "test.jar"); final JarOutputStream out = new JarOutputStream(new FileOutputStream(jar)); final String path = getClass().getName().replace('.', '/') + ".class"; out.putNextEntry(new ZipEntry(path)); copy(getClass().getResource("/" + path).openStream(), out, true); final ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(jar, FileUtils.urlToFile(location)); jar.deleteOnExit(); }
@Test public void testUnpackedClass() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final String path = getClass().getName().replace('.', '/') + ".class"; final File classFile = new File(tmpDir, path); assertTrue(classFile.getParentFile().exists() || classFile.getParentFile().mkdirs()); copy(getClass().getResource("/" + path).openStream(), new FileOutputStream(classFile), true); final ClassLoader classLoader = new URLClassLoader(new URL[] { tmpDir.toURI().toURL() }, null); final URL location = ClassUtils.getLocation(getClass().getName(), classLoader); assertEquals(tmpDir, FileUtils.urlToFile(location)); FileUtils.deleteRecursively(tmpDir); }
/** Tests {@link Types#location} with a class on the file system. */ @Test public void testLocationUnpackedClass() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final String path = getClass().getName().replace('.', '/') + ".class"; final File classFile = new File(tmpDir, path); assertTrue(classFile.getParentFile().exists() || classFile.getParentFile().mkdirs()); copy(getClass().getResource("/" + path).openStream(), new FileOutputStream(classFile), true); final ClassLoader classLoader = new URLClassLoader(new URL[] { tmpDir.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(tmpDir, FileUtils.urlToFile(location)); FileUtils.deleteRecursively(tmpDir); }
final File jqpublicFile = FileUtils.urlToFile("file:" + jqpublic); assertEqualsPath(jqpublic, jqpublicFile.getPath() + "/"); final File fileFile = FileUtils.urlToFile(fileURL); assertEqualsPath(filePath, fileFile.getPath()); new File(jqpublic.replace("jqpublic", "Spaceman Spiff") + "stun/Blaster.class"); final URL spaceURL = spaceFileOriginal.toURI().toURL(); final File spaceFileResult = FileUtils.urlToFile(spaceURL); assertEqualsPath(spaceFileOriginal.getPath(), spaceFileResult.getPath()); alphaHi + "/" + numbers + "/" + special + "/foo/Bar.class"); final URL specialURL = specialFileOriginal.toURI().toURL(); final File specialFileResult = FileUtils.urlToFile(specialURL); assertEqualsPath(specialFileOriginal.getPath(), specialFileResult.getPath()); final File jarFile = FileUtils.urlToFile(jarURL); assertEqualsPath(jarPath, jarFile.getPath()); "bundleresource://346.fwk2106232034:4/foo/Bar.class"; try { final File bundleFile = FileUtils.urlToFile(bundleURL); fail("Expected exception not thrown; result=" + bundleFile);