Internal copy method accepting file, inputstream or string
@param executionContext context
@param file file
@param input stream
@param content string
@param node node
@param destination path
@return file path
@throws FileCopierException on error
187373
String copyFile(
            final ExecutionContext executionContext,
            final File file,
            final InputStream input,
            final String content,
            final INodeEntry node,
            String destination
    ) throws FileCopierException
    {
        final String pluginname = getProvider().getName();
        final Map<String, Map<String, String>> localDataContext = createScriptDataContext(
            executionContext.getFramework(),
            executionContext.getFrameworkProject(),
            executionContext.getDataContext());
        //add node context data
        localDataContext.put("node", DataContextUtils.nodeData(node));

        Description pluginDesc = getDescription();
        //load config.* property values in from project or framework scope
        final Map<String, Map<String, String>> finalDataContext;
        try {
            finalDataContext = loadConfigData(executionContext, loadInstanceDataFromNodeAttributes(node, pluginDesc), localDataContext, pluginDesc,
                                              ServiceNameConstants.FileCopier
            );
        } catch (ConfigurationException e) {
            throw new FileCopierException("[" + pluginname + "]: "+e.getMessage(),
                    StepFailureReason.ConfigurationFailure, e);
        }

        final File srcFile =
                null != file ?
                        file :
                        BaseFileCopier.writeTempFile(executionContext, null, input, content);

        String destFilePath = destination;
        if (null == destFilePath) {

            String identity = null!=executionContext.getDataContext() && null!=executionContext.getDataContext().get("job")?
                              executionContext.getDataContext().get("job").get("execid"):null;
            destFilePath = BaseFileCopier.generateRemoteFilepathForNode(
                    node,
                    executionContext.getFramework().getFrameworkProjectMgr().getFrameworkProject(executionContext.getFrameworkProject()),
                    executionContext.getFramework(),
                    (null != file ? file.getName() : "dispatch-script"),
                    null,
                    identity
            );
        }
        //put file in a directory
        if (null != destFilePath && destFilePath.endsWith("/")) {
            destFilePath += srcFile.getName();
        }
        //add some more data context values to allow templatized script-copy attribute
        final HashMap<String, String> scptexec = new HashMap<>();
        //set up the data context to include the local temp file
        scptexec.put("file", srcFile.getAbsolutePath());
        scptexec.put("destination", null != destFilePath ? destFilePath : "");
        finalDataContext.put("file-copy", scptexec);
        Map<String, Map<String, String>> fileCopyContext =
                DataContextUtils.addContext(
                        "file-copy",
                        scptexec,
                        null
                );

        final ExecArgList execArgList = createScriptArgsList(fileCopyContext);
        final String localNodeOsFamily=getFramework().createFrameworkNode().getOsFamily();

        executionContext.getExecutionListener().log(3,
                                                    "[" +
                                                    getProvider().getName() +
                                                    "] executing: " +
                                                    execArgList.asFlatStringList()
        );

        final ByteArrayOutputStream captureSysOut = new ByteArrayOutputStream();
        try {
            final int result = getScriptExecHelper().runLocalCommand(
                    localNodeOsFamily,
                    execArgList,
                    finalDataContext,
                    null,
                    captureSysOut,
                    System.err
            );
            executionContext.getExecutionListener().log(3, "[" + pluginname + "]: result code: " + result);
            if(result!=0){
                throw new FileCopierException("[" + pluginname + "]: external script failed with exit code: " + result,
                                              NodeStepFailureReason.NonZeroResultCode);
            }
        } catch (IOException e) {
            throw new FileCopierException("[" + pluginname + "] " + e.getMessage(), StepFailureReason.IOFailure);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new FileCopierException("[" + pluginname + "] " + e.getMessage(), StepFailureReason.Interrupted);
        }finally {
            if(null == file) {
                if (!ScriptfileUtils.releaseTempFile(srcFile)) {
                    executionContext.getExecutionListener().log(
                            Constants.WARN_LEVEL,
                            "Unable to remove local temp file: " + srcFile.getAbsolutePath()
                    );
                }
            }
        }


        //load string of output from outputstream
        final String output = captureSysOut.toString();
        if (null == output || output.length() < 1) {
            if (null != destFilePath) {
                return destFilePath;
            }
            throw new FileCopierException("[" + pluginname + "]: No output from external script",
                                          ScriptPluginFailureReason.ScriptPluginFileCopierOutputMissing
            );
        }
        //TODO: require any specific format for the data?
        //look for first line of output
        final String[] split1 = output.split("(\\r?\\n)");
        if (split1.length < 1) {
            if (null != destFilePath) {
                return destFilePath;
            }
            throw new FileCopierException("[" + pluginname + "]: No output from external script",
                                          ScriptPluginFailureReason.ScriptPluginFileCopierOutputMissing);
        }
        final String remotefilepath = split1[0];

        executionContext.getExecutionListener().log(3, "[" + pluginname + "]: result filepath: " + remotefilepath);

        return remotefilepath;
    }
172883
private void copy(final InputStream inputStream, final FileOutputStream outputStream) throws IOException {
        final byte[] buffer = new byte[1024*4];
        int n;
        try {
            while (-1 != (n = inputStream.read(buffer))) {
                outputStream.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
            outputStream.close();
        }
    }
765933
public static void updateFileFromInputStream(final InputStream input, final String destinationFilePath) throws
        UpdateException {
        update(new FileStreamUpdater(input), new File(destinationFilePath));
    }
1099366
String copyFile(
            final ExecutionContext executionContext,
            final File file,
            final InputStream input,
            final String content,
            final INodeEntry node,
            String remotePath
    ) throws
      FileCopierException
    {
        File workingdir = null;
        String scriptargs;
        String dirstring;

        //get project or framework property for script-exec args
        final Framework framework = executionContext.getFramework();
        //look for specific property
        scriptargs = framework.getProjectProperty(executionContext.getFrameworkProject(),
                                                  SCRIPT_COPY_DEFAULT_COMMAND_PROPERTY);


        if (null != node.getAttributes().get(SCRIPT_ATTRIBUTE)) {
            scriptargs = node.getAttributes().get(SCRIPT_ATTRIBUTE);
        }
        if (null == scriptargs) {
            throw new FileCopierException(
                "[script-copy file copier] no attribute " + SCRIPT_ATTRIBUTE + " was found on node: "
                + node
                    .getNodename() + ", and no " + SCRIPT_COPY_DEFAULT_COMMAND_PROPERTY
                + " property was configured for the project or framework.",
                StepFailureReason.ConfigurationFailure
            );
        }

        dirstring = framework.getProjectProperty(executionContext.getFrameworkProject(),
                                                 SCRIPT_COPY_DEFAULT_DIR_PROPERTY);
        if (null != node.getAttributes().get(DIR_ATTRIBUTE)) {
            dirstring = node.getAttributes().get(DIR_ATTRIBUTE);
        }
        if (null != dirstring && !"".equals(dirstring)) {
            workingdir = new File(dirstring);
        }

        final File srcFile =
                null != file ?
                        file :
                        BaseFileCopier.writeTempFile(executionContext, file, input, content);


        //create context data with node attributes
        Map<String, Map<String, String>> newDataContext;
        final Map<String, Map<String, String>> nodeContext =
                DataContextUtils.addContext("node", DataContextUtils.nodeData(node), executionContext.getDataContext());

        final HashMap<String, String> scptexec = new HashMap<String, String>(){{
            //set filename of source file
            put("filename", srcFile.getName());
            put("destfilename", srcFile.getName());
            put("file", srcFile.getAbsolutePath());
            //add file, dir, destination to the file-copy data
        }};
	
        if(null != remotePath && !remotePath.endsWith("/")) {
            scptexec.put("destfilename", new File(remotePath).getName());
        }

        if (null != workingdir) {
            //set up the data context to include the working dir
            scptexec.put("dir", workingdir.getAbsolutePath());
        }

        newDataContext = DataContextUtils.addContext("file-copy", scptexec, nodeContext);

        //expand remote filepath if we are copying a script
        String copiedFilepath;
        if (null == remotePath) {
            copiedFilepath = framework.getProjectProperty(executionContext.getFrameworkProject(),
                    SCRIPT_COPY_DEFAULT_REMOTE_FILEPATH_PROPERTY);
            if (null != node.getAttributes().get(REMOTE_FILEPATH_ATTRIBUTE)) {
                copiedFilepath = node.getAttributes().get(REMOTE_FILEPATH_ATTRIBUTE);
            }
            if (null != copiedFilepath) {
                if (!(copiedFilepath.contains("${file-copy.filename}")
                        || copiedFilepath.contains("${file-copy.file}"))
                        && !copiedFilepath.endsWith("/")) {
                    copiedFilepath += "/";
                }
                copiedFilepath = DataContextUtils.replaceDataReferencesInString(copiedFilepath, newDataContext);
            }
        } else {
            //we are copying to a specific destination
            copiedFilepath = remotePath;
        }

        //put file in a directory
        if (null != copiedFilepath && copiedFilepath.endsWith("/")) {
            copiedFilepath += srcFile.getName();
        }

        scptexec.put("destination", null != copiedFilepath ? copiedFilepath : "");

        newDataContext = DataContextUtils.addContext("file-copy", scptexec, nodeContext);

        final Process exec;

        String remoteShell = framework.getProjectProperty(executionContext.getFrameworkProject(),
                                                          SCRIPT_COPY_DEFAULT_REMOTE_SHELL);
        if (null != node.getAttributes().get(SHELL_ATTRIBUTE)) {
            remoteShell = node.getAttributes().get(SHELL_ATTRIBUTE);
        }

        try {
            if (null != remoteShell) {
                exec = ScriptUtil.execShellProcess(executionContext.getExecutionListener(), workingdir, scriptargs,
                                                   nodeContext, newDataContext, remoteShell, "script-copy");
            } else {
                exec = ScriptUtil.execProcess(executionContext.getExecutionListener(), workingdir, scriptargs,
                                              nodeContext,
                                              newDataContext, "script-copy");
            }
        } catch (IOException e) {
            throw new FileCopierException(e.getMessage(), StepFailureReason.IOFailure, e);
        }finally {
            if(null == file) {
                if (!ScriptfileUtils.releaseTempFile(srcFile)) {
                    executionContext.getExecutionListener().log(
                            Constants.WARN_LEVEL,
                            "Unable to remove local temp file: " + srcFile.getAbsolutePath()
                    );
                }
            }
        }

        final Thread errthread;
        Thread outthread = null;
        final int result;
        final boolean success;


        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            exec.getOutputStream().close();
            errthread = Streams.copyStreamThread(exec.getErrorStream(), System.err);
            if (null == copiedFilepath) {
                outthread = Streams.copyStreamThread(exec.getInputStream(), byteArrayOutputStream);
            }
            errthread.start();
            if (null != outthread) {
                outthread.start();
            }
            result = exec.waitFor();
            System.err.flush();
            byteArrayOutputStream.flush();
            errthread.join();
            if (null != outthread) {
                outthread.join();
            }
            exec.getErrorStream().close();
            exec.getInputStream().close();
            success = 0 == result;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new FileCopierException(e.getMessage(), StepFailureReason.Interrupted, e);
        } catch (IOException e) {
            throw new FileCopierException(e.getMessage(), StepFailureReason.IOFailure, e);
        }
        if (!success) {
            if (scriptargs.contains("${file-copy.destination}") && null == copiedFilepath) {
                executionContext.getExecutionListener().log(0,
                        "[script-copy]: ${file-copy.destination} is referenced in the file-copy script, but its value " +
                                "could not be determined. " +
                                "The node " + node.getNodename() + " may need a " + REMOTE_FILEPATH_ATTRIBUTE
                                + " attribute.");
            }
            throw new FileCopierException("[script-copy]: external script failed with exit code: " + result,
                                          NodeStepFailureReason.NonZeroResultCode);
        }

        if (null != copiedFilepath) {
            return copiedFilepath;
        }
        //load string of output from outputstream
        final String output = byteArrayOutputStream.toString();
        if (null == output || output.length() < 1) {
            throw new FileCopierException("[script-copy]: No output from external script",
                                          Reason.ScriptFileCopierPluginExpectedOutputMissing);
        }
        //TODO: require any specific format for the data?
        //look for first line of output
        final String[] split1 = output.split("(\\r?\\n)");
        if (split1.length < 1) {
            throw new FileCopierException("[script-copy]: No output from external script",
                                          Reason.ScriptFileCopierPluginExpectedOutputMissing);
        }
        final String remotefilepath = split1[0];

        executionContext.getExecutionListener().log(3, "[script-copy]: result filepath: " + remotefilepath);

        return remotefilepath;
    }
