[英]DataInputStream's readFully query
我正在使用dataInputStream的readFully消息讀取固定長度的字節數組,如下所示:
byte[] record = new byte[4660004];
in.readFully(record);
這里的問題是,有時讀取這些許多字節需要5秒鍾以上的時間,這等於20000條記錄。 我正在套接字上接收此數據。 客戶端將數據作為4660004字節的字節數組發送。 有沒有一種方法可以更快地接收此數據,因為現在大約需要5分鍾才能處理一百萬條這樣的記錄。
編輯::完整的數據流:
首先,我創建流:
static DataInputStream dIn = null;
dIn = new DataInputStream(connection.getInputStream());
msgType = dIn.readByte();
int msgIntLen = dIn.readInt();
processBatch(msgIntType, msgIntLen, dIn, connector);
.
.
private static void processBatch(int msgIntType, int msgIntLen, DataInputStream in,
Connector connector) throws IOException {
int recordIntLen = in.readInt();
byte[] record = new byte[msgIntLen - 4];
in.readFully(record);
}
如果wudf有幫助,我應該在哪里添加緩沖?
評論開始滾動,因此轉向答案。
通過使用BufferedOutputStream在客戶端上緩沖您的輸出。 確保在寫入數據后調用dlOut.flush()
,以使未發送的字節不會保留在緩沖的輸出流中。
通過使用BufferedInputStream在客戶端上緩沖輸入。
因為您只是發送字節數組,所以可能不需要DataInputStream / DataOuputStream,除非您將它們用於其他目的。 您可能只是使用BufferedInputStream / BufferedOutputStream。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.