byte[] b),read(byte[] b, int off, int len)。其中read()方法是一次讀取一個位元組,鬼都知道效率是非常低的。是以最好是使用後面兩個方法。
例如以下代碼:
- /**
- * 讀取流
- *
- * @param inStream
- * @return 位元組數組
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws
- new
- byte[] buffer = new byte[1024];
- int len = -1;
- while ((len = inStream.read(buffer)) != -1) {
- 0, len);
- }
- outSteam.close();
- inStream.close();
- return
- }
我們來測試一下:
- public static void
- try
- new File("C:\\ceshi.txt");
- new
- byte[] filebt = readStream(fin);
- System.out.println(filebt.length);
- catch
- e.printStackTrace();
- }
- }
背景會列印這個文本的位元組大小。看起來,這個是沒有問題的。
關于InputStream類的available()方法
這個方法的意思是傳回此輸入流下一個方法調用可以不受阻塞地從此輸入流讀取(或跳過)的估計位元組數。為什麼需要這個方法?因為在一些網絡應用中,資料流并不是一次性就能傳遞的,如果我們還是像上面那樣去将這個流轉換,會出問題的。我們來做一個例子,這是一個Socket程式設計的簡單例子,具體Socket内容我會在後面文章中解釋的。
首先編寫兩個類,一個使用者初始化Socket服務,并且處理每個請求都有新的線程去處理,代碼如下:
- package
- import
- public class
- public static void
- try
- // 啟動監聽端口 8001
- new ServerSocket(8001);
- boolean bRunning = true;
- while
- // 接收請求
- Socket s = ss.accept();
- // 将請求指定一個線程去執行
- new Thread(new
- }
- catch
- e.printStackTrace();
- }
- }
- }
那麼處理類我們也來看一下:
- package
- import
- import
- import
- public class DstServiceImpl implements
- null;
- public
- this.socket = s;
- }
- public void
- try
- InputStream ips = socket.getInputStream();
- OutputStream ops = socket.getOutputStream();
- while (true) {
- byte[] bt = StreamTool.readStream(ips);
- new
- "主機收到資訊:"
- "你好,主機已經收到資訊!";
- ops.write(restr.getBytes());
- ops.flush();
- }
- catch
- e.printStackTrace();
- }
- }
- }
至于工具類,我就直接給代碼了:
- package
- import
- public class
- public static void
- try
- new File("C:\\ceshi.txt");
- new
- byte[] filebt = readStream(fin);
- System.out.println(filebt.length);
- catch
- e.printStackTrace();
- }
- }
- /**
- * @功能 讀取流
- * @param inStream
- * @return 位元組數組
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws
- new
- byte[] buffer = new byte[1024];
- int len = -1;
- while ((len = inStream.read(buffer)) != -1) {
- 0, len);
- }
- outSteam.close();
- inStream.close();
- return
- }
- }
你可以直接運作這個類,會看到流被轉換的效果。
我們來寫一個Socket用戶端測試一下:
- package
- import
- import
- import
- public class
- public static void
- try
- new Socket("127.0.0.1", 8001);
- // 開啟保持活動狀态的套接字
- true);
- // 設定讀取逾時時間
- 30 * 1000);
- OutputStream ops = socket.getOutputStream();
- "你好,我是崔素強!";
- ops.write(mess.getBytes());
- InputStream ips = socket.getInputStream();
- byte[] rebyte = StreamTool.readStream(ips);
- new
- "收到主機消息:"
- socket.close();
- catch
- e.printStackTrace();
- }
- }
- }
先運作DstService,然後運作用戶端,看效果。會發現,控制台沒有任何輸出。經過調試發現,因為請求死在了
- while ((len = inStream.read(buffer)) != -1) {
這行代碼上面。這就是在網絡應用中會造成的後果。那麼如何解決呢?有的人給出了如下代碼:
- int
- byte[] b = new byte[count];
- in.read(b);
可是在進行網絡操作時往往出錯,因為你調用available()方法時,對發發送的資料可能還沒有到達,你得到的count是0。需要做如下修改,是我們的讀取流方法改成如下:
- /**
- * @功能 讀取流
- * @param inStream
- * @return 位元組數組
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws
- int count = 0;
- while (count == 0) {
- count = inStream.available();
- }
- byte[] b = new byte[count];
- inStream.read(b);
- return
- }
下面你在運作,會看到服務端和用戶端都收到了消息。
關于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)這兩個方法都是用來從流裡讀取多個位元組的,有經驗的程式員就會發現,這兩個方法經常 讀取不到自己想要讀取的個數的位元組。比如第一個方法,程式員往往希望程式能讀取到b.length個位元組,而實際情況是,系統往往讀取不了這麼多。仔細閱讀Java的API說明就發現了,這個方法 并不保證能讀取這麼多個位元組,它隻能保證最多讀取這麼多個位元組(最少1個)。是以,如果要讓程式讀取count個位元組,最好用以下代碼:
- int count = 100;
- byte[] b = new byte[count];
- int readCount = 0; // 已經成功讀取的位元組的個數
- while
- readCount += inStream.read(b, readCount, count - readCount);
- }