Vikings

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;
                        }
原来要从一个InputStream里面读一个byte[],
我是这么写的:
byte[] bytes = new byte[is.avlilabel]
is.read[bytes];
原来如果is过大的话,
没有buffer,有可能读不完整。

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

就没这问题了。

posted on 2006-02-17 19:25 Vikings 阅读(865) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: