复制InputStream,如果大小超过限制则中止操作 [英] Copy InputStream, abort operation if size exceeds limit

查看:483
本文介绍了复制InputStream,如果大小超过限制则中止操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将InputStream复制到File,如果InputStream的大小大于1MB,则中止复制。
在Java7中,我编写了如下代码:

I tried to copy an InputStream to a File, and abort the copy if the size of InputStream is greater than 1MB. In Java7, I wrote code as below:

public void copy(InputStream input, Path target) {
    OutputStream out = Files.newOutputStream(target,
            StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
    boolean isExceed = false;
    try {
        long nread = 0L;
        byte[] buf = new byte[BUFFER_SIZE];
        int n;
        while ((n = input.read(buf)) > 0) {
            out.write(buf, 0, n);
            nread += n;
            if (nread > 1024 * 1024) {// Exceed 1 MB
                isExceed = true;
                break;
            }
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        out.close();
        if (isExceed) {// Abort the copy
            Files.deleteIfExists(target);
            throw new IllegalArgumentException();
        }
    }}




  • 第一个问题:它有更好的解决方案吗?

  • 第二个问题:我的另一个解决方案 - 在复制操作之前,我计算了这个InputStream的大小。所以我将InputStream复制到 ByteArrayOutputStream 然后得到 size()。但问题是InputStream可能不是 markSupported(),因此无法在复制文件操作中重复使用InputStream。

    • First question: is there any better solution for it?
    • Second question: my other solution - Before the copy operation, I calculate the size of this InputStream. So I copy the InputStream to ByteArrayOutputStream then get size(). But the problem is InputStream may not markSupported(), so the InputStream cannot be reused in a copy file operation.
    • 推荐答案


      第一个问题:有什么更好的解决方案吗?

      First question: is there any better solution for it?

      不是。当然,并没有明显更好。

      Not really. Certainly, not significantly better.


      第二个问题:我的另一个解决方案 - 在复制操作之前,我计算InputStream的大小。所以我将InputStream复制到ByteArrayOutputStream然后获取size()。但问题是InputStream可能没有markSupported(),所以InputStream不能在复制文件操作中重复使用。

      Second question: my other solution - Before the copy operation, I calculate size of InputStream. So I copy the InputStream to ByteArrayOutputStream then get size(). But the problem is InputStream may not markSupported(), so the InputStream cannot be reused in copy file operation.

      暂且不说以上是一个陈述而不是一个问题...

      如果你已经将字节复制到 ByteArrayOutputStream ,然后您可以从 baos.toByteArray()返回的字节数组中创建 ByteArrayInputStream 。所以你不需要标记/重置原始流。

      If you have copied the bytes to the ByteArrayOutputStream, you can then create a ByteArrayInputStream from the byte array returned by baos.toByteArray(). So you don't need to mark / reset the original stream.

      然而,这是一种非常难看的实现方式。尤其是因为您正在阅读并缓冲整个输入流。

      However, that is a pretty ugly way of implementing this. Not least because you are reading and buffering the entire input stream anyway.

      这篇关于复制InputStream,如果大小超过限制则中止操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