- java.lang.Object
-
- dev.bitbite.networking.IOHandler
-
public class IOHandler extends java.lang.ObjectThis IOHandler class combines an input- and an output stream object into a single class. Incoming data of the inputstream will be propagated to the onRead consumer method passed as an argument to the constructor.
You can write to the outputstream viawrite(byte[])
The process of reading data must be initiated usingread(). This is necessary to make it possible to read data of multiple IOHandlers within a single thread without any IOHandler blocking the process.
-
-
Constructor Summary
Constructors Constructor Description IOHandler(java.io.InputStream inputStream, java.io.OutputStream outputStream, java.util.function.Consumer<byte[]> onRead)Initializes the IOHandler with the given Streams and read-Callback method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the streamsprotected voidflushRead()Calls the readCallback with the bytes currently contained in the bufferstatic bytegetEndOfMessageByte()Returns the byte that is currently set to mark the end of a message.static intgetMaxReadSize()Returns the maximum count of bytes that are being read at one time.longgetTimeSinceLastRead()Returns the time that has passed since the last read in nanosecondsvoidread()Reads data from the inputstream if there is data available.protected voidreadNBytes(int amount)Tries to read a set amount of bytes from the stream.protected voidreadToNBytes(int total)Reads bytes until the buffer contains the given amount of bytesvoidregisterListener(IOHandlerListener listener)Registers a ClientListenervoidremoveListener(IOHandlerListener listener)Removes ClientListener from the listenersstatic voidsetEndOfMessageByte(byte endOfMessageByte)Sets the byte that marks the end of a message.static voidsetMaxReadSize(int maxReadSize)Sets the maximum count of bytes that are being read at one time.voidwrite(byte[] data)Writes data to the OutputStream and flushes it.
-
-
-
Constructor Detail
-
IOHandler
public IOHandler(java.io.InputStream inputStream, java.io.OutputStream outputStream, java.util.function.Consumer<byte[]> onRead)Initializes the IOHandler with the given Streams and read-Callback method.- Parameters:
inputStream- , the inputStream to read the data fromoutputStream- , the outputStream to write toonRead- , the read Callback method which is called when a message is received- Throws:
java.lang.IllegalArgumentException- if at least one of the supplied arguments is null
-
-
Method Detail
-
close
public void close()
Closes the streams
-
read
public void read()
Reads data from the inputstream if there is data available.
-
readNBytes
protected void readNBytes(int amount)
Tries to read a set amount of bytes from the stream. If an end-of-message byte is detected the read bytes are passed to the read callback. If an end of stream is detected the IOHandler is closed. If more bytes are available than the amount that should be read they are left in the stream until the next call- Parameters:
amount- of bytes to read
-
readToNBytes
protected void readToNBytes(int total)
Reads bytes until the buffer contains the given amount of bytes- Parameters:
total- number of bytes to be read to the buffer until it gets flushed
-
flushRead
protected void flushRead()
Calls the readCallback with the bytes currently contained in the buffer
-
write
public void write(byte[] data)
Writes data to the OutputStream and flushes it.- Parameters:
data- to be send- See Also:
PrintWriter
-
registerListener
public void registerListener(IOHandlerListener listener)
Registers a ClientListener- Parameters:
listener- to add
-
removeListener
public void removeListener(IOHandlerListener listener)
Removes ClientListener from the listeners- Parameters:
listener- to remove
-
getTimeSinceLastRead
public long getTimeSinceLastRead()
Returns the time that has passed since the last read in nanoseconds- Returns:
- the time that has passed since the last read in nanoseconds
-
getEndOfMessageByte
public static byte getEndOfMessageByte()
Returns the byte that is currently set to mark the end of a message. Its default value is set to 0x0A, which is the LINE FEED byte.- Returns:
- the byte that is currently set to mark the end of a message
-
setEndOfMessageByte
public static void setEndOfMessageByte(byte endOfMessageByte)
Sets the byte that marks the end of a message. Its default value is set to 0x0A, which is the LINE FEED byte.- Parameters:
endOfMessageByte- the byte that should represent the end of a message
-
getMaxReadSize
public static int getMaxReadSize()
Returns the maximum count of bytes that are being read at one time. This is done so that a long message does not block other connections from reading for too long.- Returns:
- the maximum count of bytes that are being read at one time.
-
setMaxReadSize
public static void setMaxReadSize(int maxReadSize)
Sets the maximum count of bytes that are being read at one time. This is done so that a long message does not block other connections from reading for too long. Higher values may result in longer answer times.- Parameters:
maxReadSize- the maximum count of bytes that are being read at one time.
-
-