Java 强制read()方法读取最少个字符

Java 强制read()方法读取最少个字符,java,telnet,bufferedreader,expect,Java,Telnet,Bufferedreader,Expect,我正在尝试使用expect实现对路由器进行telnet 我的套接字通信代码如下所示: server = "my-server-ip-domain-here"; socket = new Socket(); socket.connect(new InetSocketAddress(server, 23), 10000);//Will wait for 10 seconds socket.setKeepA

我正在尝试使用expect实现对路由器进行telnet

我的套接字通信代码如下所示:

            server = "my-server-ip-domain-here";
            socket = new Socket();
            socket.connect(new InetSocketAddress(server, 23), 10000);//Will wait for 10 seconds
            socket.setKeepAlive(true);
            socket.setSoTimeout(10000);
            expectBuffer = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(), true);
  public static void send(String cmd) {
        pw.print(cmd + "\r");
        pw.flush();
    }
  public static String expect(String expectString) {
        try {

            int c = 0;


            char[] buf = new char[4096];

            //Here c will return the no. of chars read 
            while ((c = br.read(buf)) != -1) {


                String tmp = "";

                //converting that char array to String
                for (int i = 0; i < c; i++) {
                    //Printing that character
                    System.out.print(buf[i]);

                    tmp += buf[i];
                }



                expectBuffer.append(tmp).append(NEW_LINE);


                if (expectBuffer.toString().contains(expectString)) {
                    break;
                }
            }
            String expBuff = expectBuffer.toString();
            expectBuffer.setLength(0);
//            System.out.println(expBuff);
            return expBuff;
        } catch (Exception e) {
            System.out.println(e);
            return "";
        }

    }
我的send实现如下:

            server = "my-server-ip-domain-here";
            socket = new Socket();
            socket.connect(new InetSocketAddress(server, 23), 10000);//Will wait for 10 seconds
            socket.setKeepAlive(true);
            socket.setSoTimeout(10000);
            expectBuffer = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(), true);
  public static void send(String cmd) {
        pw.print(cmd + "\r");
        pw.flush();
    }
  public static String expect(String expectString) {
        try {

            int c = 0;


            char[] buf = new char[4096];

            //Here c will return the no. of chars read 
            while ((c = br.read(buf)) != -1) {


                String tmp = "";

                //converting that char array to String
                for (int i = 0; i < c; i++) {
                    //Printing that character
                    System.out.print(buf[i]);

                    tmp += buf[i];
                }



                expectBuffer.append(tmp).append(NEW_LINE);


                if (expectBuffer.toString().contains(expectString)) {
                    break;
                }
            }
            String expBuff = expectBuffer.toString();
            expectBuffer.setLength(0);
//            System.out.println(expBuff);
            return expBuff;
        } catch (Exception e) {
            System.out.println(e);
            return "";
        }

    }
我的预期实现如下:

            server = "my-server-ip-domain-here";
            socket = new Socket();
            socket.connect(new InetSocketAddress(server, 23), 10000);//Will wait for 10 seconds
            socket.setKeepAlive(true);
            socket.setSoTimeout(10000);
            expectBuffer = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(), true);
  public static void send(String cmd) {
        pw.print(cmd + "\r");
        pw.flush();
    }
  public static String expect(String expectString) {
        try {

            int c = 0;


            char[] buf = new char[4096];

            //Here c will return the no. of chars read 
            while ((c = br.read(buf)) != -1) {


                String tmp = "";

                //converting that char array to String
                for (int i = 0; i < c; i++) {
                    //Printing that character
                    System.out.print(buf[i]);

                    tmp += buf[i];
                }



                expectBuffer.append(tmp).append(NEW_LINE);


                if (expectBuffer.toString().contains(expectString)) {
                    break;
                }
            }
            String expBuff = expectBuffer.toString();
            expectBuffer.setLength(0);
//            System.out.println(expBuff);
            return expBuff;
        } catch (Exception e) {
            System.out.println(e);
            return "";
        }

    }
根据我发送的命令,它将显示一些输出。无论我发送什么,都会被阅读,不幸的是,它是以一种单独的方式打印出来的

如下图所示

com
mandABC

<command output here>

command-
efg

<command output here>

commandHij

<command output here>
com
曼达巴
命令-
efg
司令部
正如我在上面指出的,只有我发送的命令以单独的方式打印

我检查了当时读取的字符数,发现它在2-10之间

这就是为什么它会以这种方式印刷

是否有限制读取至少100个字符的方法

提前感谢。

  • 如果要等到读完整行文本,请尝试
    bf.readLine()
    (您需要确保每个命令都以'\n'终止)
  • 如果要确保在继续处理之前已读取一定数量的字符(例如100),请使用循环:

    char buffer[128];
    for (int charsRead = 0; charsRead < 100; ) {
        charsRead += bf.read(buffer, charsRead, (100 - charsRead));
    }
    
    charsRead
    作为偏移量大小传递意味着每个读取的字符块将存储在先前读取的字符块之后。将
    (100-charsRead)
    作为
    max\u to\u read
    传递会限制您读取的总字符数

资料来源:API ref