| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | class ServerThread extends Thread { |
| 23 | |
| 24 | private final Socket socket; |
| 25 | private final int count; |
| 26 | |
| 27 | public ServerThread(Socket socket, int count) { |
| 28 | this.socket = socket; |
| 29 | this.count = count; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void run() { |
| 34 | var file = new File("hello.html"); |
| 35 | try ( |
| 36 | // get the input stream |
| 37 | var in = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| 38 | // get character output stream to client (for headers) |
| 39 | var out = new PrintWriter(socket.getOutputStream()); |
| 40 | // get binary output stream to client (for requested data) |
| 41 | var dataOut = new BufferedOutputStream(socket.getOutputStream()); |
| 42 | var fileIn = new FileInputStream(file) |
| 43 | ) { |
| 44 | // add 2 second delay to every 10th request |
| 45 | if (count % 10 == 0) { |
| 46 | System.out.println("Adding delay. Count: " + count); |
| 47 | Thread.sleep(2000); |
| 48 | } |
| 49 | |
| 50 | // read the request first to avoid connection reset errors |
| 51 | while (true) { |
| 52 | String requestLine = in.readLine(); |
| 53 | if (requestLine == null || requestLine.length() == 0) { |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // read the HTML file |
| 59 | var fileLength = (int) file.length(); |
| 60 | var fileData = new byte[fileLength]; |
| 61 | fileIn.read(fileData); |
| 62 | |
| 63 | var contentMimeType = "text/html"; |
| 64 | // send HTTP Headers |
nothing calls this directly
no outgoing calls
no test coverage detected