Vikings

          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          JAVA BLOG

          JAVA論壇

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          InputStream , buffer概念

          try {
                                      String host 
          = new URL(link).getHost();
                                      String url 
          = "http://"+ host + "/favicon.ico";
                                      is 
          = new URL(url).openConnection().getInputStream();
                                      
          byte[] buffer = new byte[5000];
                                      bos 
          = new ByteArrayOutputStream();
                                      
          int bytesRead = 0;
                                      
          while ((bytesRead = is.read(buffer)) > 0{
                                          bos.write(buffer, 
          0, bytesRead);
                                      }

                                      bos.flush();
                                  }
          catch (IOException e){
                                      canDownloader 
          = false;
                                  }
          原來(lái)要從一個(gè)InputStream里面讀一個(gè)byte[],
          我是這么寫的:
          byte[] bytes = new byte[is.avlilabel]
          is.read[bytes];
          原來(lái)如果is過(guò)大的話,
          沒(méi)有buffer,有可能讀不完整。

              while ((bytesRead = is.read(buffer)) > 0{
                                          bos.write(buffer, 
          0, bytesRead);
                  }

          就沒(méi)這問(wèn)題了。

          posted on 2006-02-17 19:25 Vikings 閱讀(884) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 昭通市| 红河县| 和政县| 新蔡县| 九龙城区| 雅安市| 巴马| 夏邑县| 东乌| 大安市| 休宁县| 平阴县| 德昌县| 黄陵县| 洛川县| 阆中市| 枝江市| 通化市| 松原市| 洛扎县| 象州县| 信丰县| 邯郸市| 苍溪县| 聂荣县| 绥中县| 万山特区| 永康市| 大荔县| 郓城县| 泸西县| 黑山县| 洛阳市| 吐鲁番市| 望城县| 洪雅县| 荃湾区| 岫岩| 定襄县| 金门县| 防城港市|