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

The following examples show how to use java.nio.channels.FileChannel#transferTo() . 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: FileUtils.java    From EasyPhotos with Apache License 2.0 6 votes vote down vote up
/**
 * Copies one file into the other with the given paths.
 * In the event that the paths are the same, trying to copy one file to the other
 * will cause both files to become null.
 * Simply skipping this step if the paths are identical.
 */
public static boolean copyFile(FileInputStream inputStream, String outFilePath) throws IOException {
    if (inputStream == null) {
        return false;
    }
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = inputStream.getChannel();
        outputChannel = new FileOutputStream(new File(outFilePath)).getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        inputChannel.close();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (inputChannel != null) inputChannel.close();
        if (outputChannel != null) outputChannel.close();
    }
}
 
Example 2
Source File: IOUtils.java    From bytekit with Apache License 2.0 6 votes vote down vote up
/**
 * 文件拷贝
 * @param src source {@link File}
 * @param dst destination {@link File}
 * @throws IOException
 */
public static void copyFile(File src, File dst) throws IOException {
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dst);
    FileChannel inChannel = in.getChannel();
    FileChannel outChannel = out.getChannel();

    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }

    in.close();
    out.close();
}
 
Example 3
Source File: UpdateHandler.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
/**
 * Copies in to out using FileChannels.
 *
 * This tries to use channels for fast copying. If it doesn't work, fall back to
 * copyFileFallBack below.
 *
 * @param in the stream to copy from.
 * @param out the stream to copy to.
 * @throws IOException if both the normal and fallback methods raise exceptions.
 */
private static void copyFile(final InputStream in, final OutputStream out)
        throws IOException {
    DebugLogUtils.l("Copying files");
    if (!(in instanceof FileInputStream) || !(out instanceof FileOutputStream)) {
        DebugLogUtils.l("Not the right types");
        copyFileFallback(in, out);
    } else {
        try {
            final FileChannel sourceChannel = ((FileInputStream) in).getChannel();
            final FileChannel destinationChannel = ((FileOutputStream) out).getChannel();
            sourceChannel.transferTo(0, Integer.MAX_VALUE, destinationChannel);
        } catch (IOException e) {
            // Can't work with channels, or something went wrong. Copy by hand.
            DebugLogUtils.l("Won't work");
            copyFileFallback(in, out);
        }
    }
}
 
Example 4
Source File: FileUtilRt.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void copy(@Nonnull InputStream inputStream, @Nonnull OutputStream outputStream) throws IOException {
  if (USE_FILE_CHANNELS && inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
    final FileChannel fromChannel = ((FileInputStream)inputStream).getChannel();
    try {
      final FileChannel toChannel = ((FileOutputStream)outputStream).getChannel();
      try {
        fromChannel.transferTo(0, Long.MAX_VALUE, toChannel);
      }
      finally {
        toChannel.close();
      }
    }
    finally {
      fromChannel.close();
    }
  }
  else {
    final byte[] buffer = getThreadLocalBuffer();
    while (true) {
      int read = inputStream.read(buffer);
      if (read < 0) break;
      outputStream.write(buffer, 0, read);
    }
  }
}
 
