List of usage examples for java.nio.channels FileChannel transferFrom
public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;
From source file:org.mule.util.FileUtils.java
/** * Try to move a file by renaming with backup attempt by copying/deleting via NIO. * Creates intermidiate directories as required. */// ww w . j a v a2 s .c o m public static boolean moveFileWithCopyFallback(File sourceFile, File destinationFile) { // try fast file-system-level move/rename first boolean success = sourceFile.renameTo(destinationFile); if (!success) { // try again using NIO copy FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(sourceFile); if (!destinationFile.exists()) { FileUtils.createFile(destinationFile.getPath()); } fos = new FileOutputStream(destinationFile); FileChannel srcChannel = fis.getChannel(); FileChannel dstChannel = fos.getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); success = sourceFile.delete(); } catch (IOException ioex) { // grr! success = false; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } } return success; }
From source file:com.github.fritaly.dualcommander.Utils.java
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); }/* w w w . j a v a2 s . com*/ FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); input = fis.getChannel(); output = fos.getChannel(); long size = input.size(); long pos = 0; long count = 0; while (pos < size) { count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos; pos += output.transferFrom(input, pos, count); } } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(input); IOUtils.closeQuietly(fis); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } if (preserveFileDate) { destFile.setLastModified(srcFile.lastModified()); } }
From source file:org.mule.util.FileUtils.java
/** * Internal copy file method./*w w w. j a v a 2s.co m*/ * * @param srcFile the validated source file, must not be <code>null</code> * @param destFile the validated destination file, must not be <code>null</code> * @param preserveFileDate whether to preserve the file date * @throws IOException if an error occurs */ private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); } FileChannel input = new FileInputStream(srcFile).getChannel(); try { FileChannel output = new FileOutputStream(destFile).getChannel(); try { output.transferFrom(input, 0, input.size()); } finally { closeQuietly(output); } } finally { closeQuietly(input); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } if (preserveFileDate) { destFile.setLastModified(srcFile.lastModified()); } }
From source file:padl.creator.cppfile.eclipse.plugin.internal.Utils.java
static void copyFile(final File sourceFile, final File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();/* ww w . j av a2 s . c o m*/ } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:com.remobile.file.LocalFilesystem.java
private static void copyResource(CordovaResourceApi.OpenForReadResult input, OutputStream outputStream) throws IOException { try {/*from w ww . j ava 2s . co m*/ InputStream inputStream = input.inputStream; if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) { FileChannel inChannel = ((FileInputStream) input.inputStream).getChannel(); FileChannel outChannel = ((FileOutputStream) outputStream).getChannel(); long offset = 0; long length = input.length; if (input.assetFd != null) { offset = input.assetFd.getStartOffset(); } // transferFrom()'s 2nd arg is a relative position. Need to set the absolute // position first. inChannel.position(offset); outChannel.transferFrom(inChannel, 0, length); } else { final int BUFFER_SIZE = 8192; byte[] buffer = new byte[BUFFER_SIZE]; for (;;) { int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE); if (bytesRead <= 0) { break; } outputStream.write(buffer, 0, bytesRead); } } } finally { input.inputStream.close(); if (outputStream != null) { outputStream.close(); } } }
From source file:org.datavaultplatform.common.io.FileCopy.java
/** * Internal copy file method./*from w ww. j a v a2 s. c o m*/ * * @param progress the progress tracking object * @param srcFile the validated source file, must not be {@code null} * @param destFile the validated destination file, must not be {@code null} * @param preserveFileDate whether to preserve the file date * @throws IOException if an error occurs */ private static void doCopyFile(Progress progress, File srcFile, File destFile, boolean preserveFileDate) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); } FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); input = fis.getChannel(); output = fos.getChannel(); long size = input.size(); long pos = 0; long count = 0; while (pos < size) { count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos; long copied = output.transferFrom(input, pos, count); pos += copied; progress.byteCount += copied; progress.timestamp = System.currentTimeMillis(); } } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(input); IOUtils.closeQuietly(fis); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } if (preserveFileDate) { destFile.setLastModified(srcFile.lastModified()); } progress.fileCount += 1; }
From source file:dk.clanie.io.IOUtil.java
/** * Creates a copy of a file./*w w w.j a v a2s . c o m*/ * * If the destination is a directory the file is copied to that directory * with the same name as the source file. * * @param from * - source File * @param to * - destination File * @param overwrite * - allow overwriting existing file * * @throws RuntimeIOException */ @SuppressWarnings("resource") public static void copyFile(File from, File to, boolean overwrite) throws RuntimeIOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { if (!from.exists()) throw new RuntimeIOException.FileNotFound(from); if (to.isDirectory()) { to = new File(to.getPath() + File.separator + from.getName()); } if (to.exists()) { if (!overwrite) throw new RuntimeIOException.FileAlreadyExists(to); } else if (!to.createNewFile()) throw new RuntimeIOException.FailedToCreate(to); inputChannel = new FileInputStream(from).getChannel(); outputChannel = new FileOutputStream(to).getChannel(); long size = inputChannel.size(); long copied = 0L; while (copied < size) copied += outputChannel.transferFrom(inputChannel, copied, size - copied); } catch (java.io.IOException e) { throw new RuntimeIOException(e.getMessage(), e); } finally { closeChannels(inputChannel, outputChannel); } }
From source file:it.geosolutions.tools.io.file.IOUtils.java
/** * Optimize version of copy method for file channels. * /*from w w w. j a va2s . c o m*/ * @param bufferSize * size of the temp buffer to use for this copy. * @param source * the source {@link ReadableByteChannel}. * @param destination * the destination {@link WritableByteChannel};. * @throws IOException * in case something bad happens. */ public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination) throws IOException { Objects.notNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); FileLock lock = null; try { lock = destination.lock(); final long sourceSize = source.size(); long pos = 0; while (pos < sourceSize) { // read and flip final long remaining = (sourceSize - pos); final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining; destination.transferFrom(source, pos, mappedZoneSize); // update zone pos += mappedZoneSize; } } finally { if (lock != null) { try { lock.release(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } } }
From source file:com.parse.ParseFileUtils.java
/** * Internal copy file method./* ww w. j a va2 s . co m*/ * This caches the original file length, and throws an IOException * if the output file length is different from the current input file length. * So it may fail if the file changes size. * It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way * through copying the data and the new file size is less than the current position. * * @param srcFile the validated source file, must not be {@code null} * @param destFile the validated destination file, must not be {@code null} * @param preserveFileDate whether to preserve the file date * @throws IOException if an error occurs * @throws IOException if the output file length is not the same as the input file length after the copy completes * @throws IllegalArgumentException "Negative size" if the file is truncated so that the size is less than the position */ private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); } FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); input = fis.getChannel(); output = fos.getChannel(); final long size = input.size(); // TODO See IO-386 long pos = 0; long count = 0; while (pos < size) { final long remain = size - pos; count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain; final long bytesCopied = output.transferFrom(input, pos, count); if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size break; // ensure we don't loop forever } pos += bytesCopied; } } finally { ParseIOUtils.closeQuietly(output); ParseIOUtils.closeQuietly(fos); ParseIOUtils.closeQuietly(input); ParseIOUtils.closeQuietly(fis); } final long srcLen = srcFile.length(); // TODO See IO-386 final long dstLen = destFile.length(); // TODO See IO-386 if (srcLen != dstLen) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen); } if (preserveFileDate) { destFile.setLastModified(srcFile.lastModified()); } }
From source file:org.lnicholls.galleon.util.Tools.java
public static void copy(File src, File dst) { try {/* www. j a va 2s . c o m*/ FileChannel srcChannel = new FileInputStream(src).getChannel(); FileChannel dstChannel = new FileOutputStream(dst).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); srcChannel = null; dstChannel.close(); dstChannel = null; } catch (IOException ex) { Tools.logException(Tools.class, ex, dst.getAbsolutePath()); } }