1 | package com.renomad.minum.web; | |
2 | ||
3 | import com.renomad.minum.logging.ILogger; | |
4 | ||
5 | import java.io.*; | |
6 | import java.net.Socket; | |
7 | import java.net.SocketAddress; | |
8 | import java.nio.charset.Charset; | |
9 | ||
10 | /** | |
11 | * This wraps Sockets to make them more particular to our use case | |
12 | */ | |
13 | final class SocketWrapper implements ISocketWrapper { | |
14 | ||
15 | private final Socket socket; | |
16 | private final String hostName; | |
17 | private final BufferedOutputStream writer; | |
18 | private final ILogger logger; | |
19 | private final IServer server; | |
20 | private final BufferedInputStream bufferedInputStream; | |
21 | ||
22 | /** | |
23 | * Constructor | |
24 | * @param socket a socket we intend to wrap with methods applicable to our use cases | |
25 | * @param logger not much more to say on this param | |
26 | * @param timeoutMillis we'll configure the socket to timeout after this many milliseconds. | |
27 | */ | |
28 | SocketWrapper(Socket socket, IServer server, ILogger logger, int timeoutMillis, String hostName) throws IOException { | |
29 | this.socket = socket; | |
30 | this.hostName = hostName; | |
31 | logger.logTrace(() -> String.format("Setting timeout of %d milliseconds on socket %s", timeoutMillis, socket)); | |
32 |
1
1. <init> : removed call to java/net/Socket::setSoTimeout → KILLED |
this.socket.setSoTimeout(timeoutMillis); |
33 | this.bufferedInputStream = new BufferedInputStream(socket.getInputStream()); | |
34 | writer = new BufferedOutputStream(socket.getOutputStream()); | |
35 | this.logger = logger; | |
36 | this.server = server; | |
37 | } | |
38 | ||
39 | @Override | |
40 | public void send(String msg) throws IOException { | |
41 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
writer.write(msg.getBytes(Charset.defaultCharset())); |
42 | } | |
43 | ||
44 | @Override | |
45 | public void send(byte[] bodyContents) throws IOException { | |
46 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
writer.write(bodyContents); |
47 | } | |
48 | ||
49 | @Override | |
50 | public void send(byte[] bodyContents, int off, int len) throws IOException { | |
51 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
writer.write(bodyContents, off, len); |
52 | } | |
53 | ||
54 | @Override | |
55 | public void send(int b) throws IOException { | |
56 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
writer.write(b); |
57 | } | |
58 | ||
59 | @Override | |
60 | public void sendHttpLine(String msg) throws IOException { | |
61 | logger.logTrace(() -> String.format("%s sending: \"%s\"", this, msg)); | |
62 |
1
1. sendHttpLine : removed call to com/renomad/minum/web/SocketWrapper::send → KILLED |
send(msg + WebEngine.HTTP_CRLF); |
63 | } | |
64 | ||
65 | @Override | |
66 | public int getLocalPort() { | |
67 |
1
1. getLocalPort : replaced int return with 0 for com/renomad/minum/web/SocketWrapper::getLocalPort → KILLED |
return socket.getLocalPort(); |
68 | } | |
69 | ||
70 | @Override | |
71 | public SocketAddress getRemoteAddrWithPort() { | |
72 |
1
1. getRemoteAddrWithPort : replaced return value with null for com/renomad/minum/web/SocketWrapper::getRemoteAddrWithPort → KILLED |
return socket.getRemoteSocketAddress(); |
73 | } | |
74 | ||
75 | @Override | |
76 | public String getRemoteAddr() { | |
77 |
1
1. getRemoteAddr : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getRemoteAddr → KILLED |
return socket.getInetAddress().getHostAddress(); |
78 | } | |
79 | ||
80 | @Override | |
81 | public HttpServerType getServerType() { | |
82 |
1
1. getServerType : replaced return value with null for com/renomad/minum/web/SocketWrapper::getServerType → KILLED |
return server.getServerType(); |
83 | } | |
84 | ||
85 | @Override | |
86 | public void close() throws IOException { | |
87 | logger.logTrace(() -> "close called on " + this); | |
88 |
1
1. close : removed call to java/net/Socket::close → KILLED |
socket.close(); |
89 |
2
1. close : negated conditional → KILLED 2. close : removed call to com/renomad/minum/web/IServer::removeMyRecord → KILLED |
if (server != null) server.removeMyRecord(this); |
90 | } | |
91 | ||
92 | @Override | |
93 | public InputStream getInputStream() { | |
94 |
1
1. getInputStream : replaced return value with null for com/renomad/minum/web/SocketWrapper::getInputStream → KILLED |
return bufferedInputStream; |
95 | } | |
96 | ||
97 | /** | |
98 | * Note that since we are indicating just the remote address | |
99 | * as the unique value, in cases like tests where we are operating as | |
100 | * sometimes server or client, you might see the server as the remote. | |
101 | */ | |
102 | @Override | |
103 | public String toString() { | |
104 |
1
1. toString : replaced return value with "" for com/renomad/minum/web/SocketWrapper::toString → KILLED |
return "(SocketWrapper for remote address: " + this.getRemoteAddrWithPort().toString() + ")"; |
105 | } | |
106 | ||
107 | @Override | |
108 | public String getHostName() { | |
109 |
1
1. getHostName : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getHostName → KILLED |
return hostName; |
110 | } | |
111 | ||
112 | @Override | |
113 | public void flush() throws IOException { | |
114 |
1
1. flush : removed call to java/io/BufferedOutputStream::flush → KILLED |
this.writer.flush(); |
115 | } | |
116 | } | |
Mutations | ||
32 |
1.1 |
|
41 |
1.1 |
|
46 |
1.1 |
|
51 |
1.1 |
|
56 |
1.1 |
|
62 |
1.1 |
|
67 |
1.1 |
|
72 |
1.1 |
|
77 |
1.1 |
|
82 |
1.1 |
|
88 |
1.1 |
|
89 |
1.1 2.2 |
|
94 |
1.1 |
|
104 |
1.1 |
|
109 |
1.1 |
|
114 |
1.1 |