Example 5
Source File: FileUtil.java    From haystack with MIT License 6 votes vote down vote up
public void insert(String filename, long offset, String content) {
    try {
        RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
        RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
        long fileSize = r.length();
        FileChannel sourceChannel = r.getChannel();
        FileChannel targetChannel = rtemp.getChannel();
        sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
        sourceChannel.truncate(offset);
        r.seek(offset);
        r.writeUTF(content);
        long newOffset = r.getFilePointer();
        targetChannel.position(0L);
        sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
        sourceChannel.close();
        targetChannel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: FileUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Copy a single file using NIO.
 * @throws IOException
 */
private static void nioCopy(FileOutputStream fos, FileInputStream fis)
    throws IOException {
  FileChannel outChannel = fos.getChannel();
  FileChannel inChannel = fis.getChannel();
  long length = inChannel.size();
  long offset = 0;
  while(true) {
    long remaining = length - offset;
    
    long toTransfer = remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE;
    long transferredBytes = inChannel.transferTo(offset, toTransfer, outChannel);
    offset += transferredBytes;
    length = inChannel.size();
    if(offset >= length) {
      break;
    }
  }
}
 
Example 7
Source File: FileUtil.java    From apkReSign with Apache License 2.0 6 votes vote down vote up
public static void copyFile(FileInputStream source, File target) {
    FileChannel in = null;
    FileChannel out = null;
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = source;
        outStream = new FileOutputStream(target);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(inStream);
        close(in);
        close(outStream);
        close(out);
    }
}
 
Example 8
Source File: IOTinyUtils.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
static public void copyFile(String source, String target) throws IOException {
    File sf = new File(source);
    if (!sf.exists()) {
        throw new IllegalArgumentException("source file does not exist.");
    }
    File tf = new File(target);
    tf.getParentFile().mkdirs();
    if (!tf.exists() && !tf.createNewFile()) {
        throw new RuntimeException("failed to create target file.");
    }

    FileChannel sc = null;
    FileChannel tc = null;
    try {
        tc = new FileOutputStream(tf).getChannel();
        sc = new FileInputStream(sf).getChannel();
        sc.transferTo(0, sc.size(), tc);
    } finally {
        if (null != sc) {
            sc.close();
        }
        if (null != tc) {
            tc.close();
        }
    }
}
 
Example 9
Source File: StorageUtil.java    From archiva with Apache License 2.0 5 votes vote down vote up
public static final void copy( final FileChannel is, final WritableByteChannel os ) {
    try
    {
        is.transferTo( 0, is.size( ), os );
    }
    catch ( IOException e )
    {
        throw new RuntimeException( e );
    }
}
 
Example 10
Source File: Utils.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void copyFile(File from, File to) throws IOException {
    if (!from.exists()) {
        throw new FileNotFoundException();
    }
    if (from.isDirectory() || to.isDirectory()) {
        throw new FileNotFoundException();
    }
    FileInputStream fi = null;
    FileChannel in = null;
    FileOutputStream fo = null;
    FileChannel out = null;
    try {
        if (!to.exists()) {
            to.createNewFile();
        }
        fi = new FileInputStream(from);
        in = fi.getChannel();
        fo = new FileOutputStream(to);
        out = fo.getChannel();
        in.transferTo(0, in.size(), out);
    } finally {
        if (fi != null) fi.close();
        if (in != null) in.close();
        if (fo != null) fo.close();
        if (out != null) out.close();
    }
}
 
Example 11
Source File: FileIO.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public static void copyFile(File src, File dst) throws IOException {
    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();
        }
    }
}
 
Example 12
Source File: Transfer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void xferTest07() throws Exception { // for bug 5103988
    File source = File.createTempFile("source", null);
    source.deleteOnExit();

    FileChannel sourceChannel = new RandomAccessFile(source, "rw")
        .getChannel();
    sourceChannel.position(32000L)
        .write(ByteBuffer.wrap("The End".getBytes()));

    // The sink is a non-blocking socket channel
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(0));
    InetSocketAddress sa = new InetSocketAddress(
        InetAddress.getLocalHost(), ssc.socket().getLocalPort());
    SocketChannel sink = SocketChannel.open(sa);
    sink.configureBlocking(false);
    SocketChannel other = ssc.accept();

    long size = sourceChannel.size();

    // keep sending until congested
    long n;
    do {
        n = sourceChannel.transferTo(0, size, sink);
    } while (n > 0);

    sourceChannel.close();
    sink.close();
    other.close();
    ssc.close();
    source.delete();
}
 
Example 13
Source File: SimpleNIOFileStore.java    From cloudhopper-commons with Apache License 2.0 5 votes vote down vote up
private void channelCopyFromFile(final FileChannel src, final WritableByteChannel dest) throws IOException
   {
long position = 0;
long transferred = 0;
do {
    transferred = src.transferTo(position, B16K, dest);
    position += transferred;
} while (transferred >= B16K);
   }
 
Example 14
Source File: AjpServerResponseConduit.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public long transferFrom(final FileChannel src, final long position, final long count) throws IOException {
    return src.transferTo(position, count, new ConduitWritableByteChannel(this));
}
 
