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.alfresco.repo.content.AbstractContentReader.java
/** * {@inheritDoc}/*from w ww. ja va2 s . c o m*/ */ public FileChannel getFileChannel() throws ContentIOException { /* * Where the underlying support is not present for this method, a temporary * file will be used as a substitute. When the write is complete, the * results are copied directly to the underlying channel. */ // get the underlying implementation's best readable channel channel = getReadableChannel(); // now use this channel if it can provide the random access, otherwise spoof it FileChannel clientFileChannel = null; if (channel instanceof FileChannel) { // all the support is provided by the underlying implementation clientFileChannel = (FileChannel) channel; // debug if (logger.isDebugEnabled()) { logger.debug("Content reader provided direct support for FileChannel: \n" + " reader: " + this); } } else { // No random access support is provided by the implementation. // Spoof it by providing a 2-stage read from a temp file File tempFile = TempFileProvider.createTempFile("random_read_spoof_", ".bin"); FileContentWriter spoofWriter = new FileContentWriter(tempFile); // pull the content in from the underlying channel FileChannel spoofWriterChannel = spoofWriter.getFileChannel(false); try { long spoofFileSize = this.getSize(); spoofWriterChannel.transferFrom(channel, 0, spoofFileSize); } catch (IOException e) { throw new ContentIOException( "Failed to copy from permanent channel to spoofed temporary channel: \n" + " reader: " + this + "\n" + " temp: " + spoofWriter, e); } finally { try { spoofWriterChannel.close(); } catch (IOException e) { } } // get a reader onto the spoofed content final ContentReader spoofReader = spoofWriter.getReader(); // Attach a listener // - ensure that the close call gets propogated to the underlying channel ContentStreamListener spoofListener = new ContentStreamListener() { public void contentStreamClosed() throws ContentIOException { try { channel.close(); } catch (IOException e) { throw new ContentIOException("Failed to close underlying channel", e); } } }; spoofReader.addListener(spoofListener); // we now have the spoofed up channel that the client can work with clientFileChannel = spoofReader.getFileChannel(); // debug if (logger.isDebugEnabled()) { logger.debug("Content writer provided indirect support for FileChannel: \n" + " writer: " + this + "\n" + " temp writer: " + spoofWriter); } } // the file is now available for random access return clientFileChannel; }
From source file:org.talend.designer.core.ui.preferences.I18nPreferencePage.java
private void transferStream(File sourceFile, File targetFile) { if (!sourceFile.exists()) { return;// w w w. j ava 2 s .com } try { FileChannel sourceChannel = new FileInputStream(sourceFile.getAbsoluteFile()).getChannel(); FileChannel targetChannel = new FileOutputStream(targetFile.getAbsoluteFile()).getChannel(); targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); sourceChannel.close(); targetChannel.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.wso2.appserver.integration.tests.config.EnvironmentVariableReadTestCase.java
public void applyConfiguration(File sourceFile, File targetFile) throws IOException { FileChannel source = null;/*from w w w . j av a 2 s .c o m*/ FileChannel destination = null; if (!targetFile.exists() && !targetFile.createNewFile()) { throw new IOException("File " + targetFile + "creation fails"); } source = (new FileInputStream(sourceFile)).getChannel(); destination = (new FileOutputStream(targetFile)).getChannel(); destination.transferFrom(source, 0L, source.size()); if (source != null) { source.close(); } if (destination != null) { destination.close(); } }
From source file:com.aimluck.eip.services.storage.impl.ALDefaultStorageHanlder.java
@Override public boolean copyFile(String srcRootPath, String srcDir, String srcFileName, String destRootPath, String destDir, String destFileName) { File srcPath = new File(srcRootPath + separator() + Database.getDomainName() + separator() + srcDir); if (!srcPath.exists()) { try {/*from w w w . j a va2 s. com*/ srcPath.mkdirs(); } catch (Exception e) { logger.error("Can't create directory...:" + srcPath); return false; } } File destPath = new File(destRootPath + separator() + Database.getDomainName() + separator() + destDir); if (!destPath.exists()) { try { destPath.mkdirs(); } catch (Exception e) { logger.error("Can't create directory...:" + destPath); return false; } } File from = new File(srcPath + separator() + srcFileName); File to = new File(destPath + separator() + destFileName); boolean res = true; FileChannel srcChannel = null; FileChannel destChannel = null; try { srcChannel = new FileInputStream(from).getChannel(); destChannel = new FileOutputStream(to).getChannel(); destChannel.transferFrom(srcChannel, 0, srcChannel.size()); } catch (Exception ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } finally { if (destChannel != null) { try { destChannel.close(); } catch (IOException ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } } if (srcChannel != null) { try { srcChannel.close(); } catch (IOException ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } } } return res; }
From source file:com.amaze.filemanager.utils.files.GenericCopyUtil.java
private void 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.getSize()); }
From source file:org.multibit.file.FileHandler.java
public static void copyFile(File sourceFile, File destinationFile) throws IOException { if (!destinationFile.exists()) { destinationFile.createNewFile(); }/* w w w.j av a 2 s .c om*/ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; FileChannel source = null; FileChannel destination = null; try { fileInputStream = new FileInputStream(sourceFile); source = fileInputStream.getChannel(); fileOutputStream = new FileOutputStream(destinationFile); destination = fileOutputStream.getChannel(); long transfered = 0; long bytes = source.size(); while (transfered < bytes) { transfered += destination.transferFrom(source, 0, source.size()); destination.position(transfered); } } finally { if (source != null) { source.close(); source = null; } else if (fileInputStream != null) { fileInputStream.close(); fileInputStream = null; } if (destination != null) { destination.close(); destination = null; } else if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } }
From source file:org.kepler.ssh.LocalExec.java
/** * Copies src file to dst file. If the dst file does not exist, it is * created/*from w w w .j a v a2 s.com*/ */ private void copyFile(File src, File dst) throws IOException { // see if source and destination are the same if (src.equals(dst)) { // do not copy return; } //System.out.println("copying " + src + " to " + dst); FileChannel srcChannel = new FileInputStream(src).getChannel(); FileChannel dstChannel = new FileOutputStream(dst).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); /* hacking for non-windows */ // set the permission of the target file the same as the source file if (_commandArr[0] == "/bin/sh") { String osName = StringUtilities.getProperty("os.name"); if (osName.startsWith("Mac OS X")) { // chmod --reference does not exist on mac, so do the best // we can using the java file api // WARNING: this relies on the umask to set the group, world // permissions. dst.setExecutable(src.canExecute()); dst.setWritable(src.canWrite()); } else { String cmd = "chmod --reference=" + src.getAbsolutePath() + " " + dst.getAbsolutePath(); try { ByteArrayOutputStream streamOut = new ByteArrayOutputStream(); ByteArrayOutputStream streamErr = new ByteArrayOutputStream(); executeCmd(cmd, streamOut, streamErr); } catch (ExecException e) { log.warn("Tried to set the target file permissions the same as " + "the source but the command failed: " + cmd + "\n" + e); } } } }
From source file:org.cloudml.connectors.util.MercurialConnector.java
private void copyFile(File f) { FileChannel sourceChannel = null; FileChannel destChannel = null; try {//from w ww . j a v a 2 s. co m sourceChannel = new FileInputStream(f).getChannel(); String destPath; if (!System.getProperty("os.name").toLowerCase().contains("windows")) { destPath = directory + "/" + filename; } else { destPath = directory + "\\" + filename; } File destFile = new File(destPath); if (!destFile.exists()) { destFile.createNewFile(); } destChannel = new FileOutputStream(destFile).getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.krawler.portal.tools.FileImpl.java
public void copyFile(File source, File destination, boolean lazy) { if (!source.exists()) { return;//w w w . ja va 2 s . c o m } if (lazy) { String oldContent = null; try { oldContent = read(source); } catch (Exception e) { logger.warn(e.getMessage(), e); return; } String newContent = null; try { newContent = read(destination); } catch (Exception e) { logger.warn(e.getMessage(), e); } if ((oldContent == null) || !oldContent.equals(newContent)) { copyFile(source, destination, false); } } else { if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } 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.warn(ioe.getMessage(), ioe); // _log.error(ioe.getMessage()); } } }