iteye_10600 2009-01-15 15:21
浏览 1007
已采纳

关于获取 InputStream 输入流的 字节数

现在要获取 InputStream 流的字节数,
所以用 available()获取字节长度 ,结果出错 报IO异常

下面为代码部分:
调用下面方法时,会传入一个有内容的 输入流

[code="java"]
public final void writeInputStream(InputStream in,OutputStream out){
try
{

int len = in.available(); //这一行报错!!!

       byte[] data=new byte[1024*1024*10];
       int bytesRead=0;
       while((bytesRead=in.read(data,0,1024*1024*10))!=-1)
       {
         out.write(data, 0, bytesRead);
       }
       in.close();
       out.close();           

    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
   }

[/code]
[b]问题补充:[/b]
为什么
[code="java"]
while((bytesRead=in.read(data,0,1024*1024*10))!=-1)

[/code]
这个方法可以获取流的字节数,
而我直接用 in.available(); 获取字节数却出错,并且有 IOException?

  • 写回答

3条回答 默认 最新

  • bohemia 2009-01-15 19:38
    关注

    [quote]available
    public int available()
    throws IOException
    Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread. [/quote]

    请参照JavaAPI;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler