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:com.cisco.dvbu.ps.common.util.CommonUtils.java
/** * The copyFile method is used to copy files from a source to a destination folder. * /* w w w. ja v a 2 s . co m*/ * @param fromFilePath * @param toFilePath * @throws ValidationException */ public static void copyFile(String fromFilePath, String toFilePath, boolean forceCopy) throws ValidationException { FileChannel srcChannel = null; FileChannel dstChannel = null; boolean fileExists = fileExists(toFilePath); if (forceCopy && fileExists) { removeFile(toFilePath); fileExists = fileExists(toFilePath); } if ((!fileExists) || (forceCopy && fileExists)) { try { // Create channel on the source srcChannel = new FileInputStream(fromFilePath).getChannel(); // Create channel on the destination dstChannel = new FileOutputStream(toFilePath).getChannel(); // Force the copy - added to overcome copy error dstChannel.force(true); // Copy file contents from source to destination dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); } catch (IOException e) { String message = "Could not copy file " + fromFilePath + ".An error was encountered: " + e.toString(); throw new ValidationException(message, e); } finally { try { // Close the channels if (srcChannel != null) srcChannel.close(); if (dstChannel != null) dstChannel.close(); srcChannel = null; dstChannel = null; } catch (IOException e) { String message = "Could not copy file " + fromFilePath + ". Error encountered while closing source and destination channels: " + e.toString(); throw new ValidationException(message, e); } } } }
From source file:com.example.util.FileUtils.java
/** * Internal copy file method./*from w w w . j ava2 s. co m*/ * * @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(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; 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.sakaiproject.search.index.impl.JDBCClusterIndexStore.java
/** * @param packetStream//from w w w . j a va 2 s.c o m * @param sharedStream * @throws IOException */ private void doBlockedStream(FileChannel from, FileChannel to) throws IOException { to.position(0); long size = from.size(); for (long pos = 0; pos < size;) { long count = size - pos; if (count > MAX_BLOCK_SIZE) { count = MAX_BLOCK_SIZE; } to.position(pos); long cpos = to.position(); log.debug("NIOTransfering |" + count + "| bytes from |" + pos + "| to |" + cpos + "|"); long t = to.transferFrom(from, pos, count); pos = pos + t; } log.debug(" Final Size Source " + from.size()); log.debug(" Final Size Destination " + to.size()); }
From source file:com.matteoveroni.model.copy.FileUtils.java
/** * Internal copy file method.//from w ww.ja v a2 s .c o m * * @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(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 { bytesTransferedTotal = 0; 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); //Split into into deceleration and assignment to count bytes transfered long bytesTransfered = output.transferFrom(input, pos, count); //complete original method pos += bytesTransfered; //update total bytes copied, so it can be used to calculate progress bytesTransferedTotal += bytesTransfered; System.out.println((int) Math.floor((100.0 / size) * bytesTransferedTotal) + "%"); } } 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:com.ezac.gliderlogs.FlightOverviewActivity.java
@SuppressLint("SimpleDateFormat") public void GliderLogToDB(String DBPath, String DB, String device) { // format date SimpleDateFormat TSD = new SimpleDateFormat("yyyyMMdd_kkss"); SimpleDateFormat DIR = new SimpleDateFormat("yyyy/MM_dd"); Date myDate = new Date(); String backupDBPath = device + "_" + TSD.format(myDate); String TS_DIR = DIR.format(myDate); // to internal sdcard File dir = new File(Environment.getExternalStorageDirectory() + "/Download/" + TS_DIR); if (!dir.exists() || !dir.isDirectory()) { dir.mkdir();/*from ww w . j av a 2s .c o m*/ } File data = Environment.getDataDirectory(); // create a file channel object FileChannel src = null; FileChannel des = null; File currentDB = new File(data + "/data/" + DBPath + "/databases/", DB); File backupDB = new File(dir, backupDBPath); try { backupDB.delete(); src = new FileInputStream(currentDB).getChannel(); des = new FileOutputStream(backupDB).getChannel(); des.transferFrom(src, 0, src.size()); src.close(); des.close(); } catch (IOException e) { Log.d(TAG, e.toString()); e.printStackTrace(); } }
From source file:org.apache.hadoop.hdfs.server.datanode.FSDataset.java
/** * Copies a file as fast as possible. Tries to do a hardlink instead of a copy * if the hardlink parameter is specified. * * @param src/*from w w w . j a va 2s . com*/ * the source file for copying * @param dst * the destination file for copying * @param hardlink * whether or not to attempt a hardlink * @throws IOException */ public void copyFile(File src, File dst, boolean hardlink) throws IOException { if (src == null || dst == null) { throw new IOException("src/dst file is null"); } try { if (hardlink && shouldHardLinkBlockCopy) { // Remove destination before hard linking, since this file might already // exist and a hardlink would fail as a result. if (dst.exists()) { if (!dst.delete()) { throw new IOException("Deletion of file : " + dst + " failed"); } } NativeIO.link(src, dst); DataNode.LOG.info("Hard Link Created from : " + src + " to " + dst); return; } } catch (IOException e) { DataNode.LOG .warn("Hard link failed from : " + src + " to " + dst + " continuing with regular file copy"); } FileChannel input = null; FileChannel output = null; try { // This improves copying performance a lot, it uses native buffers // for copying. input = new FileInputStream(src).getChannel(); output = new FileOutputStream(dst).getChannel(); if (input == null || output == null) { throw new IOException("Could not create file channels for src : " + src + " dst : " + dst); } long bytesLeft = input.size(); long position = 0; while (bytesLeft > 0) { long bytesWritten = output.transferFrom(input, position, bytesLeft); bytesLeft -= bytesWritten; position += bytesWritten; } if (datanode.syncOnClose) { output.force(true); } } finally { if (input != null) { input.close(); } if (output != null) { output.close(); } } }
From source file:com.zoffcc.applications.zanavi.Navit.java
private static void copyFile(File sourceFile, File destFile) throws IOException { if (!sourceFile.exists()) { return;//from ww w . ja va 2 s.c om } if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); if (destination != null && source != null) { destination.transferFrom(source, 0, source.size()); } if (source != null) { source.close(); } if (destination != null) { destination.close(); } }
From source file:com.zoffcc.applications.zanavi.Navit.java
private void import_map_points_from_sdcard() { String orig_file = NAVIT_DATA_SHARE_DIR + Navit_DEST_FILENAME; String dest_file_dir = CFG_FILENAME_PATH + "../export/"; try {/*from ww w . j av a 2 s .c o m*/ File source = new File(dest_file_dir + Navit_DEST_FILENAME); File destination = new File(orig_file); if (source.exists()) { FileInputStream fi = new FileInputStream(source); FileOutputStream fo = new FileOutputStream(destination); FileChannel src = fi.getChannel(); FileChannel dst = fo.getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); fi.close(); fo.close(); } } catch (Exception e) { e.printStackTrace(); } read_map_points(); }
From source file:com.zoffcc.applications.zanavi.Navit.java
private void export_map_points_to_sdcard() { String orig_file = NAVIT_DATA_SHARE_DIR + Navit_DEST_FILENAME; String dest_file_dir = CFG_FILENAME_PATH + "../export/"; try {// w ww . j a v a 2 s.com File dir = new File(dest_file_dir); dir.mkdirs(); } catch (Exception e) { e.printStackTrace(); } try { File source = new File(orig_file); File destination = new File(dest_file_dir + Navit_DEST_FILENAME); if (source.exists()) { FileInputStream fi = new FileInputStream(source); FileOutputStream fo = new FileOutputStream(destination); FileChannel src = fi.getChannel(); FileChannel dst = fo.getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); fi.close(); fo.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.clark.func.Functions.java
/** * Internal copy file method.//w w w . ja v a 2 s. 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"); } 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) > FIFTY_MB ? FIFTY_MB : (size - pos); pos += output.transferFrom(input, pos, count); } } finally { closeQuietly(output); closeQuietly(fos); closeQuietly(input); 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()); } }