I'm using a DataInputStream to read characters/data from a socket.
I want to use .readUnsignedShort(); and have it throw an exception if there isn't 2 bytes to read. Should I subclass the DataInputStream and override the methods adding the exceptions, or is there an easier way?
If you want something quick and dirty, try inputStream.available().
if (stream.available() < 2) {
// throw it
}
If you want true non blocking reads and callbacks when data is available, I think Pablo's answer is better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With