0

我正在使用 dataInputStream 的 readFully 消息来读取固定长度的字节数组:

byte[] record = new byte[4660004];

in.readFully(record);

这里的问题是,有时读取这么多字节需要超过 5 秒,相当于 20000 条记录。我正在套接字上接收这些数据。客户端正在以 4660004 字节的字节数组形式发送数据。有没有办法更快地接收这些数据,因为现在大约需要 5 分钟到 100 万条这样的记录。

编辑:: 完整的数据流:

首先我创建流:

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 有帮助,我应该在哪里包含缓冲?

4

1 回答 1

0

评论开始滚动,所以转向答案。

使用 BufferedOutputStream 在客户端缓冲您的输出。确保dlOut.flush()在写入数据后调用,以便未发送的字节不会保留在缓冲的输出流中。

使用 BufferedInputStream 在客户端缓冲您的输入。

因为您只是发送字节数组,所以您可能不需要 DataInputStream/DataOuputStream,除非您将它们用于其他目的。您可能只是使用 BufferedInputStream/BufferedOutputStream。

于 2011-05-06T21:48:25.730 回答