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:com.buaa.cfs.io.nativeio.NativeIO.java
/** * Unbuffered file copy from src to dst without tainting OS buffer cache * <p>//from w w w . j a va2 s .com * In POSIX platform: It uses FileChannel#transferTo() which internally attempts unbuffered IO on OS with native * sendfile64() support and falls back to buffered IO otherwise. * <p> * It minimizes the number of FileChannel#transferTo call by passing the the src file size directly instead of a * smaller size as the 3rd parameter. This saves the number of sendfile64() system call when native sendfile64() is * supported. In the two fall back cases where sendfile is not supported, FileChannle#transferTo already has its own * batching of size 8 MB and 8 KB, respectively. * <p> * In Windows Platform: It uses its own native wrapper of CopyFileEx with COPY_FILE_NO_BUFFERING flag, which is * supported on Windows Server 2008 and above. * <p> * Ideally, we should use FileChannel#transferTo() across both POSIX and Windows platform. Unfortunately, the * wrapper(Java_sun_nio_ch_FileChannelImpl_transferTo0) used by FileChannel#transferTo for unbuffered IO is not * implemented on Windows. Based on OpenJDK 6/7/8 source code, Java_sun_nio_ch_FileChannelImpl_transferTo0 on * Windows simply returns IOS_UNSUPPORTED. * <p> * Note: This simple native wrapper does minimal parameter checking before copy and consistency check (e.g., size) * after copy. It is recommended to use wrapper function like the Storage#nativeCopyFileUnbuffered() function in * hadoop-hdfs with pre/post copy checks. * * @param src The source path * @param dst The destination path * * @throws IOException */ public static void copyFileUnbuffered(File src, File dst) throws IOException { if (nativeLoaded && Shell.WINDOWS) { copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath()); } else { FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(src); fos = new FileOutputStream(dst); input = fis.getChannel(); output = fos.getChannel(); long remaining = input.size(); long position = 0; long transferred = 0; while (remaining > 0) { transferred = input.transferTo(position, remaining, output); remaining -= transferred; position += transferred; } } finally { IOUtils.cleanup(LOG, output); IOUtils.cleanup(LOG, fos); IOUtils.cleanup(LOG, input); IOUtils.cleanup(LOG, fis); } } }
From source file:org.rzo.yajsw.os.ms.win.w32.WindowsJavaHome.java
/** * Copy file./* w w w.j ava 2 s . c om*/ * * @param in * the in * @param out * the out * * @throws IOException * Signals that an I/O exception has occurred. */ void copyFile(File in, File out) throws IOException { System.out.println("copying : " + in.getAbsolutePath() + " -> " + out.getAbsolutePath()); FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:com.linkedin.pinot.core.segment.creator.impl.inv.OffHeapBitmapInvertedIndexCreator.java
@Override public void seal() throws IOException { FileOutputStream fos = null;/*from ww w . j a v a2s . com*/ FileInputStream fisOffsets = null; FileInputStream fisBitmaps = null; final DataOutputStream bitmapsOut; final DataOutputStream offsetsOut; String tempOffsetsFile = invertedIndexFile + ".offsets"; String tempBitmapsFile = invertedIndexFile + ".binary"; try { // build the posting list constructPostingLists(); // we need two separate streams, one to write the offsets and another to write the serialized // bitmap data. We need two because we dont the serialized length of each bitmap without // constructing. offsetsOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tempOffsetsFile))); bitmapsOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tempBitmapsFile))); // write out offsets of bitmaps. The information can be used to access a certain bitmap // directly. // Totally (invertedIndex.length+1) offsets will be written out; the last offset is used to // calculate the length of // the last bitmap, which might be needed when accessing bitmaps randomly. // If a bitmap's offset is k, then k bytes need to be skipped to reach the bitmap. int startOffset = 4 * (cardinality + 1); offsetsOut.writeInt(startOffset);// The first bitmap's offset MutableRoaringBitmap bitmap = new MutableRoaringBitmap(); for (int i = 0; i < cardinality; i++) { bitmap.clear(); int length = postingListLengths.get(i); for (int j = 0; j < length; j++) { int bufferOffset = postingListStartOffsets.get(i) + j; int value = postingListBuffer.get(bufferOffset); bitmap.add(value); } // serialize bitmap to bitmapsOut stream bitmap.serialize(bitmapsOut); startOffset += bitmap.serializedSizeInBytes(); // write offset offsetsOut.writeInt(startOffset); } offsetsOut.close(); bitmapsOut.close(); // merge the two files by simply writing offsets data first and then bitmap serialized data fos = new FileOutputStream(invertedIndexFile); fisOffsets = new FileInputStream(tempOffsetsFile); fisBitmaps = new FileInputStream(tempBitmapsFile); FileChannel channelOffsets = fisOffsets.getChannel(); channelOffsets.transferTo(0, channelOffsets.size(), fos.getChannel()); FileChannel channelBitmaps = fisBitmaps.getChannel(); channelBitmaps.transferTo(0, channelBitmaps.size(), fos.getChannel()); LOGGER.debug("persisted bitmap inverted index for column : " + spec.getName() + " in " + invertedIndexFile.getAbsolutePath()); } catch (Exception e) { LOGGER.error("Exception while creating bitmap index for column:" + spec.getName(), e); } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(fisOffsets); IOUtils.closeQuietly(fisOffsets); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(fos); // MMaputils handles the null checks for buffer MmapUtils.unloadByteBuffer(origValueBuffer); origValueBuffer = null; valueBuffer = null; if (origLengths != null) { MmapUtils.unloadByteBuffer(origLengths); origLengths = null; lengths = null; } MmapUtils.unloadByteBuffer(origPostingListBuffer); origPostingListBuffer = null; postingListBuffer = null; MmapUtils.unloadByteBuffer(origPostingListCurrentOffsets); origPostingListCurrentOffsets = null; postingListCurrentOffsets = null; MmapUtils.unloadByteBuffer(origPostingListLengths); origPostingListLengths = null; postingListLengths = null; MmapUtils.unloadByteBuffer(origPostingListStartOffsets); origPostingListStartOffsets = null; postingListStartOffsets = null; FileUtils.deleteQuietly(new File(tempOffsetsFile)); FileUtils.deleteQuietly(new File(tempBitmapsFile)); } }
From source file:org.chromium.APKPackager.java
private void copyFile(File src, File dest) throws FileNotFoundException, IOException { FileInputStream istream = new FileInputStream(src); FileOutputStream ostream = new FileOutputStream(dest); FileChannel input = istream.getChannel(); FileChannel output = ostream.getChannel(); try {// w w w . j av a 2s . c o m input.transferTo(0, input.size(), output); } finally { istream.close(); ostream.close(); input.close(); output.close(); } }
From source file:org.cloudata.core.commitlog.FileTransferThread.java
private void transfer(FileChannel[] channelList, SocketChannel socketChannel) { for (FileChannel fc : channelList) { try {//from www. jav a 2 s . com long numTransferred = 0; long pos = fc.position(); long totalLength = fc.size() - pos; long count = totalLength; LOG.info(dirName + " start File Transfer:" + pos + "," + totalLength); while ((numTransferred += fc.transferTo(pos, count, socketChannel)) < totalLength) { pos += numTransferred; count -= numTransferred; } //LOG.info("End File Transfer:" + pos + "," + totalLength); } catch (IOException e) { LOG.warn("transfering file is fail due to : ", e); } finally { if (fc != null) { try { fc.close(); } catch (IOException e) { LOG.warn("closing file is fail due to : ", e); } } } } }
From source file:org.gnucash.android.export.ExporterAsyncTask.java
/** * Copies a file from <code>src</code> to <code>dst</code> * @param src Absolute path to the source file * @param dst Absolute path to the destination file * @throws IOException if the file could not be copied *//* ww w . j a va2 s. c o m*/ public void copyFile(File src, File dst) throws IOException { //TODO: Make this asynchronous at some time, t in the future. if (mExportParams.getExportFormat() == ExportFormat.QIF) { splitQIF(src, dst); } else { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } }
From source file:net.ytbolg.mcxa.ImportOldMc.java
void fileChannelCopy(File s, File t) { FileInputStream fi = null;/*from w w w . j av a 2 s .co m*/ FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();//? out = fo.getChannel();//? in.transferTo(0, in.size(), out);//?in???out? } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.filemanager.free.filesystem.FileUtil.java
/** * Copy a file. The target file may even be on external SD card for Kitkat. * * @param source The source file//from w w w .j a v a 2s . c o m * @param target The target file * @return true if the copying was successful. */ @SuppressWarnings("null") public static boolean copyFile(final File source, final File target, Context context) { FileInputStream inStream = null; OutputStream outStream = null; FileChannel inChannel = null; FileChannel outChannel = null; try { inStream = new FileInputStream(source); // First try the normal way if (isWritable(target)) { // standard way outStream = new FileOutputStream(target); inChannel = inStream.getChannel(); outChannel = ((FileOutputStream) outStream).getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Storage Access Framework DocumentFile targetDocument = getDocumentFile(target, false, context); outStream = context.getContentResolver().openOutputStream(targetDocument.getUri()); } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { // Workaround for Kitkat ext SD card Uri uri = MediaStoreHack.getUriFromFile(target.getAbsolutePath(), context); outStream = context.getContentResolver().openOutputStream(uri); } else { return false; } if (outStream != null) { // Both for SAF and for Kitkat, write to output stream. byte[] buffer = new byte[16384]; // MAGIC_NUMBER int bytesRead; while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } } } } catch (Exception e) { Log.e("AmazeFileUtils", "Error when copying file from " + source.getAbsolutePath() + " to " + target.getAbsolutePath(), e); return false; } finally { try { inStream.close(); } catch (Exception e) { // ignore exception } try { outStream.close(); } catch (Exception e) { // ignore exception } try { inChannel.close(); } catch (Exception e) { // ignore exception } try { outChannel.close(); } catch (Exception e) { // ignore exception } } return true; }
From source file:org.forgerock.openidm.script.javascript.JavaScriptFactory.java
private Script initializeScript(String name, File source, boolean sharedScope) throws ScriptException { initDebugListener();//from w ww .j av a 2s . com if (debugInitialised) { try { FileChannel inChannel = new FileInputStream(source).getChannel(); FileChannel outChannel = new FileOutputStream(getTargetFile(name)).getChannel(); FileLock outLock = outChannel.lock(); FileLock inLock = inChannel.lock(0, inChannel.size(), true); inChannel.transferTo(0, inChannel.size(), outChannel); outLock.release(); inLock.release(); inChannel.close(); outChannel.close(); } catch (IOException e) { logger.warn("JavaScript source was not updated for {}", name, e); } } return new JavaScript(name, source, sharedScope); }
From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java
private void copyFile(StudyFileEditBean editBean) throws IOException { File tempFile = new File(editBean.getTempSystemFileLocation()); dbgLog.fine("begin copyFile()"); // create a sub-directory "ingested" File newDir = new File(tempFile.getParentFile(), "ingested"); if (!newDir.exists()) { newDir.mkdirs();/* w w w .j a va 2 s . c o m*/ } dbgLog.fine("newDir: abs path:\n" + newDir.getAbsolutePath()); File newFile = new File(newDir, tempFile.getName()); FileInputStream fis = new FileInputStream(tempFile); FileOutputStream fos = new FileOutputStream(newFile); FileChannel fcin = fis.getChannel(); FileChannel fcout = fos.getChannel(); fcin.transferTo(0, fcin.size(), fcout); fcin.close(); fcout.close(); fis.close(); fos.close(); dbgLog.fine("newFile: abs path:\n" + newFile.getAbsolutePath()); // store the tab-file location editBean.setIngestedSystemFileLocation(newFile.getAbsolutePath()); }