简体   繁体   English

DataInputStream的readFully查询

[英]DataInputStream's readFully query

I am using dataInputStream's readFully message to read a fixed length byte array as: 我正在使用dataInputStream的readFully消息读取固定长度的字节数组,如下所示:

byte[] record = new byte[4660004];

in.readFully(record);

The problem here is that sometimes it takes more than 5 seconds to read these many bytes, which is equal to 20000 records. 这里的问题是,有时读取这些许多字节需要5秒钟以上的时间,这等于20000条记录。 And I am receiving this data on socket. 我正在套接字上接收此数据。 Client is sending data as byte array of 4660004 bytes. 客户端将数据作为4660004字节的字节数组发送。 Is there a way to received this data faster as right now it takes about 5 minutes to 1 million such records. 有没有一种方法可以更快地接收此数据,因为现在大约需要5分钟才能处理一百万条这样的记录。

EDIT:: complete data flow : 编辑::完整的数据流:

first I create the stream : 首先,我创建流:

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);

}   

where should I include the Buffering if that wudf help ? 如果wudf有帮助,我应该在哪里添加缓冲?

Comments are beginning to scroll, so moving to an answer. 评论开始滚动,因此转向答案。

Buffer your output on the client side by using a BufferedOutputStream. 通过使用BufferedOutputStream在客户端上缓冲您的输出。 Make sure to call dlOut.flush() after writing the data, so that unsent bytes don't remain in the buffered output stream. 确保在写入数据后调用dlOut.flush() ,以使未发送的字节不会保留在缓冲的输出流中。

Buffer your input on the client side by using a BufferedInputStream. 通过使用BufferedInputStream在客户端上缓冲输入。

Because you are just sending byte arrays, you probably don't need the DataInputStream/DataOuputStream, unless you are using them for an additional purpose. 因为您只是发送字节数组,所以可能不需要DataInputStream / DataOuputStream,除非您将它们用于其他目的。 You could just be using BufferedInputStream/BufferedOutputStream. 您可能只是使用BufferedInputStream / BufferedOutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM