简体   繁体   中英

Why a new InputStreamReader won't read the remaining characters in the console?

So I have a very simple server written in Java:

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("Server Socket created, waiting for client...");
        Socket accept = serverSocket.accept();
        InputStreamReader inputStreamReader = new InputStreamReader(accept.getInputStream());
        int read;
        System.out.println("Client connected, waiting for input");
        while ((read = inputStreamReader.read()) != -1) {
            System.out.print((char) read);
        }
    }
}

And here is a code that I use to connect to it:

public class SimpleClient {

    public static void main(String[] args) throws Exception {

        Socket socket = new Socket("localhost",8888);
        OutputStreamWriter outputStream = new OutputStreamWriter(socket.getOutputStream());

        InputStreamReader inputStreamReader;
        char[] chars = new char[5];

        while (true) {
            System.out.println("Say something: ");
            inputStreamReader = new InputStreamReader(System.in);
            inputStreamReader.read(chars);
            int x = 0;
            for (int i=0;i<5;i++) {
                if(chars[i]!='\u0000') {
                    x++;
                }
            }
            outputStream.write(chars,0,x);
            outputStream.flush();
            chars = new char[5];
        }

    }

}

Now when I type something like this in the terminal of the Client:

123456789

I will see in the terminal of the Server:

Server Socket created, waiting for client...
Client connected, waiting for input
12345

However, when I change client as follows:

public class SimpleClient {

    public static void main(String[] args) throws Exception {

        Socket socket = new Socket("localhost",8888);
        OutputStreamWriter outputStream = new OutputStreamWriter(socket.getOutputStream());

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        char[] chars = new char[5];

        while (true) {
            System.out.println("Say something: ");
            inputStreamReader.read(chars);
            int x = 0;
            for (int i=0;i<5;i++) {
                if(chars[i]!='\u0000') {
                    x++;
                }
            }
            outputStream.write(chars,0,x);
            outputStream.flush();
            chars = new char[5];
        }

    }

}

then for the same input, I will see:

Server Socket created, waiting for client...
Client connected, waiting for input
123456789

My question is, System.out is a static variable which is already open and connected to the terminal in this case. Why is the information in the terminal lost when a new InputStreamReader object is created? Same terminal is passed to the object, isn't it?

Why is the information in the terminal lost when a new InputStreamReader object is created?

When you call read() on the InputStreamReader , it's allowed to (and often will) read more data from the stream than you've actually requested, and store the rest in a buffer, to satisfy later read calls. I suspect the whole of the line of text has actually been read by the first InputStreamReader , so when you construct a second InputStreamReader for the same stream, there's nothing left for it to read, and you'd have to type in more text to get it to do anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM