org.xsocket.stream
Class StreamUtils
java.lang.Object
org.xsocket.stream.StreamUtils
public final class StreamUtils
- extends java.lang.Object
utility class for stream based communication
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SERVER_TRHREAD_PREFIX
public static final java.lang.String SERVER_TRHREAD_PREFIX
- See Also:
- Constant Field Values
validateSufficientDatasizeByIntLengthField
public static int validateSufficientDatasizeByIntLengthField(INonBlockingConnection connection)
throws java.io.IOException,
java.nio.BufferUnderflowException
- validate, based on a leading int length field, that enough data (getNumberOfAvailableBytes() >= length) is available. If not,
an BufferUnderflowException will been thrown. The 4 bytes of the length field will be ignore by
getting the available data size. Example:
//client
connection.setAutoflush(false); // avoid immediate write
...
connection.markWritePosition(); // mark current position
connection.write((int) 0); // write "emtpy" length field
// write and count written size
int written = connection.write(CMD_PUT);
written += ...
connection.resetToWriteMark(); // return to length field position
connection.write(written); // and update it
connection.flush(); // flush (marker will be removed implicit)
...
// server
class MyHandler implements IDataHandler {
...
public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
int length = StreamUtils.validateSufficientDatasizeByIntLengthField(connection);
// enough data (BufferUnderflowException hasn`t been thrown)
byte cmd = connection.readByte();
...
}
}
- Parameters:
connection
- the connection
- Returns:
- the length
- Throws:
java.io.IOException
- if an exception occurs
java.nio.BufferUnderflowException
- if not enough data is available
start
public static void start(IMultithreadedServer server)
- starts the given server within a dedicated thread. This method blocks
until the server is open.
- Parameters:
server
- the server to start