Fix Android Studio warnings in EngineServer

This commit is contained in:
Peter Osterlund 2022-12-11 10:18:16 +01:00
parent c0963c567c
commit af1d725f27
3 changed files with 34 additions and 40 deletions

View File

@ -29,8 +29,8 @@ import java.util.Properties;
/** Manages starting and stopping PortListeners. */
public class EngineServer implements ErrorHandler {
private EngineConfig[] configs;
private PortListener[] portListeners;
private final EngineConfig[] configs;
private final PortListener[] portListeners;
private MainWindow window;
private EngineServer(int numEngines) {

View File

@ -48,8 +48,8 @@ public class MainWindow {
private JTextField[] filename;
private JTextField[] arguments;
private EngineServer server;
private EngineConfig[] configs;
private final EngineServer server;
private final EngineConfig[] configs;
public MainWindow(EngineServer server, EngineConfig[] configs) {
this.server = server;

View File

@ -30,11 +30,11 @@ import java.util.ArrayList;
/** Listens to a TCP port and connects an engine process to a TCP socket. */
public class PortListener {
private EngineConfig config;
private ErrorHandler errorHandler;
class PortListener {
private final EngineConfig config;
private final ErrorHandler errorHandler;
private Thread thread;
private final Thread thread;
private ServerSocket serverSocket;
private Socket clientSocket;
private Process proc;
@ -44,20 +44,17 @@ public class PortListener {
this.config = config;
this.errorHandler = errorHandler;
thread = new Thread() {
@Override
public void run() {
try {
mainLoop();
} catch (InterruptedException ex) {
if (!shutDownFlag)
reportError("Background thread interrupted", ex);
} catch (IOException ex) {
if (!shutDownFlag)
reportError("IO error in background thread", ex);
}
thread = new Thread(() -> {
try {
mainLoop();
} catch (InterruptedException ex) {
if (!shutDownFlag)
reportError("Background thread interrupted", ex);
} catch (IOException ex) {
if (!shutDownFlag)
reportError("IO error in background thread", ex);
}
};
});
thread.start();
}
@ -149,27 +146,24 @@ public class PortListener {
}
private Thread forwardIO(InputStream is, OutputStream os) {
Thread t = new Thread() {
@Override
public void run() {
try {
byte[] buffer = new byte[4096];
while (true) {
int len = is.read(buffer);
if (len < 0)
break;
os.write(buffer, 0, len);
os.flush();
}
} catch (IOException ignore) {
Thread t = new Thread(() -> {
try {
byte[] buffer = new byte[4096];
while (true) {
int len = is.read(buffer);
if (len < 0)
break;
os.write(buffer, 0, len);
os.flush();
}
close(is);
close(os);
Process p = proc;
if (p != null)
p.destroyForcibly();
} catch (IOException ignore) {
}
};
close(is);
close(os);
Process p = proc;
if (p != null)
p.destroyForcibly();
});
t.start();
return t;
}