【问题标题】:How to use NIO to write InputStream to File?如何使用 NIO 将 InputStream 写入 File?
【发布时间】:2013-05-11 00:13:20
【问题描述】:

我正在使用以下方式将InputStream 写入File

private void writeToFile(InputStream stream) throws IOException {
    String filePath = "C:\\Test.jpg";
    FileChannel outChannel = new FileOutputStream(filePath).getChannel();       
    ReadableByteChannel inChannel = Channels.newChannel(stream);
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    
    while(true) {
        if(inChannel.read(buffer) == -1) {
            break;
        }
        
        buffer.flip();
        outChannel.write(buffer);
        buffer.clear();
    }
    
    inChannel.close();
    outChannel.close();
}

我想知道这是否是使用 NIO 的正确方法。我看过一个方法FileChannel.transferFrom,它需要三个参数:

  1. ReadableByteChannel src
  2. 多头
  3. 长计数

就我而言,我只有src,我没有positioncount,有什么方法可以使用这种方法来创建文件吗?

对于 Image 还有没有更好的方法来仅从 InputStream 和 NIO 创建图像?

任何信息都会对我非常有用。这里有类似的问题,在 SO 中,但我找不到适合我的情况的任何特定解决方案。

【问题讨论】:

  • 为什么要这么复杂?您可以在一行中执行相同的操作:Files.copy(stream, new File("C:\\Test.jpg").toPath());

标签: java image file inputstream nio


【解决方案1】:

我会使用 Files.copy

Files.copy(is, Paths.get(filePath));

至于你的版本

  1. ByteBuffer.allocateDirect 更快 - Java 将尽最大努力直接在其上执行本机 I/O 操作。

  2. 关闭是不可靠的,如果第一次失败,第二次将永远不会执行。请改用 try-with-resources,Channel 也是 AutoCloseable

【讨论】:

  • transferFrom()transferTo() 必须在循环中调用。不能保证他们会转移请求的计数。这就是他们返回计数的原因。
  • 使用 Files.copy(fileInputStream, filePath, StandardCopyOption.REPLACE_EXISTING);如果文件已经存在。
【解决方案2】:

不,这是不正确的。您冒着丢失数据的风险。规范的 NIO 复制循环如下:

while (in.read(buffer) >= 0 || buffer.position() > 0)
{
  buffer.flip();
  out.write(buffer);
  buffer.compact();
}

注意更改的循环条件,它负责在 EOS 上刷新输出,以及使用 compact() 而不是 clear(), 来处理短写入的可能性。

同样,规范的transferTo()/transferFrom() 循环如下:

long offset = 0;
long quantum = 1024*1024; // or however much you want to transfer at a time
long count;
while ((count = out.transferFrom(in, offset, quantum)) > 0)
{
    offset += count;
}

它必须在循环中调用,因为它不能保证传输整个量程。

【讨论】:

  • 如果 transferFrom 返回 0 并不意味着所有字节都被实际传输。为了 100% 正确,我们需要提前知道 InputStream 的预期计数并循环,直到我们将它们全部传输。你同意吗?
  • 是的,我愿意。奇怪的是,这些 API 没有正确指示 EOS。
猜你喜欢
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 2013-12-09
  • 1970-01-01
  • 2017-10-20
  • 2021-01-15
  • 2011-08-17
相关资源
最近更新 更多