get size of InputStream in Java

Deadly 提交于 2019-12-24 17:33:52

问题


I am trying to encrypt an InputStream which I am getting from FileItem.getInputStream(); and I want to know the encrypted stream's length.

If it would be a simple FileInputStream, I could have tried File#length() or FileInputStream#getChennal().size() though not sure even those would have given me exactly what I want, but, in this case what I have is an InputStream (the encrypted one) and I want to find length of the same, I tried searching on Internet but there I could not find any efficient solution to that.

Please help


回答1:


You have to read the whole stream for that. Streams are probably not even complete when you start reading from them, so the size may not be known at that time.




回答2:


You can output whats in InputStream and get the length

ObjA = new ObjA;

while( more message to read ){
ObjA = inputStreamObj.read();
}

System.out.println(ObjA.length());


来源:https://stackoverflow.com/questions/22508428/get-size-of-inputstream-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!