如何获得InputStream的大小? [英] How to get the size of an InputStream?

查看:768
本文介绍了如何获得InputStream的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从ProcessBuilder中获取一个InputStream,该源会自动读取stdout流.

I'm having an InputStream from a ProcessBuilder that acutally reads the stdout stream.

问题:如何知道该内存InputStream的大小,以便可以将其写入HttpResponse http标头?

Question: how can I know the size of that inmemory InputStream, so I can write it to a HttpResponse http header?

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

OutputStream out = response.getOutputStream();
int bytes;
while ((bytes = br.read()) != -1) {
    out.write(bytes);
}

//how can I know the size of the inmemory stream/file written?
//response.setContentLength((int) pdfFile.length());

推荐答案

没有输入流的大小之类的东西.考虑一个永远不会退出的程序,或者一个永远不会停止发送的套接字对等体.而且您不需要知道将其写入HttpResponse标头. Content-length是为您自动管理的.

There is no such thing as the size of an input stream. Consider a program which never exits, or a socket peer which never stops sending. And you don't need to know to write it to an HttpResponse header. The Content-length is managed automatically for you.

这篇关于如何获得InputStream的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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