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.eclipse.thym.core.internal.util.FileUtils.java
private static void createFileFromZipFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException { if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new IOException("Can not create parent directory for " + file.toString()); }/* w w w . j av a 2 s . c o m*/ file.createNewFile(); FileOutputStream fout = null; FileChannel out = null; InputStream in = null; try { fout = new FileOutputStream(file); out = fout.getChannel(); in = zipFile.getInputStream(zipEntry); out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE); } finally { if (out != null) out.close(); if (in != null) in.close(); if (fout != null) fout.close(); } }
From source file:com.evolveum.midpoint.util.MiscUtil.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();//from www . j ava 2s . c om } 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:cd.education.data.collector.android.utilities.FileUtils.java
private static String actualCopy(File sourceFile, File destFile) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; FileChannel src = null;/*ww w .ja v a 2 s . c o m*/ FileChannel dst = null; try { fileInputStream = new FileInputStream(sourceFile); src = fileInputStream.getChannel(); fileOutputStream = new FileOutputStream(destFile); dst = fileOutputStream.getChannel(); dst.transferFrom(src, 0, src.size()); dst.force(true); return null; } catch (FileNotFoundException e) { Log.e(t, "FileNotFoundException while copying file", e); return e.getMessage(); } catch (IOException e) { Log.e(t, "IOException while copying file", e); return e.getMessage(); } catch (Exception e) { Log.e(t, "Exception while copying file", e); return e.getMessage(); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(src); IOUtils.closeQuietly(dst); } }
From source file:phex.utils.FileUtils.java
/** * From Apache Jakarta Commons IO./*from w w w . jav a2 s . c o m*/ * * Internal copy file method. * * @param srcFile the validated source file, not null * @param destFile the validated destination file, not null * @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 { IOUtil.closeQuietly(output); } } finally { IOUtil.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:com.aurel.track.lucene.util.FileUtil.java
/** * Copies a source file to a destination file * @param source/* w w w .java2s.c o m*/ * @param destination */ public static void copyFile(File source, File destination) { if (!source.exists()) { return; } if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } FileChannel srcChannel = null; FileChannel dstChannel = null; try { srcChannel = new FileInputStream(source).getChannel(); dstChannel = new FileOutputStream(destination).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (IOException ioe) { LOGGER.error(ExceptionUtils.getStackTrace(ioe)); } finally { if (srcChannel != null) try { srcChannel.close(); } catch (IOException ioe) { } ; if (dstChannel != null) try { dstChannel.close(); } catch (IOException ioe) { } ; } }
From source file:phex.util.FileUtils.java
/** * From Apache Jakarta Commons IO.// ww w . j a v a 2 s . c o m * <p> * Internal copy file method. * * @param srcFile the validated source file, not null * @param destFile the validated destination file, not null * @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 { IOUtil.closeQuietly(output); } } finally { IOUtil.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:ru.adios.budgeter.BundleProvider.java
private static void transferFile(String source, String destination, String errMsg) { final File sourceFile = new File(source); final File destFile = new File(destination); FileChannel fcSource = null;//from ww w . j a va2 s .c om FileChannel fcDest = null; try { fcSource = new FileInputStream(sourceFile).getChannel(); fcDest = new FileOutputStream(destFile).getChannel(); fcDest.transferFrom(fcSource, 0, fcSource.size()); fcSource.close(); fcDest.close(); } catch (IOException e) { logger.error(errMsg, e); throw new DataAccessResourceFailureException(errMsg, e); } finally { try { if (fcSource != null) { fcSource.close(); } if (fcDest != null) { fcDest.close(); } } catch (IOException ignore) { logger.warn("Exception while closing IO channels", ignore); } } }
From source file:com.liferay.util.FileUtil.java
public static void copyFile(File source, File destination, boolean hardLinks) { if (!source.exists()) { return;/* w w w . j a v a 2s.c o m*/ } if (hardLinks && !Config.getBooleanProperty("CONTENT_VERSION_HARD_LINK", true)) { hardLinks = false; } if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } if (hardLinks) { // I think we need to be sure to unlink first if (destination.exists()) { JNALibrary.unlink(destination.getAbsolutePath()); } try { JNALibrary.link(source.getAbsolutePath(), destination.getAbsolutePath()); // setting this means we will try again if we cannot hard link if (!destination.exists()) { hardLinks = false; } } catch (IOException e) { Logger.error(FileUtil.class, "Can't create hardLink. source: " + source.getAbsolutePath() + ", destination: " + destination.getAbsolutePath()); // setting this means we will try again if we cannot hard link hardLinks = false; } } if (!hardLinks) { try { FileChannel srcChannel = new FileInputStream(source).getChannel(); FileChannel dstChannel = new FileOutputStream(destination).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (IOException ioe) { Logger.error(FileUtil.class, ioe.getMessage(), ioe); } } }
From source file:org.ops4j.pax.runner.platform.internal.StreamUtils.java
/** * Copy a stream to a destination. It does not close the streams. * * @param in the stream to copy from * @param out the stream to copy to * @param progressBar download progress feedback. Can be null. * * @throws IOException re-thrown//from w ww . j a va2s .co m */ public static void streamCopy(final InputStream in, final FileChannel out, final ProgressBar progressBar) throws IOException { NullArgumentException.validateNotNull(in, "Input stream"); NullArgumentException.validateNotNull(out, "Output stream"); final long start = System.currentTimeMillis(); long bytes = 0; ProgressBar feedbackBar = progressBar; if (feedbackBar == null) { feedbackBar = new NullProgressBar(); } try { ReadableByteChannel inChannel = Channels.newChannel(in); bytes = out.transferFrom(inChannel, 0, Integer.MAX_VALUE); inChannel.close(); } finally { feedbackBar.increment(bytes, bytes / Math.max(System.currentTimeMillis() - start, 1)); feedbackBar.stop(); } }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private static void copyFile(File sourceFile, File destFile) throws IOException { FileChannel source = null;//from w w w .j a v a2 s. com 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(); } } }