Java InputStream讀取數據問題
原理講解
1. 關於InputStream.read()
在從數據流里讀取數據時,為圖簡單,經常用InputStream.read()方法。這個方法是從流里每次只讀取讀取一個字節,效率會非常低。 更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次讀取多個字節。
2. 關於InputStream類的available()方法
要一次讀取多個字節時,經常用到InputStream.available()方法,這個方法可以在讀寫操作前先得知數據流里有多少個字節可以讀取。需要注意的是,如果這個方法用在從本
地文件讀取數據時,一般不會遇到問題,但如果是用於網絡操作,就經常會遇到一些麻煩。比如,Socket通訊時,對方明明發來了1000個字節,但是自己的程序調用available()方法卻只得到900,或者100,甚至是0,感覺有點莫名其妙,怎么也找不到原因。其實,這是因為網絡通訊往往是間斷性的,一串字節往往分幾批進行發送。本地程序調用available()方法有時得到0,這可能是對方還沒有響應,也可能是對方已經響應了,但是數據還沒有送達本地。對方發送了1000個字節給你,也許分成3批到達,這你就要調用3次available()方法才能將數據總數全部得到。
如果這樣寫代碼:
<span style="font-family:SimHei;font-size:16px;"> int count = in.available(); byte[] b = new byte[count]; in.read(b);</span>
在進行網絡操作時往往出錯,因為你調用available()方法時,對發發送的數據可能還沒有到達,你得到的count是0。
需要改成這樣:
int count = 0; while (count == 0) { //count = in.available(); count=response.getEntity().getContentLength();//(HttpResponse response) } byte[] b = new byte[count]; in.read(b);
3. 關於InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)這兩個方法都是用來從流里讀取多個字節的,有經驗的程序員就會發現,這兩個方法經常 讀取不到自己想要讀取的個數的字節。比如第一個方法,程序員往往希望程序能讀取到b.length個字節,而實際情況是,系統往往讀取不了這么多。仔細閱讀Java的API說明就發現了,這個方法 並不保證能讀取這么多個字節,它只能保證最多讀取這么多個字節(最少1個)。因此,如果要讓程序讀取count個字節,最好用以下代碼:
byte[] bytes = new byte[count]; int readCount = 0; // 已經成功讀取的字節的個數 while (readCount < count) { readCount += in.read(bytes, readCount, count - readCount); }
用這段代碼可以保證讀取count個字節,除非中途遇到IO異常或者到了數據流的結尾(EOFException)
代碼分享
下面分享我自己寫的測試代碼片段,供大家參考:
/** * <b>獲取指定的URL返回的數據信息</b> * @param <font color="#efac10"><a href="http://www.baidu.com">_url:指定的URL</a></font> * @return * @throws ClientProtocolException * @throws IOException */ public String getReponse(String _url) throws ClientProtocolException, IOException { String readContent=null; DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(SinaJsonTest.SinaUrl); System.out.println("0.Send the URL to Sina Sever...."); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("1.Get Response Status: " + response.getStatusLine()); if (entity != null) { System.out.println(" Get ResponseContentEncoding():"+entity.getContentEncoding()); System.out.println(" Content Length():"+entity.getContentLength()); //getResponse InputStream in=entity.getContent(); int count = 0; while (count == 0) { count = Integer.parseInt(""+entity.getContentLength());//in.available(); } byte[] bytes = new byte[count]; int readCount = 0; // 已經成功讀取的字節的個數 while (readCount <= count) { if(readCount == count)break; readCount += in.read(bytes, readCount, count - readCount); } //轉換成字符串 readContent= new String(bytes, 0, readCount, "UTF-8"); // convert to string using bytes System.out.println("2.Get Response Content():\n"+readContent); } return readContent; }
