Java InputStream. Read (byte [], int, int) method, how to block until the exact number of bytes is read
•
Java
I am writing a simple client / server network application to send and receive fixed size messages through TCP sockets
So far, I have been using the Socket class getInputStream () and getOutputStream () method to get the stream, and then call the InputStream class read (byte [] B, int off, int len) to read 60 bytes (this is the size of the message).
Later, I read the Javadoc of this method:
I wonder if there is any Java "out of the box" solution to block until len bytes are read and wait forever if necessary
I can obviously create a simple loop, but I think I'm reshaping the wheel Can you suggest a clean Java aware solution?
Solution
Use datainputstream readFully. This is Javadoc
InputStream in = ... DataInputStream dis = new DataInputStream( in ); byte[] array = ... dis.readFully( array );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码