736219
public static void copy(final File sourceFile, File targetFile, final boolean overwrite) throws IOException {
        if (sourceFile == null) {
            throw new IOException("Source must not be null");
        }

        if (targetFile == null) {
            throw new IOException("Target must not be null");
        }

        // checks
        if ((false == sourceFile.isFile()) || (false == sourceFile.exists())) {
            throw new IOException("Source file '" + sourceFile.getAbsolutePath() + "' not found!");
        }

        if (true == targetFile.exists()) {
            if (true == targetFile.isDirectory()) {

                // Directory? -> use source file name
                targetFile = new File(targetFile, sourceFile.getName());
            } else if (true == targetFile.isFile()) {
                if (false == overwrite) {
                    throw new IOException("Target file '" + targetFile.getAbsolutePath() + "' already exists!");
                }
            } else {
                throw new IOException("Invalid target object '" + targetFile.getAbsolutePath() + "'!");
            }
        } else {
            // create parent dir
            FileUtils.forceMkdir(targetFile.getParentFile());
        }

        FileInputStream input = null;
        FileOutputStream output = null;

        try {
            input = new FileInputStream(sourceFile);
            output = new FileOutputStream(targetFile);
            copy(input, output);
            output.flush();
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    }
859105
public static int copy(File in, File out) throws IOException {
        assert in != null : "No input File specified";
        assert out != null : "No output File specified";
        return copy(new BufferedInputStream(new FileInputStream(in)),
                new BufferedOutputStream(new FileOutputStream(out)));
    }
1014085
function copyFile(source, target, copyCallback) {
    const readStream = fs.createReadStream(source);
    const writeStream = fs.createWriteStream(target);
    let done = false;

    readStream.on('error', copyDone);
    writeStream.on('error', copyDone);

    writeStream.on('close', function onWriteCb() {
        copyDone(null);
    });

    readStream.pipe(writeStream);

    /**
     * Finish copying. Reports error when needed
     * @param [error] optional error
     */
    function copyDone(error) {
        if (!done) {
            done = true;
            copyCallback(error);
        }
    }
}
106359
public void stringToFile(String content, String filename) throws IOException {
        stringToOutputStream(content, new FileOutputStream(filename));
    }
650818
public static void copyStream(InputStream copyFrom, File copyTo)
			throws IOException {
		copyStream(copyFrom, copyTo, null);
	}
835750
@Override
  public void commit()
      throws IOException {

    if (!this.actualProcessedCopyableFile.isPresent()) {
      return;
    }

    CopyableFile copyableFile = this.actualProcessedCopyableFile.get();
    Path stagingFilePath = getStagingFilePath(copyableFile);
    Path outputFilePath = getSplitOutputFilePath(copyableFile, this.outputDir,
        copyableFile.getDatasetAndPartition(this.copyableDatasetMetadata), this.state);

    log.info(String.format("Committing data from %s to %s", stagingFilePath, outputFilePath));
    try {
      setFilePermissions(copyableFile);

      Iterator<OwnerAndPermission> ancestorOwnerAndPermissionIt =
          copyableFile.getAncestorsOwnerAndPermission() == null ? Iterators.<OwnerAndPermission>emptyIterator()
              : copyableFile.getAncestorsOwnerAndPermission().iterator();

      ensureDirectoryExists(this.fs, outputFilePath.getParent(), ancestorOwnerAndPermissionIt);

      this.fileContext.rename(stagingFilePath, outputFilePath, renameOptions);
    } catch (IOException ioe) {
      log.error("Could not commit file %s.", outputFilePath);
      // persist file
      this.recoveryHelper.persistFile(this.state, copyableFile, stagingFilePath);
      throw ioe;
    } finally {
      try {
        this.fs.delete(this.stagingDir, true);
      } catch (IOException ioe) {
        log.warn("Failed to delete staging path at " + this.stagingDir);
      }
    }
  }
1059041
public static void copyFile(File srcFile, File destFile) {
		OutputStream out = null;
		InputStream in = null;
		try {
			if (!destFile.getParentFile().exists())
				destFile.getParentFile().mkdirs();

			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);

			// Transfer bytes from in to out
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
1253909
public static void copy(FileObject source, FileObject target) throws IOException {
        InputStream in = source.openInputStream();
        OutputStream out = target.openOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int len = in.read(buffer);
            while (len != -1) {
                out.write(buffer, 0, len);
                len = in.read(buffer);
            }
        } finally {
            in.close();
            out.close();
        }
    }
1186341
public static void copyFile(File src, File dest) throws IOException {

        if (src == null)
            throw new NullPointerException("Source must not be null");
        if (dest == null)
            throw new NullPointerException("Destination must not be null");
        if (!src.exists())
            throw new FileNotFoundException("Source '" + src + "' does not exist");
        if (!src.isFile())
            throw new IOException("Source '" + src + "' is not a file");
        if (dest.exists())
            throw new IOException("Destination '" + dest + "' is already exists");

        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(src).getChannel();
            out = new FileOutputStream(dest).getChannel();

            in.transferTo(0, in.size(), out);
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                // Ignore.
            }
        }

        if (src.length() != dest.length()) {
            throw new IOException("Failed to copy full contents from '" +
                src + "' to '" + dest + "'");
        }

        dest.setLastModified(src.lastModified());
    }
