List of usage examples for java.nio.channels FileChannel transferTo
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;
From source file:gov.nasa.ensemble.common.io.FileUtilities.java
/** * Make a copy of a file on the filesystem (platform independent). * /*w w w . j av a2s. c om*/ * @param source * the file to copy. * @param dest * the copy to make. * @return whether the file copy exists on the filesystem upon completion. * @throws IOException */ public static boolean copyFile(File source, File dest) throws IOException { FileInputStream sourceStream = new FileInputStream(source); FileChannel sourceChannel = sourceStream.getChannel(); FileOutputStream destStream = new FileOutputStream(dest); FileChannel destChannel = destStream.getChannel(); try { sourceChannel.transferTo(0, sourceChannel.size(), destChannel); } catch (IOException ex) { if (ex.getCause() instanceof OutOfMemoryError) { IOUtils.copy(sourceStream, destStream); } } finally { destChannel.close(); IOUtils.closeQuietly(destStream); sourceChannel.close(); IOUtils.closeQuietly(sourceStream); } return dest.exists(); }
From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java
public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {/*www . j av a 2s .com*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:com.opendesign.utils.CmnUtil.java
/** * ? //from w w w.jav a 2s .co m * * @param oldFilePath * @param newFilePath */ public static void fileCopy(String oldFilePath, String newFilePath) { File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); try { FileInputStream inputStream = new FileInputStream(oldFile); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel fcin = inputStream.getChannel(); FileChannel fcout = outputStream.getChannel(); long size = fcin.size(); fcin.transferTo(0, size, fcout); fcout.close(); fcin.close(); outputStream.close(); inputStream.close(); } catch (Exception e) { } }
From source file:com.opendesign.utils.CmnUtil.java
/** * ? /*from ww w . j a va 2s. c o m*/ * * @param oldFilePath * @param oldFileName * @param newFilePath * @param newFileName */ public static void fileCopy(String oldFilePath, String oldFileName, String newFilePath, String newFileName) { File oldFile = new File(oldFilePath, oldFileName); File newFile = new File(newFilePath, newFileName); try { FileInputStream inputStream = new FileInputStream(oldFile); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel fcin = inputStream.getChannel(); FileChannel fcout = outputStream.getChannel(); long size = fcin.size(); fcin.transferTo(0, size, fcout); fcout.close(); fcin.close(); outputStream.close(); inputStream.close(); } catch (Exception e) { } }
From source file:org.eclipse.smila.binarystorage.persistence.io.BssIOUtils.java
/** * Copies data from input stream into persistence location, by using channels. * /*from w w w .j ava 2 s . c om*/ * @param path * @param stream * @throws BinaryStorageException */ private static void writeCopyByChannels(final String path, final InputStream stream) throws BinaryStorageException { FileChannel inChannel = null; FileChannel outChannel = null; try { inChannel = ((FileInputStream) stream).getChannel(); outChannel = new FileOutputStream(path).getChannel(); if (inChannel.size() < FILE_SIZE_64MB) { outChannel.transferFrom(inChannel, 0, inChannel.size()); } else { // the transferTo() does not transfer files > than 2^31-1 bytes final long size = inChannel.size(); long position = 0; while (position < size) { position += inChannel.transferTo(position, FILE_SIZE_TRANSFER, outChannel); } } } catch (final IOException ioe) { throw new BinaryStorageException(ioe, "Could not write binary record to :" + path); } finally { // Close the channels closeChannel(inChannel); closeChannel(outChannel); } }
From source file:org.xwoot.xwootUtil.FileUtil.java
/** * Copy a file from one location to another. * <p>/* w ww.j av a 2s .co m*/ * If the destination file exists, it will be overridden. * * @param sourceFile the file to copy from. * @param destinationFile the file to copy to. * @throws IOException if the sourceFilePath is not found or other IO problems occur. */ public static void copyFile(File sourceFile, File destinationFile) throws IOException { // check destination. FileUtil.checkDirectoryPath(destinationFile.getParentFile()); // do the actual copy FileChannel sourceChannel = null; FileChannel destinationChannel = null; try { sourceChannel = new FileInputStream(sourceFile).getChannel(); destinationChannel = new FileOutputStream(destinationFile).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); } catch (IOException e) { throw e; } finally { try { if (sourceChannel != null) { sourceChannel.close(); } if (destinationChannel != null) { destinationChannel.close(); } } catch (IOException e) { throw e; } } }
From source file:li.barter.utils.Utils.java
public static boolean copyFile(final File src, final File dst) { boolean returnValue = true; FileChannel inChannel = null, outChannel = null; try {/* w w w . ja v a 2 s .co m*/ inChannel = new FileInputStream(src).getChannel(); outChannel = new FileOutputStream(dst).getChannel(); } catch (final FileNotFoundException fnfe) { Logger.d(TAG, "inChannel/outChannel FileNotFoundException"); fnfe.printStackTrace(); return false; } try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (final IllegalArgumentException iae) { Logger.d(TAG, "TransferTo IllegalArgumentException"); iae.printStackTrace(); returnValue = false; } catch (final NonReadableChannelException nrce) { Logger.d(TAG, "TransferTo NonReadableChannelException"); nrce.printStackTrace(); returnValue = false; } catch (final NonWritableChannelException nwce) { Logger.d(TAG, "TransferTo NonWritableChannelException"); nwce.printStackTrace(); returnValue = false; } catch (final ClosedByInterruptException cie) { Logger.d(TAG, "TransferTo ClosedByInterruptException"); cie.printStackTrace(); returnValue = false; } catch (final AsynchronousCloseException ace) { Logger.d(TAG, "TransferTo AsynchronousCloseException"); ace.printStackTrace(); returnValue = false; } catch (final ClosedChannelException cce) { Logger.d(TAG, "TransferTo ClosedChannelException"); cce.printStackTrace(); returnValue = false; } catch (final IOException ioe) { Logger.d(TAG, "TransferTo IOException"); ioe.printStackTrace(); returnValue = false; } finally { if (inChannel != null) { try { inChannel.close(); } catch (final IOException e) { e.printStackTrace(); } } if (outChannel != null) { try { outChannel.close(); } catch (final IOException e) { e.printStackTrace(); } } } return returnValue; }
From source file:bridge.toolkit.commands.S1000DConverter.java
/** * copy file content/*w w w . j ava2 s. c o m*/ * * @param from_ * String * @param to_ * String * @throws Exception */ public static void copy(String from_, String to_) throws Exception { try { FileChannel sourceChannel = new FileInputStream(from_).getChannel(); FileChannel destinationChannel = new FileOutputStream(to_).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); sourceChannel.close(); destinationChannel.close(); } catch (Exception ex) { throw (ex); } }
From source file:gridool.util.xfer.TransferUtils.java
public static void sendfile(@Nonnull final File file, final long fromPos, final long count, @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort, final boolean append, final boolean sync, @Nonnull final TransferClientHandler handler) throws IOException { if (!file.exists()) { throw new IllegalArgumentException(file.getAbsolutePath() + " does not exist"); }//from w w w .j a va 2s.co m if (!file.isFile()) { throw new IllegalArgumentException(file.getAbsolutePath() + " is not file"); } if (!file.canRead()) { throw new IllegalArgumentException(file.getAbsolutePath() + " cannot read"); } final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort); SocketChannel channel = null; Socket socket = null; final OutputStream out; try { channel = SocketChannel.open(); socket = channel.socket(); socket.connect(dstSockAddr); out = socket.getOutputStream(); } catch (IOException e) { LOG.error("failed to connect: " + dstSockAddr, e); IOUtils.closeQuietly(channel); NetUtils.closeQuietly(socket); throw e; } DataInputStream din = null; if (sync) { InputStream in = socket.getInputStream(); din = new DataInputStream(in); } final DataOutputStream dos = new DataOutputStream(out); final StopWatch sw = new StopWatch(); FileInputStream src = null; final long nbytes; try { src = new FileInputStream(file); FileChannel fc = src.getChannel(); String fileName = file.getName(); IOUtils.writeString(fileName, dos); IOUtils.writeString(writeDirPath, dos); long xferBytes = (count == -1L) ? fc.size() : count; dos.writeLong(xferBytes); dos.writeBoolean(append); // append=false dos.writeBoolean(sync); if (handler == null) { dos.writeBoolean(false); } else { dos.writeBoolean(true); handler.writeAdditionalHeader(dos); } // send file using zero-copy send nbytes = fc.transferTo(fromPos, xferBytes, channel); if (LOG.isDebugEnabled()) { LOG.debug("Sent a file '" + file.getAbsolutePath() + "' of " + nbytes + " bytes to " + dstSockAddr.toString() + " in " + sw.toString()); } if (sync) {// receive ack in sync mode long remoteRecieved = din.readLong(); if (remoteRecieved != xferBytes) { throw new IllegalStateException( "Sent " + xferBytes + " bytes, but remote node received " + remoteRecieved + " bytes"); } } } catch (FileNotFoundException e) { LOG.error(PrintUtils.prettyPrintStackTrace(e, -1)); throw e; } catch (IOException e) { LOG.error(PrintUtils.prettyPrintStackTrace(e, -1)); throw e; } finally { IOUtils.closeQuietly(src); IOUtils.closeQuietly(din, dos); IOUtils.closeQuietly(channel); NetUtils.closeQuietly(socket); } }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.FileCopier.java
/** * Copies the contents of a file into a new file in the given directory. The new file will have the same name as * the original file./*from w w w. ja va2s .c o m*/ * * @param fromFile the file to copy * @param deployDirectory the directory in which to copy the file -- if this is not a directory, the new file will * be created with this name * * @return the File that was created * * @throws IOException if there are I/O errors during reading or writing */ public static File copy(final File fromFile, final File deployDirectory) throws IOException { // if not a directory, then just use deployDirectory as the filename for the copy File destination = deployDirectory; if (deployDirectory.isDirectory()) { // otherwise use original file's name destination = new File(deployDirectory, fromFile.getName()); } FileInputStream fileInputStream = null; FileChannel sourceChannel = null; FileOutputStream fileOutputStream = null; FileChannel destinationChannel = null; try { //noinspection IOResourceOpenedButNotSafelyClosed fileInputStream = new FileInputStream(fromFile); //noinspection ChannelOpenedButNotSafelyClosed sourceChannel = fileInputStream.getChannel(); //noinspection IOResourceOpenedButNotSafelyClosed fileOutputStream = new FileOutputStream(destination); //noinspection ChannelOpenedButNotSafelyClosed destinationChannel = fileOutputStream.getChannel(); final int maxCount = (64 * 1024 * 1024) - (32 * 1024); final long size = sourceChannel.size(); long position = 0; while (position < size) { position += sourceChannel.transferTo(position, maxCount, destinationChannel); } } catch (IOException ie) { throw new IOException("Failed to copy " + fromFile + " to " + deployDirectory + ".\n Error Details: " + ie.toString()); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(sourceChannel); IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(destinationChannel); } return destination; }