1 package net.pterodactylus.fcp.fake;
3 import java.io.BufferedReader;
4 import java.io.Closeable;
5 import java.io.EOFException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.OutputStream;
10 import java.io.OutputStreamWriter;
11 import java.io.Writer;
12 import java.net.Socket;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.hamcrest.Matcher;
21 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23 class TextSocket implements Closeable {
25 private final Socket socket;
26 private final InputStream socketInput;
27 private final OutputStream socketOutput;
28 private final BufferedReader inputReader;
29 private final Writer outputWriter;
31 TextSocket(Socket socket) throws IOException {
33 this.socketInput = socket.getInputStream();
34 this.socketOutput = socket.getOutputStream();
35 this.inputReader = new BufferedReader(new InputStreamReader(socketInput, "UTF-8"));
36 this.outputWriter = new OutputStreamWriter(socketOutput, "UTF-8");
39 public String readLine() throws IOException {
40 return inputReader.readLine();
43 public void writeLine(String line) throws IOException {
44 outputWriter.write(line + "\n");
48 public List<String> collectUntil(Matcher<String> lineMatcher) throws IOException {
49 List<String> collectedLines = new ArrayList<>();
51 String line = readLine();
53 throw new EOFException();
55 collectedLines.add(line);
56 if (lineMatcher.matches(line)) {
60 return collectedLines;
64 public void close() throws IOException {