Example 15
Source File: SocketOutputStream.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Transfers data from FileChannel using 
 * {@link FileChannel#transferTo(long, long, WritableByteChannel)}.
 * Updates <code>waitForWritableTime</code> and <code>transferToTime</code>
 * with the time spent blocked on the network and the time spent transferring
 * data from disk to network respectively.
 * 
 * Similar to readFully(), this waits till requested amount of 
 * data is transfered.
 * 
 * @param fileCh FileChannel to transfer data from.
 * @param position position within the channel where the transfer begins
 * @param count number of bytes to transfer.
 * @param waitForWritableTime nanoseconds spent waiting for the socket 
 *        to become writable
 * @param transferTime nanoseconds spent transferring data
 * 
 * @throws EOFException 
 *         If end of input file is reached before requested number of 
 *         bytes are transfered.
 *
 * @throws SocketTimeoutException 
 *         If this channel blocks transfer longer than timeout for 
 *         this stream.
 *          
 * @throws IOException Includes any exception thrown by 
 *         {@link FileChannel#transferTo(long, long, WritableByteChannel)}. 
 */
public void transferToFully(FileChannel fileCh, long position, int count,
    LongWritable waitForWritableTime,
    LongWritable transferToTime) throws IOException {
  long waitTime = 0;
  long transferTime = 0;
  while (count > 0) {
    /* 
     * Ideally we should wait after transferTo returns 0. But because of
     * a bug in JRE on Linux (http://bugs.sun.com/view_bug.do?bug_id=5103988),
     * which throws an exception instead of returning 0, we wait for the
     * channel to be writable before writing to it. If you ever see 
     * IOException with message "Resource temporarily unavailable" 
     * thrown here, please let us know.
     * 
     * Once we move to JAVA SE 7, wait should be moved to correct place.
     */
    long start = System.nanoTime();
    waitForWritable();
    long wait = System.nanoTime();

    int nTransfered = (int) fileCh.transferTo(position, count, getChannel());
    
    if (nTransfered == 0) {
      //check if end of file is reached.
      if (position >= fileCh.size()) {
        throw new EOFException("EOF Reached. file size is " + fileCh.size() + 
                               " and " + count + " more bytes left to be " +
                               "transfered.");
      }
      //otherwise assume the socket is full.
      //waitForWritable(); // see comment above.
    } else if (nTransfered < 0) {
      throw new IOException("Unexpected return of " + nTransfered + 
                            " from transferTo()");
    } else {
      position += nTransfered;
      count -= nTransfered;
    }
    long transfer = System.nanoTime();
    waitTime += wait - start;
    transferTime += transfer - wait;
  }
  
  if (waitForWritableTime != null) {
    waitForWritableTime.set(waitTime);
  }
  if (transferToTime != null) {
    transferToTime.set(transferTime);
  }
}
 
Example 16
Source File: ServiceUtils.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
/**
 * Append the data in sourceFile to destinationFile.
 *
 * Note that the sourceFile is deleted after appending the data.
 *
 * @param sourceFile
 *                 The file that is to be appended.
 * @param destinationFile
 *                 The file to append to.
 */
public static void appendFile(File sourceFile, File destinationFile) {
    ValidationUtils.assertNotNull(destinationFile, "destFile");
    ValidationUtils.assertNotNull(sourceFile, "sourceFile");
    if (!FileLocks.lock(sourceFile)) {
        throw new FileLockException("Fail to lock " + sourceFile);
    }
    if (!FileLocks.lock(destinationFile)) {
        throw new FileLockException("Fail to lock " + destinationFile);
    }

    FileChannel in = null;
    FileChannel out = null;
    try {
        in = new FileInputStream(sourceFile).getChannel();
        out = new FileOutputStream(destinationFile, true).getChannel();
        final long size = in.size();
        // In some Windows platforms, copying large files fail due to insufficient system resources.
        // Limit copy size to 32 MB in each transfer
        final long count = 32 * MB;
        long position = 0;

        while (position < size) {
            position += in.transferTo(position, count, out);
        }
    } catch (IOException e) {
        throw new SdkClientException("Unable to append file " + sourceFile.getAbsolutePath()
                + "to destination file " + destinationFile.getAbsolutePath() + "\n" + e.getMessage(), e);
    } finally {
        closeQuietly(out, LOG);
        closeQuietly(in, LOG);
        FileLocks.unlock(sourceFile);
        FileLocks.unlock(destinationFile);
        try {
            if (!sourceFile.delete()) {
                LOG.warn("Failed to delete file " + sourceFile.getAbsolutePath());
            }
        } catch (SecurityException exception) {
            LOG.warn("Security manager denied delete access to file " + sourceFile.getAbsolutePath());
        }
    }
}
 
Example 17
Source File: Socket.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public long transferFrom(FileChannel src, long position, long count) throws IOException{
    return src.transferTo(position, count, writer);
}
 
Example 18
Source File: MemIndexSnapshot.java    From light-task-scheduler with Apache License 2.0 4 votes vote down vote up
@Override
protected void loadFromDisk() throws IOException {

    FileUtils.createDirIfNotExist(storeConfig.getIndexPath());

    String[] indexFiles = getIndexFiles();
    if (indexFiles == null || indexFiles.length == 0) {
        return;
    }

    FileChannel fileChannel = null;
    try {
        File lastSnapshot = new File(storeConfig.getIndexPath(), indexFiles[indexFiles.length - 1]);
        fileChannel = FileUtils.newFileChannel(lastSnapshot, "rw");
        IndexSnapshotFileHeader fileHeader = new IndexSnapshotFileHeader();
        fileHeader.read(fileChannel);

        ConcurrentMap<K, IndexItem<K>> indexMap = null;
        if (fileHeader.getStoreTxLogRecordId() != 0) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Start to read IndexSnapshot File ....");
            }

            UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
            WritableByteChannel target = Channels.newChannel(os);
            long readLength = fileChannel.size() - fileHeader.getLength();
            if (readLength != 0) {
                fileChannel.transferTo(fileHeader.getLength(), readLength, target);

                UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.toByteArray());

                indexMap = serializer.deserialize(is,
                        new TypeReference<ConcurrentSkipListMap<K, IndexItem<K>>>() {
                        }.getType());
            }

            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Finish read IndexSnapshot File");
            }
        }

        if (indexMap == null) {
            indexMap = new ConcurrentSkipListMap<K, IndexItem<K>>();
        }

        ((MemIndex<K, V>) index).setIndexMap(indexMap);

        StoreTxLogPosition lastTxLog = new StoreTxLogPosition();
        lastTxLog.setRecordId(fileHeader.getStoreTxLogRecordId());

        ((MemIndex<K, V>) index).setLastTxLog(lastTxLog);

    } finally {
        if (fileChannel != null) {
            fileChannel.close();
        }
    }
}
 
