Hello, such a problem is that the loop that should end when the method of the PipedInputStream object returns -1 , but it does not return. How to be? Where am I wrong? Tell me please.
public void rw1() { StringBuilder sb = new StringBuilder(); try (PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out) ) { out.write("message".getBytes("UTF8")); out.flush(); int data = in.read(); while ((data) != -1) { sb.append((char) data); System.out.println(data); data = in.read(); } System.out.println(new String(sb)); } catch (IOException e) { e.printStackTrace(); } } The execution flow simply stops and does not go any further.
I understand that he is waiting for more data, I could use the available() method, but I still want to understand the logic with this -1 which read() should return. What is the matter?