416260
public static void copy(byte[] in, File out) throws IOException {
		Assert.notNull(in, "No input byte array specified");
		Assert.notNull(out, "No output File specified");
		ByteArrayInputStream inStream = new ByteArrayInputStream(in);
		OutputStream outStream = new BufferedOutputStream(new FileOutputStream(out));
		copy(inStream, outStream);
	}
200499
public static File copyFile(File src, File dest, StandardCopyOption... options) throws IORuntimeException {
		// check
		Assert.notNull(src, "Source File is null !");
		if (false == src.exists()) {
			throw new IORuntimeException("File not exist: " + src);
		}
		Assert.notNull(dest, "Destination File or directiory is null !");
		if (equals(src, dest)) {
			throw new IORuntimeException("Files '{}' and '{}' are equal", src, dest);
		}
		return copyFile(src.toPath(), dest.toPath(), options).toFile();
	}
533745
private void copyStream(InputStream inputStream) throws IOException {
		OutputStream outputStream = null;
		try {
			outputStream = getContext().getOutputStream();
			byte[] buffer = new byte[65535];
			int bufferLen;
			int totalLen = 0;
			while ((bufferLen = inputStream.read(buffer)) > 0) {
				outputStream.write(buffer, 0, bufferLen);
				totalLen += bufferLen;
			}
			getContext().getResponse().setContentLength(totalLen);
		} finally {
			if (outputStream != null) {
				outputStream.flush();
			}
		}
	}
534378
public static void copy(byte[] in, File out) throws IOException {
        assert in != null : "No input byte array specified";
        assert out != null : "No output File specified";
        ByteArrayInputStream inStream = new ByteArrayInputStream(in);
        OutputStream outStream = new BufferedOutputStream(Files.newOutputStream(out.toPath()));
        copy(inStream, outStream);
    }