Example 19
Source File: QQqpydReader.java    From ThesaurusParser with MIT License 4 votes vote down vote up
/**
*  读取qq词库文件(qpyd),返回一个包含所以词的list
* @param inputPath : qpyd文件的路径
* @return: 包含词库文件中所有词的一个List<String>
* @throws Exception
*/
  public static List<String> readQpydFile(String inputPath) throws Exception
  {
      
      List<String> wordList = new ArrayList<String>();
      // read qpyd into byte array
      ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
      FileChannel fChannel = new RandomAccessFile(inputPath, "r").getChannel();
      fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));
      fChannel.close();

      // qpyd as bytes
      ByteBuffer dataRawBytes = ByteBuffer.wrap(dataOut.toByteArray());
      dataRawBytes.order(ByteOrder.LITTLE_ENDIAN);

      // read info of compressed data
      int startZippedDictAddr = dataRawBytes.getInt(0x38);
      int zippedDictLength = dataRawBytes.limit() - startZippedDictAddr;

      // read zipped qqyd dictionary into byte array
      dataOut.reset();
      Channels.newChannel(new InflaterOutputStream(dataOut)).write(
              ByteBuffer.wrap(dataRawBytes.array(), startZippedDictAddr, zippedDictLength));

      // uncompressed qqyd dictionary as bytes
      ByteBuffer dataUnzippedBytes = ByteBuffer.wrap(dataOut.toByteArray());
      dataUnzippedBytes.order(ByteOrder.LITTLE_ENDIAN);

      // for debugging: save unzipped data to *.unzipped file
      Channels.newChannel(new FileOutputStream(inputPath + ".unzipped")).write(dataUnzippedBytes);
   
      // stores the start address of actual dictionary data
      int unzippedDictStartAddr = -1;
      int idx = 0;
       
      byte[] byteArray = dataUnzippedBytes.array();
      while (unzippedDictStartAddr == -1 || idx < unzippedDictStartAddr) 
      {
          // read word
          int pinyinStartAddr = dataUnzippedBytes.getInt(idx + 0x6);
          int pinyinLength = dataUnzippedBytes.get(idx + 0x0) & 0xff;
          int wordStartAddr = pinyinStartAddr + pinyinLength;
          int wordLength = dataUnzippedBytes.get(idx + 0x1) & 0xff;
          if (unzippedDictStartAddr == -1) 
          {
              unzippedDictStartAddr = pinyinStartAddr;
          }
          String word = new String(Arrays.copyOfRange(byteArray, wordStartAddr, wordStartAddr + wordLength),
                  "UTF-16LE");
          wordList.add(word);
           
          // step up
          idx += 0xa;
      }
    return wordList;  
  }
 
Example 20
Source File: UnionBugs.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Copy a File
 *
 * @param in
 *            to Copy From
 * @param out
 *            to Copy To
 * @throws IOException
 */
private static void copyFile(File in, File out) throws IOException {
    try (FileInputStream inStream = new FileInputStream(in);
            FileOutputStream outStream = new FileOutputStream(out);) {
        FileChannel inChannel = inStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outStream.getChannel());
    }
}