Copies class resource to path
@param source As in getResourceAsStream
@param target Target file
@param cls A class for finding class loader.
@throws IOException
@see java.lang.Class#getResourceAsStream(java.lang.String)
357994
public static final void copyResource(String source, Path target, Class<?> cls) throws IOException
    {
        try (InputStream is = cls.getResourceAsStream(source))
        {
            Files.copy(is, target, REPLACE_EXISTING);
        }
    }
1018102
private T addNestedJarFileResource(final File resource, final ArchivePath target, final ArchivePath base)
        throws IllegalArgumentException {
        final Iterable<ClassLoader> classLoaders = ((Configurable) this.getArchive()).getConfiguration()
            .getClassLoaders();

        for (final ClassLoader classLoader : classLoaders) {
            final InputStream in = classLoader.getResourceAsStream(resourceAdjustedPath(resource));
            if (in != null) {
                final Asset asset = new ByteArrayAsset(in);
                return add(asset, base, target.get());
            }
        }
        throw new IllegalArgumentException(resource.getPath() + " was not found in any available ClassLoaders");
    }
686525
public static Path serialize(Object source, FileSystem fs, Path targetPath) {
		Assert.notNull(targetPath, "'targetPath' must not be null");
		Assert.notNull(fs, "'fs' must not be null");
		Assert.notNull(source, "'source' must not be null");

		Path resultPath = targetPath.makeQualified(fs.getUri(), fs.getWorkingDirectory());
		OutputStream targetOutputStream = null;
		try {
			targetOutputStream = fs.create(targetPath);
			SerializationUtils.serialize(source, targetOutputStream);
		}
		catch (Exception e) {
			throw new IllegalStateException("Failed to serialize " + source + " to " + resultPath, e);
		}
		return resultPath;
	}
400105
public static URL getResourceURL(String resource) throws IOException {
    URL url = null;
    ClassLoader loader = ResourcesUtils.class.getClassLoader();
    if (loader != null) {
      url = loader.getResource(resource);
    }
    if (url == null) {
      url = ClassLoader.getSystemResource(resource);
    }
    if (url == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return url;
  }
1300692
private static InputStream findStreamInClasspathOrFileSystem(String fn) throws FileNotFoundException {
    // ms 10-04-2010:
    // - even though this may look like a regular file, it may be a path inside a jar in the CLASSPATH
    // - check for this first. This takes precedence over the file system.
    InputStream is = IOUtils.class.getClassLoader().getResourceAsStream(fn);
    // if not found in the CLASSPATH, load from the file system
    if (is == null) is = new FileInputStream(fn);
    return is;
  }
1062954
public static File copyFile(String source, String target) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(target).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            if (inputChannel != null) {
                inputChannel.close();
            }
            if (outputChannel != null) {
                outputChannel.close();
            }
        }
        return new File(target);
    }
907447
private static File _getClassSource (final Class <?> type)
  {
    if (type != null)
    {
      final String classResource = type.getName ().replace ('.', '/') + ".class";
      return _getResourceSource (classResource, type.getClassLoader ());
    }
    return null;
  }
993481
public static void copyDirs(Path source, Path target, CopyOption... options) throws IOException {
        if (Files.notExists(target)) {
            Files.createDirectory(target);
        }

        logger.debug("  --> Copying resources from [{}]", source);
        if (Files.notExists(source)) {
            throw new RuntimeException(source + " doesn't seem to exist.");
        }

        Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new InternalFileVisitor(source, target, options));

        logger.debug("  --> Resources ready in [{}]", target);
    }
715313
public static URL getSourceUrl(SourceUnit source) {
        URL url = null;
        final String filename = source.getName();
        if(filename==null) {
            return null;
        }

        Resource resource = new FileSystemResource(filename);
        if (resource.exists()) {
            try {
                url = resource.getURL();
            } catch (IOException e) {
                // ignore
            }
        }
        return url;
    }
