Skip to content

Commit 99e8943

Browse files
committed
Some code-style update (final, this, space, ...)
1 parent 24844ca commit 99e8943

File tree

5 files changed

+91
-92
lines changed

5 files changed

+91
-92
lines changed

src/main/java/fr/i360matt/runnableOverNetwork/DynamicClassLoader.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ public class DynamicClassLoader extends SecureClassLoader {
1616
private final ConcurrentHashMap<String, Object> rawInstances;
1717
private final CodeSource codeSource;
1818

19-
public DynamicClassLoader(boolean isolated) {
19+
public DynamicClassLoader(final boolean isolated) {
2020
super(isolated ? null : DynamicClassLoader.class.getClassLoader());
2121
this.rawClasses = new ConcurrentHashMap<>();
2222
this.rawInstances = new ConcurrentHashMap<>();
2323
this.codeSource = new CodeSource(null, new CodeSigner[0]);
2424
}
2525

26-
public void putClass(String className, byte[] classData) {
26+
public void putClass(final String className, final byte[] classData) {
2727
this.rawClasses.put(className, classData);
2828
}
2929

30-
public boolean hasClass(String className) {
30+
public boolean hasClass(final String className) {
3131
return this.rawClasses.containsKey(className);
3232
}
3333

3434
@Override
35-
public InputStream getResourceAsStream(String name) {
36-
byte[] cl;
35+
public InputStream getResourceAsStream(final String name) {
36+
final byte[] cl;
3737
if (name.endsWith(".class") && (cl = rawClasses.get(
3838
name.substring(0, name.length() - 6).replace('/', '.'))) != null) {
3939
return new ByteArrayInputStream(cl);
@@ -44,11 +44,11 @@ public InputStream getResourceAsStream(String name) {
4444
@Override
4545
protected Class<?> findClass(String name) throws ClassNotFoundException {
4646
name = name.replace('/', '.');
47-
byte[] cl;
47+
final byte[] cl;
4848
if ((cl = rawClasses.get(name.replace('/', '.'))) != null) {
49-
int index = name.lastIndexOf('.');
49+
final int index = name.lastIndexOf('.');
5050
if (index != -1) {
51-
String pkg = name.substring(0, index);
51+
final String pkg = name.substring(0, index);
5252
if (this.getPackage(pkg) == null) {
5353
this.definePackage(pkg, null, null, null, null, null, null, null);
5454
}
@@ -58,11 +58,11 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
5858
throw new ClassNotFoundException(name);
5959
}
6060

61-
public Object loadInstance(String name) throws ReflectiveOperationException {
61+
public Object loadInstance(final String name) {
6262
return this.rawInstances.computeIfAbsent(name, n -> {
6363
try {
6464
return findClass(n).newInstance();
65-
} catch (ReflectiveOperationException e) {
65+
} catch (final ReflectiveOperationException e) {
6666
IOHelper.sneakyThrow(e);
6767
return null;
6868
}

src/main/java/fr/i360matt/runnableOverNetwork/DynamicClient.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import java.io.*;
55
import java.net.Socket;
66
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.TimeUnit;
78

8-
public class DynamicClient implements Closeable,ConnectionConstants {
9+
public class DynamicClient implements Closeable, ConnectionConstants {
910

1011
private final Object writeLock = new Object();
1112
private final Socket socket;
@@ -14,7 +15,7 @@ public class DynamicClient implements Closeable,ConnectionConstants {
1415
private final ConcurrentHashMap<String, Runnable> clRunnable;
1516
private long lastWrite;
1617

17-
public DynamicClient (String host, int port, final String password) throws IOException {
18+
public DynamicClient (final String host, final int port, final String password) throws IOException {
1819
this(new Socket(host, port), password);
1920
}
2021

@@ -31,122 +32,121 @@ public DynamicClient (final Socket socket, final String password) throws IOExcep
3132
this.startKeepAlive();
3233
}
3334

34-
private void startKeepAlive() {
35+
private void startKeepAlive () {
3536
this.lastWrite = System.currentTimeMillis();
3637
new Thread() {
3738
@Override
38-
public void run() {
39+
public void run () {
3940
try {
4041
while (!this.isInterrupted() && !socket.isClosed()) {
41-
long current = System.currentTimeMillis();
42+
final long current = System.currentTimeMillis();
4243
if (lastWrite + 5000 < current) {
4344
sendKeepAlive();
4445
}
45-
Thread.sleep(5000);
46+
TimeUnit.MILLISECONDS.sleep(5000);
4647
}
47-
} catch (InterruptedException | IOException e) {
48+
} catch (final InterruptedException | IOException e) {
4849
e.printStackTrace();
4950
}
5051
}
5152
}.start();
5253
}
5354

54-
public void execRunnable (final Class<? extends Runnable> clazz) throws IOException {
55+
public void execRunnable (final Class<? extends Runnable> clazz) {
5556
this.sendRunnable(clazz).run();
5657
}
5758

58-
public Runnable sendRunnable (final Class<? extends Runnable> clazz) throws IOException {
59+
public Runnable sendRunnable (final Class<? extends Runnable> clazz) {
5960
if (clazz.getClassLoader() == null) {
6061
return () -> {
6162
try {
6263
execClass(clazz.getName());
63-
} catch (IOException e) {
64+
} catch (final IOException e) {
6465
throw new RuntimeException(e);
6566
}
6667
};
6768
}
68-
return clRunnable.computeIfAbsent(clazz.getName(), name -> {
69-
InputStream is = clazz.getClassLoader().getResourceAsStream(clazz.getName().replace('.', '/') + ".class");
69+
return clRunnable.computeIfAbsent (clazz.getName(), name -> {
70+
final InputStream is = clazz.getClassLoader().getResourceAsStream(clazz.getName().replace('.', '/') + ".class");
7071
try {
7172
return sendStream(is, clazz.getName(), false);
72-
} catch (IOException e) {
73+
} catch (final IOException e) {
7374
IOHelper.sneakyThrow(e);
7475
return null;
7576
}
7677
});
7778
}
7879

7980
public Runnable sendFile (final File file,final String className) throws IOException {
80-
try(final FileInputStream fis = new FileInputStream(file)) {
81+
try (final FileInputStream fis = new FileInputStream(file)) {
8182
return this.sendStream(fis, className, false);
8283
}
8384
}
8485

85-
public Runnable sendStream(final InputStream is,String name,boolean execute) throws IOException {
86+
public Runnable sendStream (final InputStream is, final String name, final boolean execute) throws IOException {
8687
return this.sendData(IOHelper.readAllBytes(is), name, execute);
8788
}
8889

89-
public Runnable sendData(byte[] buffer,String name,boolean execute) throws IOException {
90-
synchronized (writeLock) {
91-
dos.writeInt(execute ? ID_SEND_EXEC_DATA : ID_SEND_DATA);
92-
dos.writeInt(buffer.length);
93-
IOHelper.writeString(dos, name);
94-
dos.write(buffer);
95-
dos.flush();
90+
public Runnable sendData (final byte[] buffer, final String name, final boolean execute) throws IOException {
91+
synchronized (this.writeLock) {
92+
this.dos.writeInt(execute ? ID_SEND_EXEC_DATA : ID_SEND_DATA);
93+
this.dos.writeInt(buffer.length);
94+
IOHelper.writeString(this.dos, name);
95+
this.dos.write(buffer);
96+
this.dos.flush();
9697
}
9798
return () -> {
9899
try {
99100
execData(name);
100-
} catch (IOException e) {
101+
} catch (final IOException e) {
101102
throw new RuntimeException(e);
102103
}
103104
};
104105
}
105106

106-
public void execData(String name) throws IOException {
107-
synchronized (writeLock) {
108-
dos.writeInt(ID_EXEC_DATA);
109-
IOHelper.writeString(dos, name);
110-
dos.flush();
107+
public void execData (final String name) throws IOException {
108+
synchronized (this.writeLock) {
109+
this.dos.writeInt(ID_EXEC_DATA);
110+
IOHelper.writeString(this.dos, name);
111+
this.dos.flush();
112+
}
113+
if (dis.readByte() != 0) {
114+
throw new IOException("Data " + name + " was not sent yet");
111115
}
112-
if (dis.readByte() != 0) {
113-
throw new IOException("Data " + name + " was not sent yet");
114-
}
115-
116116
}
117117

118-
public void execClass(String name) throws IOException {
119-
synchronized (writeLock) {
120-
dos.writeInt(ID_EXEC_CLASS);
121-
IOHelper.writeString(dos, name);
122-
dos.flush();
118+
public void execClass (final String name) throws IOException {
119+
synchronized (this.writeLock) {
120+
this.dos.writeInt(ID_EXEC_CLASS);
121+
IOHelper.writeString(this.dos, name);
122+
this.dos.flush();
123123
}
124124
}
125125

126-
public void sendKeepAlive() throws IOException {
127-
synchronized (writeLock) {
128-
dos.writeInt(ID_KEEP_ALIVE);
129-
dos.flush();
126+
public void sendKeepAlive () throws IOException {
127+
synchronized (this.writeLock) {
128+
this.dos.writeInt(ID_KEEP_ALIVE);
129+
this.dos.flush();
130130
}
131131
}
132132

133-
public void remoteCloseServer() throws IOException {
134-
synchronized (writeLock) {
135-
dos.writeInt(ID_CLOSE_SERVER);
136-
dos.flush();
133+
public void remoteCloseServer () throws IOException {
134+
synchronized (this.writeLock) {
135+
this.dos.writeInt(ID_CLOSE_SERVER);
136+
this.dos.flush();
137137
}
138-
if (dis.readByte() != 0) {
138+
if (this.dis.readByte() != 0) {
139139
throw new SecurityException("Client is not allowed to close the server");
140140
}
141141
this.close();
142142
}
143143

144144
@Override
145145
public void close () throws IOException {
146-
if (socket != null) socket.close();
146+
if (this.socket != null) this.socket.close();
147147
}
148148

149-
public boolean isClosed() {
150-
return socket == null || socket.isClosed();
149+
public boolean isClosed () {
150+
return this.socket == null || this.socket.isClosed();
151151
}
152152
}

src/main/java/fr/i360matt/runnableOverNetwork/DynamicServer.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import java.io.*;
44
import java.net.*;
55

6-
public class DynamicServer implements Closeable,ConnectionConstants {
6+
public class DynamicServer implements Closeable, ConnectionConstants {
77

88
private final ServerSocket server;
99
private final String password;
1010
private boolean logErrors = true;
1111
private boolean isolated = false;
1212
private boolean allowRemoteClose = false;
1313

14-
public DynamicServer (int port, final String password) throws IOException {
14+
public DynamicServer (final int port, final String password) throws IOException {
1515
this.server = new ServerSocket(port);
1616
this.password = password;
1717
}
@@ -20,23 +20,22 @@ public DynamicServer (final ServerSocket server, final String password) throws I
2020
this.server = server;
2121
this.password = password;
2222
while (!server.isClosed()) {
23-
Socket clientSock = server.accept();
23+
final Socket clientSock = server.accept();
2424
hasNewClient(clientSock);
2525
}
2626
}
2727

28-
public void listen() throws IOException {
28+
public void listen () throws IOException {
2929
while (!server.isClosed()) {
30-
Socket clientSock;
3130
try {
32-
clientSock = server.accept();
33-
} catch (IOException ioe) {
34-
if ("Socket closed".equals(ioe.getMessage()))
31+
final Socket clientSock = server.accept();
32+
clientSock.setTcpNoDelay(true);
33+
hasNewClient(clientSock);
34+
} catch (final IOException ioe) {
35+
if ("socket closed".equals(ioe.getMessage()))
3536
return; // Happen on close, this is not an error
3637
throw ioe;
3738
}
38-
clientSock.setTcpNoDelay(true);
39-
hasNewClient(clientSock);
4039
}
4140
}
4241

@@ -56,7 +55,7 @@ private void hasNewClient (final Socket clientSock) {
5655
final DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(isolated);
5756

5857
while (!this.server.isClosed() && !clientSock.isClosed()) {
59-
int code = receiver.readInt();
58+
final int code = receiver.readInt();
6059
switch (code) {
6160
case ID_KEEP_ALIVE:
6261
break;
@@ -72,9 +71,9 @@ private void hasNewClient (final Socket clientSock) {
7271
break;
7372
case ID_SEND_EXEC_DATA:
7473
case ID_SEND_DATA: {
75-
int filesize = receiver.readInt(); // Send file size in separate msg
76-
String className = IOHelper.readString(receiver);
77-
byte[] buffer = new byte[filesize];
74+
final int filesize = receiver.readInt(); // Send file size in separate msg
75+
final String className = IOHelper.readString(receiver);
76+
final byte[] buffer = new byte[filesize];
7877
IOHelper.readLarge(receiver, buffer);
7978

8079
dynamicClassLoader.putClass(className, buffer);
@@ -85,7 +84,7 @@ private void hasNewClient (final Socket clientSock) {
8584
}
8685
case ID_EXEC_DATA:
8786
case ID_EXEC_CLASS:
88-
String className = IOHelper.readString(receiver);
87+
final String className = IOHelper.readString(receiver);
8988
if (code == ID_EXEC_DATA) {
9089
if (!dynamicClassLoader.hasClass(className)) {
9190
sender.writeByte(1);
@@ -105,18 +104,18 @@ private void hasNewClient (final Socket clientSock) {
105104
} catch (final Exception ignored) {
106105
try {
107106
clientSock.close();
108-
} catch (IOException ignored1) { }
107+
} catch (final IOException ignored1) { }
109108
}
110109
}).start();
111110
}
112111

113112

114-
private void execRunnable (final DynamicClassLoader classLoader, final String className,boolean forceNew) {
113+
private void execRunnable (final DynamicClassLoader classLoader, final String className, final boolean forceNew) {
115114
try {
116115
final Object obj;
117116
if (forceNew) {
118117
// Load the class from the classloader by name....
119-
final Class<?> loadedClass = classLoader.loadClass(className);
118+
final Class<?> loadedClass = classLoader.loadClass(className);
120119
// Create a new instance...
121120
obj = loadedClass.newInstance();
122121
} else {
@@ -135,15 +134,15 @@ private void execRunnable (final DynamicClassLoader classLoader, final String cl
135134
}
136135
}
137136

138-
public void setLogErrors(boolean logErrors) {
137+
public void setLogErrors (final boolean logErrors) {
139138
this.logErrors = logErrors;
140139
}
141140

142-
public void setIsolated(boolean isolated) {
141+
public void setIsolated (final boolean isolated) {
143142
this.isolated = isolated;
144143
}
145144

146-
public void setAllowRemoteClose(boolean allowRemoteClose) {
145+
public void setAllowRemoteClose (final boolean allowRemoteClose) {
147146
this.allowRemoteClose = allowRemoteClose;
148147
}
149148

@@ -152,7 +151,7 @@ public void close () throws IOException {
152151
this.server.close();
153152
}
154153

155-
public boolean isClosed() {
154+
public boolean isClosed () {
156155
return server == null || server.isClosed();
157156
}
158157
}

0 commit comments

Comments
 (0)