I am making a multithreaded server-client app where every client has its own dedicated thread. Server and client communicate using a specific message format that ends with "END\r\n". For this purpose, I thought of using Scanner class and its useDellimiter method. Here is the code
private static String getMessage(InputStream input) throws IOException {
Scanner n = new Scanner(input);
n.useDelimiter("END\r\n");
while(!n.hasNext()) {}
return n.next();
}
The problem I ran into is that n.next() doesn't block if the message isn't available instead it throws an exception. I fixed this with basic polling with this while loop, it works, but that seems like bad practice. Can I make n.next() block? Is there a better way of achieving what I want without using the Scanner class?