java.nio.channels.FileChannel.transferFrom()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(309)

本文整理了Java中java.nio.channels.FileChannel.transferFrom()方法的一些代码示例,展示了FileChannel.transferFrom()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileChannel.transferFrom()方法的具体详情如下:
包路径:java.nio.channels.FileChannel
类名称:FileChannel
方法名:transferFrom

FileChannel.transferFrom介绍

[英]Reads up to count bytes from src and stores them in this channel's file starting at position. No bytes are transferred if position is larger than the size of this channel's file. Less than count bytes are transferred if there are less bytes remaining in the source channel or if the source channel is non-blocking and has less than count bytes immediately available in its output buffer.

Note that this channel's position is not modified.
[中]从src最多读取个字节,并将它们存储在此通道的文件中,从位置开始。如果位置大于此通道文件的大小,则不传输字节。如果源通道中剩余的字节数较少,或者源通道为非阻塞且其输出缓冲区中立即可用的字节数少于count,则传输少于count字节。
请注意,此通道的位置未修改。

代码示例

代码示例来源:origin: stackoverflow.com

URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

代码示例来源:origin: stackoverflow.com

public static void copyFile( File from, File to ) throws IOException {

  if ( !to.exists() ) { to.createNewFile(); }

  try (
    FileChannel in = new FileInputStream( from ).getChannel();
    FileChannel out = new FileOutputStream( to ).getChannel() ) {

    out.transferFrom( in, 0, in.size() );
  }
}

代码示例来源:origin: lets-blade/blade

public static void copyFile(File source, File dest) throws IOException {
  try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) {
    out.transferFrom(in, 0, in.size());
  }
}

代码示例来源:origin: redisson/redisson

/**
   * Downloads resource to a file, potentially very efficiently.
   */
  public static void downloadFile(String url, File file) throws IOException {
    InputStream inputStream = new URL(url).openStream();
    ReadableByteChannel rbc = Channels.newChannel(inputStream);
    FileOutputStream fos = new FileOutputStream(file);
    fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  }
}

代码示例来源:origin: lets-blade/blade

public static void copyFile(File source, File dest) throws IOException {
  try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) {
    out.transferFrom(in, 0, in.size());
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

public static void writeToFile(byte[] data, File target) throws IOException {
  FileOutputStream fo = null;
  ReadableByteChannel src = null;
  FileChannel out = null;
  try {
    src = Channels.newChannel(new ByteArrayInputStream(data));
    fo = new FileOutputStream(target);
    out = fo.getChannel();
    out.transferFrom(src, 0, data.length);
  } finally {
    if (fo != null) {
      fo.close();
    }
    if (src != null) {
      src.close();
    }
    if (out != null) {
      out.close();
    }
  }
}

代码示例来源:origin: ZHENFENG13/My-Blog

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 {
    assert inputChannel != null;
    inputChannel.close();
    assert outputChannel != null;
    outputChannel.close();
  }
}

代码示例来源:origin: googleapis/google-cloud-java

private File downloadZipFile() throws IOException {
 // Check if we already have a local copy of the emulator and download it if not.
 File zipFile = new File(System.getProperty("java.io.tmpdir"), fileName);
 if (!zipFile.exists() || (md5CheckSum != null && !md5CheckSum.equals(md5(zipFile)))) {
  if (log.isLoggable(Level.FINE)) {
   log.fine("Fetching emulator");
  }
  ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream());
  try (FileOutputStream fos = new FileOutputStream(zipFile)) {
   fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  }
 } else {
  if (log.isLoggable(Level.FINE)) {
   log.fine("Using cached emulator");
  }
 }
 return zipFile;
}

代码示例来源:origin: stackoverflow.com

FileChannel src = new FileInputStream(file1).getChannel();
FileChannel dest = new FileOutputStream(file2).getChannel();
dest.transferFrom(src, 0, src.size());

代码示例来源:origin: apache/incubator-gobblin

private void downloadFile(String fileUrl, Path destination) {
  if (destination.toFile().exists()) {
   Log.info(String.format("Skipping download for %s at %s because destination already exists", fileUrl,
     destination.toString()));
   return;
  }

  try {
   URL archiveUrl = new URL(fileUrl);
   ReadableByteChannel rbc = Channels.newChannel(archiveUrl.openStream());
   FileOutputStream fos = new FileOutputStream(String.valueOf(destination));
   Log.info(String.format("Downloading %s at %s", fileUrl, destination.toString()));
   fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
   Log.info(String.format("Download complete for %s at %s", fileUrl, destination.toString()));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

代码示例来源:origin: stackoverflow.com

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();
    }
  }
}

代码示例来源:origin: apache/ignite

/**
 * @param igniteUrl Url to ignite.
 * @return Ignite file name.
 */