35577
static void copyResources(URL resourceUrl, File targetPath) throws IOException, URISyntaxException {
        if (resourceUrl == null) {
            return;
        }

        URLConnection urlConnection = resourceUrl.openConnection();

        /**
         * Copy resources either from inside jar or from project folder.
         */
        if (urlConnection instanceof JarURLConnection) {
            copyJarResourceToPath((JarURLConnection) urlConnection, targetPath);
        } else if (VFS_PROTOCOL.equals(resourceUrl.getProtocol())) {
            VirtualFile virtualFileOrFolder = VFS.getChild(resourceUrl.toURI());
            copyFromWarToFolder(virtualFileOrFolder, targetPath);
        } else {
            File file = new File(resourceUrl.getPath());
            if (file.isDirectory()) {
                for (File resourceFile : FileUtils.listFiles(file, null, true)) {
                    int index = resourceFile.getPath().lastIndexOf(targetPath.getName()) + targetPath.getName().length();
                    File targetFile = new File(targetPath, resourceFile.getPath().substring(index));
                    if (!targetFile.exists() || targetFile.length() != resourceFile.length()) {
                        if (resourceFile.isFile()) {
                            FileUtils.copyFile(resourceFile, targetFile);
                        }
                    }
                }
            } else {
                if (!targetPath.exists() || targetPath.length() != file.length()) {
                    FileUtils.copyFile(file, targetPath);
                }
            }
        }
    }
924804
@Nullable
  public InputStream getResourceAsStream (final String name)
  {
    InputStream ret = null;
    if (m_aBestCandidate != null)
    {
      ret = m_aBestCandidate.getResourceAsStream (name);
      if (ret != null)
        return ret;
      m_aBestCandidate = null;
    }

    IClassLoadHelper aLoadHelper = null;
    final Iterator <IClassLoadHelper> iter = m_aLoadHelpers.iterator ();
    while (iter.hasNext ())
    {
      aLoadHelper = iter.next ();
      ret = aLoadHelper.getResourceAsStream (name);
      if (ret != null)
        break;
    }

    m_aBestCandidate = aLoadHelper;
    return ret;
  }
932043
public static void copy(final InputStream inputStream, final OutputStream outputStream)
      throws IOException {
    new ByteSource() {
      @Override
      public InputStream openStream() {
        return inputStream;
      }
    }.copyTo(new ByteSink() {
      @Override
      public OutputStream openStream() {
        return outputStream;
      }
    });
  }
467514
public void copyResource(String filename, String resource) throws IOException {
    write(filename, IOUtils.toByteArray(getClass().getResource(resource)));
  }
655457
protected Class<?> loadSourceClass( final String name ) {
        final String fileName = '/' + name.replace('.', '/') + ".java";
        final URL url = getResource( fileName );
        if ( url == null )
            return null;
        try (FileReader reader
                = new FileReader((InputStream) url.getContent())) {
            Interpreter.debug("Loading class from source file: " + fileName);
            declaringInterpreter.eval( reader );
        } catch ( IOException | EvalError e ) {
            if (Interpreter.DEBUG.get())
                e.printStackTrace();
        }
        try {
            return plainClassForName( name );
        } catch ( final ClassNotFoundException e ) {
            Interpreter.debug("Class not found in source file: " + name);
            return null;
        }
    }
204999
private void copyFile(String path, File dest) {
        String resourceName= "native/" + path;
        try
        {
            LOG.debug("Copy from resource <"+resourceName+"> to target <"+dest.getAbsolutePath()+">");
            if (copy(getClass().getResourceAsStream(resourceName), dest.getAbsolutePath()))
            {
                if (dest.exists())
                {
                    LOG.debug("Target <"+dest.getAbsolutePath()+"> exists");
                }
                else
                {
                    LOG.fatal("Target <"+dest.getAbsolutePath()+"> does not exist");
                }
            }
            else
            {
                LOG.fatal("Copy resource to target <"+dest.getAbsolutePath()+"> failed");
            }
        }
        catch (NullPointerException ex)
        {
            LOG.error("Could not find ffmpeg executable for "+resourceName+" is the correct platform jar included?");
            throw ex;
        }
    }
729948
public static Provider resourceProvider(ClassLoader classLoader, String pathToResource, Charset encoding) throws IOException {
        InputStream resourceAsStream = classLoader.getResourceAsStream(pathToResource);
        if (resourceAsStream == null) {
            throw new IOException("Cannot find " + pathToResource);
        }
        return provider(resourceAsStream, encoding);
    }
248024
public static InputStream openResourceStream(String resourceName) {
        InputStream stream = null;
        ClassLoader classLoader = Utils.class.getClassLoader();
        if (classLoader != null) {
            stream = classLoader.getResourceAsStream(resourceName);
        }

        if (stream == null) {
            stream = ClassLoader.getSystemResourceAsStream(resourceName);
        }
        return stream;
    }