[英]Slow read performance using DataInputStream when it comes to reading small length of bytes
Currently I'm writing a video stream receiver on Java TCP Socket. 目前,我正在Java TCP Socket上编写视频流接收器。 I have managed to display the stream just fine but I'm concerned on the performance of my socket reading as I need to reduce the latency.
我设法将流显示得很好,但是我担心套接字读取的性能,因为我需要减少延迟。
The first 8 bytes of each video payload representing the length of the video payload itself. 每个视频有效负载的前8个字节代表视频有效负载本身的长度。 So I have to get the length of the video payload first, and then read the video payload until the specified length has been reached before I pass it to the decoder.
因此,我必须先获取视频有效载荷的长度,然后读取视频有效载荷,直到达到指定的长度,然后再将其传递给解码器。 The code is something like this:
代码是这样的:
boolean isRunning = true;
boolean isReadingHeader = true;
byte[] headerBuff = new byte[8];
byte[] videoBuff;
Socket socket;
/* socket setup goes here */
DataInputStream dis = new DataInputStream(socket.getInputStream());
while(isRunning)
{
if(isReadingHeader) /* reading the header to get the length */
{
long start = System.currentTimeMillis();
dis.readFully(headerBuff);
long end = System.currentTimeMillis() - start;
System.out.println("Read Header : " + end + " ms");
int len = ....
videoBuff = new byte[len];
isReadingHeader = false;
}
else /* reading the video payload*/
{
long start = System.currentTimeMillis();
dis.readFully(videoBuff);
long end = System.currentTimeMillis() - start;
System.out.println("Read Payload: " + end + " ms");
/* Passing to Decoder goes here */
isReadingHeader = true;
}
}
Since the outputs are endless, I put the first 20 outputs as below (the outputs after that are consistant): 由于输出是无止境的,因此我将前20个输出如下所示(之后的输出是一致的):
Read Header : 6 ms
Read Payload : 0 ms
Read Header : 0 ms
Read Payload : 0 ms
Read Header : 0 ms
Read Payload : 0 ms
Read Header : 200 ms
Read Payload : 1 ms
Read Header : 142 ms
Read Payload : 0 ms
Read Header : 138 ms
Read Payload : 0 ms
Read Header : 135 ms
Read Payload : 0 ms
Read Header : 146 ms
Read Payload : 0 ms
Read Header : 136 ms
Read Payload : 0 ms
Read Header : 147 ms
Read Payload : 0 ms
As you can see, reading 8 bytes into headerBuff is taking between 100ms to 200ms while reading the video payload which its length can be 30k++ is done in 0 ms. 如您所见,在headerBuff中读取8个字节需要100毫秒至200毫秒,而在0毫秒内可以读取长度为30k ++的视频有效载荷。 I'm wondering why its taking too long to read 8 bytes of data.
我想知道为什么它花太长时间读取8个字节的数据。 I have tried readByte() in for loop and also the read() methods but the execution time is just the same.
我在for循环和read()方法中都尝试过readByte(),但是执行时间是相同的。 FYI, i tested this on localhost and this latency issue is not happening on my .NET stream receiver (means network is not the reason).
仅供参考,我在localhost上进行了测试,并且.NET流接收器上未发生此延迟问题(意味着网络不是原因)。
Thanks! 谢谢!
Instead of 代替
DataInputStream dis = new DataInputStream(socket.getInputStream());
Try using BufferedInputStream 尝试使用BufferedInputStream
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.