如何在不关闭的情况下 'clean' InputStream?
How to 'clean' InputStream without closing it?
客户端代码片段。基本上它从标准输入读取并向服务器发送消息。
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 1200)) {
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.US_ASCII);
Scanner scanner = new Scanner(System.in);
for (String msg = scanner.nextLine(); !msg.equals("end"); msg = scanner.nextLine()) {
writer.write(msg + "\n");
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
服务器代码片段。打印来自流的消息。
public void run() {
try (InputStreamReader reader = new InputStreamReader(this.socket.getInputStream(), StandardCharsets
.US_ASCII)) {
StringBuilder builder = new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char) c);
if ((char) c == '\n')
System.out.print(builder);
}
} catch (IOException e) {
e.printStackTrace();
}
}
来自客户端的输入:
Text1
Text2
服务器输出:
Text1
Text1
Text2
我面临的问题是服务器不仅打印收到的消息,还打印之前的所有消息。
问题:如何在不关闭的情况下重置'clean' InputStream
。如果这是不可能的,首选解决方案是什么?
您不需要 'clean' 流——您只需要在每一行之后重置缓冲区。使用 StringBuilder.setLength
:
尝试如下操作
if (c == '\n') {
System.out.print(builder.toString());
builder.setLength(0);
}
另一方面,我强烈建议不要 手动阅读这样的行。考虑使用 Scanner
like you do in the client code or alternatively a BufferedReader
.
try (final BufferedReader reader
= new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.US_ASCII))) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
}
} catch (final IOException ex) {
ex.printStackTrace();
}
客户端代码片段。基本上它从标准输入读取并向服务器发送消息。
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 1200)) {
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.US_ASCII);
Scanner scanner = new Scanner(System.in);
for (String msg = scanner.nextLine(); !msg.equals("end"); msg = scanner.nextLine()) {
writer.write(msg + "\n");
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
服务器代码片段。打印来自流的消息。
public void run() {
try (InputStreamReader reader = new InputStreamReader(this.socket.getInputStream(), StandardCharsets
.US_ASCII)) {
StringBuilder builder = new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char) c);
if ((char) c == '\n')
System.out.print(builder);
}
} catch (IOException e) {
e.printStackTrace();
}
}
来自客户端的输入:
Text1
Text2
服务器输出:
Text1
Text1
Text2
我面临的问题是服务器不仅打印收到的消息,还打印之前的所有消息。
问题:如何在不关闭的情况下重置'clean' InputStream
。如果这是不可能的,首选解决方案是什么?
您不需要 'clean' 流——您只需要在每一行之后重置缓冲区。使用 StringBuilder.setLength
:
if (c == '\n') {
System.out.print(builder.toString());
builder.setLength(0);
}
另一方面,我强烈建议不要 手动阅读这样的行。考虑使用 Scanner
like you do in the client code or alternatively a BufferedReader
.
try (final BufferedReader reader
= new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.US_ASCII))) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
}
} catch (final IOException ex) {
ex.printStackTrace();
}