Java - BufferedReader 在没有转义字符的情况下停止

Java - BufferedReader Stalls without an Escape Character

我正在 Java 中构建一个服务器,输入提要给我带来了麻烦 - 它当前的设置不能读取多于一行,并且它不能读取结尾没有“\n”的任何内容。这是当前的输入循环:

BufferedReader input = new BufferedReader(new InputStreamReader(serverConnection.getInputStream()));

    if(input.ready()){

        StringBuilder text = new StringBuilder();
        String line;


        while((line = input.readLine()) != null){

            text.append(line);
            System.out.println(line);

        }

        sentData = text.toString();
        return true;
    }

我当前使用 BufferedReader 的循环无法读取超过一行的传入数据,如果没有换行符,则会抛出超时异常(我将其编程为这样做),并且不会读取数据全部。当然,不能接受只能看到一行数据的监听器,并且在发送的数据格式不正确时停止。

所以我正在寻找一种方法,允许程序读取任意数量的行,并让程序在到达数据流末尾时停止读取(即使没有新行)。

感谢任何帮助。

您正在丢弃所有奇数行。去掉循环里面的readLine()

您可以从 InputStream 直接读入 StringBuilderBufferedReaderreadLine() 将等待 \r\n 字符。这看起来像是在拖延。

int ch;
StringBuilder text = new StringBuilder();
while((ch = serverConnection.getInputStream().read())!= -1) { // -1 will be read at EOS
    text.append((char)ch);
}
sentData = text.toString();
return true;

更新

下面的一段代码是为了演示使用BufferedReaderInputStream读取字节的区别以及在读取操作期间用户可以使用的内容。 BufferedReader 将始终为您提供由换行符或 EOS 终止的行。而 InputStream 将为用户提供可用字节。

不需要 关闭流的情况下,传输的字节有自己的方式来标记 packets/messages 的开始和结束,您将使用 InputStream 读取字节。如果您尝试对这些应用程序使用 BufferedReader,则消息的最后一行将在服务器收到下一行 packet/message 后可用,除非您发送的每一行都带有换行符。

    public static void main(String[] args) throws Exception {
        new Thread(new BufferedReaderServer()).start();
        new Thread(new InputStreamServer()).start();
        final String requestString = "Line#1\nLine#2";

        System.out.println("\nSending to BufferedReaderServer");
        Socket clientSocket1 = new Socket(InetAddress.getLocalHost()
                .getHostAddress(), 8003);
        OutputStream outputStream1 = clientSocket1.getOutputStream();
        outputStream1.write(requestString.getBytes());
        Thread.sleep(6000);
        outputStream1.close();
        clientSocket1.close();

        Thread.sleep(1000);
        System.out.println("\nSending to InputStreamServer");

        Socket clientSocket2 = new Socket(InetAddress.getLocalHost()
                .getHostAddress(), 8004);
        OutputStream outputStream2 = clientSocket2.getOutputStream();
        outputStream2.write(requestString.getBytes());
        Thread.sleep(6000);
        outputStream2.close();
        clientSocket2.close();
    }

    static class BufferedReaderServer implements Runnable {

        public void run() {
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(8003);
                Socket socket = serverSocket.accept();
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
                String s = null;
                System.out.println("BufferedReaderServer read START");
                while ((s = bufferedReader.readLine()) != null) {
                    System.out.println(s);
                }
                System.out.println("BufferedReaderServer read END");
                socket.getInputStream().close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class InputStreamServer implements Runnable {

        public void run() {
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(8004);
                Socket socket = serverSocket.accept();
                System.out.println("InputStreamServer read START");
                int ch;
                while ((ch = socket.getInputStream().read()) != -1) {
                    System.out.print((char) ch);
                }
                System.out.println("\nInputStreamServer read END");
                socket.getInputStream().close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

这里面临的问题可能是由于未关闭客户端套接字。取决于用户的应用程序