English 中文(简体)
D. 与BufferInputStream的下载
原标题:Downloading with BufferInputStream not working properly

The following Code don t work todown a file (btw clen is file s length):

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

我不禁要问,为什么,即使第二部法典部分迅速运作。 在该说明中,是否有特别理由使用直截了当的缓冲(因为这种缓冲似乎较快,而BufferedInputStream已经使用自己的缓冲)。

最佳回答

这里应如何做到这一点。

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

        }
    }
问题回答

我试图对你的法典作必要的最低限度修改,使之发挥作用。 斜体是提供双向版本流拷贝的良好工作。

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) {
    }
}
  • You were ignoring the value of pos when you were writing out the buffer to the output stream.
  • You also need to re-check the value of pos because it may have just read the end of the file. You don t increment the total_pos in that case (although you should probably report that you are 100% complete)
  • Be sure to handle your resources correctly with close()s in the appropriate places.

-edit- The general reason for using an array as a buffer is so that the output stream can do as much work as it can with a larger set of data.

写给青少年可能不会造成很多延误,但可能是一个网络的袖珍,正被写给或一些其他缓慢装置。 As the JavaDoc states

产出Stream的书写方法要求书面说明每一 by子的一条理由。 鼓励下级单位超越这一方法,提供更有效的执行。

在使用布置式投入/投放机时使用该软件的好处可能很小。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签