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. */ /** Manages starting and stopping PortListeners. */
public class EngineServer implements ErrorHandler { public class EngineServer implements ErrorHandler {
private EngineConfig[] configs; private final EngineConfig[] configs;
private PortListener[] portListeners; private final PortListener[] portListeners;
private MainWindow window; private MainWindow window;
private EngineServer(int numEngines) { private EngineServer(int numEngines) {

View File

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