private String downloadIgnite(String igniteUrl) {
  try {
    URL url = new URL(igniteUrl);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    int code = conn.getResponseCode();
    if (code == 200) {
      String fileName = fileName(url.toString());
      String filePath = props.igniteLocalWorkDir() + File.separator + fileName;
      if (new File(filePath).exists())
        return fileName;
      FileOutputStream outFile = new FileOutputStream(filePath);
      outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE);
      outFile.close();
      return fileName;
    }
    else
      throw new RuntimeException("Got unexpected response code. Response code: " + code);
  }
  catch (IOException e) {
    throw new RuntimeException("Failed update ignite.", e);
  }
}

代码示例来源:origin: mcxiaoke/packer-ng-plugin

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();
    }
  }
}

代码示例来源:origin: apache/ignite

/**
 * Downloads ignite by URL if this version wasn't downloaded before.
 *
 * @param url URL to Ignite.
 * @return File name.
 */
private String downloadIgnite(URL url) {
  assert url != null;
  try {
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    int code = conn.getResponseCode();
    if (code == 200) {
      checkDownloadFolder();
      String fileName = fileName(url.toString());
      if (fileExist(fileName))
        return fileName;
      log.log(Level.INFO, "Downloading from {0}", url.toString());
      FileOutputStream outFile = new FileOutputStream(Paths.get(downloadFolder, fileName).toFile());
      outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE);
      outFile.close();
      return fileName;
    }
    else
      throw new RuntimeException("Got unexpected response code. Response code: " + code + " from " + url);
  }
  catch (IOException e) {
    throw new RuntimeException("Failed to download Ignite.", e);
  }
}

代码示例来源:origin: stackoverflow.com

try {
   File sd = Environment.getExternalStorageDirectory();
   File data = Environment.getDataDirectory();
   if (sd.canWrite()) {
     String currentDBPath = "//data//{package name}//databases//{database name}";
     String backupDBPath = "{database name}";
     File currentDB = new File(data, currentDBPath);
     File backupDB = new File(sd, backupDBPath);
     if (currentDB.exists()) {
       FileChannel src = new FileInputStream(currentDB).getChannel();
       FileChannel dst = new FileOutputStream(backupDB).getChannel();
       dst.transferFrom(src, 0, src.size());
       src.close();
       dst.close();
     }
   }
 } catch (Exception e) {
 }

代码示例来源:origin: eirslett/frontend-maven-plugin

@Override
public void download(String downloadUrl, String destination, String userName, String password) throws DownloadException {
  // force tls to 1.2 since github removed weak cryptographic standards
  // https://blog.github.com/2018-02-02-weak-cryptographic-standards-removal-notice/
  System.setProperty("https.protocols", "TLSv1.2");
  String fixedDownloadUrl = downloadUrl;
  try {
    fixedDownloadUrl = FilenameUtils.separatorsToUnix(fixedDownloadUrl);
    URI downloadURI = new URI(fixedDownloadUrl);
    if ("file".equalsIgnoreCase(downloadURI.getScheme())) {
      FileUtils.copyFile(new File(downloadURI), new File(destination));
    }
    else {
      CloseableHttpResponse response = execute(fixedDownloadUrl, userName, password);
      int statusCode = response.getStatusLine().getStatusCode();
      if(statusCode != 200){
        throw new DownloadException("Got error code "+ statusCode +" from the server.");
      }
      new File(FilenameUtils.getFullPathNoEndSeparator(destination)).mkdirs();
      ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent());
      FileOutputStream fos = new FileOutputStream(destination);
      fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
      fos.close();
    }
  } catch (IOException | URISyntaxException e) {
    throw new DownloadException("Could not download " + fixedDownloadUrl, e);
  }
}

代码示例来源:origin: commons-io/commons-io

try (FileInputStream fis = new FileInputStream(srcFile);
   FileChannel input = fis.getChannel();
   FileOutputStream fos = new FileOutputStream(destFile);
   FileChannel output = fos.getChannel()) {
  final long size = input.size(); // TODO See IO-386
  long pos = 0;
  long count = 0;
    final long remain = size - pos;
    count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain;
    final long bytesCopied = output.transferFrom(input, pos, count);
    if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size
      break; // ensure we don't loop forever

代码示例来源:origin: apache/ignite

FileOutputStream outFile = new FileOutputStream(props.igniteLocalWorkDir() + File.separator
  + fileName(redirectUrl));
outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE);

代码示例来源:origin: redwarp/9-Patch-Resizer

public static void copy(File inputFile, File outputFile) throws IOException {
  if (inputFile != null && outputFile != null) {
   FileChannel sourceChannel = null;
   FileChannel destChannel = null;
   try {
    sourceChannel = new FileInputStream(inputFile).getChannel();
    destChannel = new FileOutputStream(outputFile).getChannel();
    destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
   } finally {
    if (sourceChannel != null) {
     sourceChannel.close();
    }
    if (destChannel != null) {
     destChannel.close();
    }
   }
  }
 }
}

代码示例来源:origin: stackoverflow.com

URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

相关文章