private static void log(final Project project, final String message) { log(project, message, Project.MSG_VERBOSE); }
private static void copyUsingFileChannels(final File sourceFile, final File destFile, final Project project) throws IOException { if (FileUtils.getFileUtils().areSame(sourceFile, destFile)) { // copying the "same" file to itself will corrupt the file, so we skip it log(project, "Skipping (self) copy of " + sourceFile + " to " + destFile); return; } final File parent = destFile.getParentFile(); if (parent != null && !parent.isDirectory() && !(parent.mkdirs() || parent.isDirectory())) { throw new IOException("failed to create the parent directory" + " for " + destFile); } try (FileChannel srcChannel = FileChannel.open(sourceFile.toPath(), StandardOpenOption.READ); FileChannel destChannel = FileChannel.open(destFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { long position = 0; final long count = srcChannel.size(); while (position < count) { final long chunk = Math.min(MAX_IO_CHUNK_SIZE, count - position); position += destChannel.transferFrom(srcChannel, position, chunk); } } }
private static void copyUsingStreams(final Resource source, final Resource dest, final boolean append, final Project project) throws IOException { if (areSame(source, dest)) { // copying the "same" file to itself will corrupt the file, so we skip it log(project, "Skipping (self) copy of " + source + " to " + dest); return; } try (InputStream in = source.getInputStream(); OutputStream out = getOutputStream(dest, append, project)) { final byte[] buffer = new byte[FileUtils.BUF_SIZE]; int count = 0; do { out.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); } }
private static void copyWithFilterChainsOrTranscoding(final Resource source, final Resource dest, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException { if (areSame(source, dest)) { // copying the "same" file to itself will corrupt the file, so we skip it log(project, "Skipping (self) copy of " + source + " to " + dest); return; } try (Reader in = filterWith(project, inputEncoding, filterChains, source.getInputStream()); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( getOutputStream(dest, append, project), charsetFor(outputEncoding)))) { final char[] buffer = new char[FileUtils.BUF_SIZE]; while (true) { final int nRead = in.read(buffer, 0, buffer.length); if (nRead == -1) { break; } out.write(buffer, 0, nRead); } } }
log(project, "Skipping (self) copy of " + source + " to " + dest); return;