2

以下代码无法下载文件(顺便说一句 clen 是文件的长度):

    int pos = 0, total_pos = 0;
    byte[] buffer = new byte[BUFFER_SIZE];
            while (pos != -1) {
                pos = in.read(buffer, 0, BUFFER_SIZE);
                total_pos += pos;
                out.write(buffer);
                setProgress((int) (total_pos * 100 / clen));
            }

...但这很好用:

    int buf;
    while ((buf = in.read()) != -1)
        out.write(buf);

我想知道为什么,即使第二个代码段运行得很快。关于这一点,是否有任何特殊原因使用 byte[] 缓冲区(因为它似乎并不快,而且 BufferedInputStream 已经使用了自己的缓冲区......?)

4

2 回答 2

2

这是应该怎么做的。

public static void copyStream(InputStream is, OutputStream os)
    {
        byte[] buff = new byte[4096];
        int count;
        try {
            while((count = is.read(buff)) > 0)
                os.write(buff, 0, count);

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
于 2011-10-11T03:52:51.977 回答
0

我已尝试对您的代码进行必要的最小更改以使其正常工作。st0le 在提供更简洁的流复制版本方面做得很好。

public class Test {
    private static final String FORMAT = "UTF-8";
    private static final int BUFFER_SIZE = 10; // for demonstration purposes.
    public static void main(String[] args) throws Exception {
        String string = "This is a test of the public broadcast system";
        int clen = string.length();
        ByteArrayInputStream in = new ByteArrayInputStream(string.getBytes(FORMAT));
        OutputStream out = System.out;
        int pos = 0, total_pos = 0;
        byte[] buffer = new byte[BUFFER_SIZE];
        while (pos != -1) {
            pos = in.read(buffer, 0, BUFFER_SIZE);
            if (pos > 0) {
                total_pos += pos;
                out.write(buffer, 0, pos);
                setProgress((int) (total_pos * 100 / clen));
            }
        }
    }
    private static void setProgress(int i) {
    }
}
  • 当您将缓冲区写入输出流时,您忽略了pos的值。
  • 您还需要重新检查pos的值,因为它可能刚刚读取了文件的末尾。在这种情况下,您不会增加total_pos(尽管您可能应该报告您已 100% 完成)
  • 确保在适当的位置使用 close() 正确处理您的资源。

-edit- 使用数组作为缓冲区的一般原因是,输出流可以使用更大的数据集完成尽可能多的工作。

写入控制台可能没有太多延迟,但可能是正在写入的网络套接字或其他一些慢速设备。正如 JavaDoc 所述

OutputStream 的 write 方法在要写出的每个字节上调用一个参数的 write 方法。鼓励子类重写此方法并提供更有效的实现。

The benefit of using it when using a Buffered Input/Output Stream are probably minimal.

于 2011-10-11T04:04:19.323 回答