clean up warnings and formatting in the Client code

This commit is contained in:
Ben Fry
2022-01-10 19:08:14 -05:00
parent aab8d8fa50
commit 9802ccbd88

View File

@@ -43,6 +43,7 @@ import java.nio.charset.StandardCharsets;
* @usage Application
* @see_external LIB_net/clientEvent
*/
@SuppressWarnings("unused")
public class Client implements Runnable {
protected static final int MAX_BUFFER_SIZE = 1 << 27; // 128 MB
@@ -61,7 +62,7 @@ public class Client implements Runnable {
final Object bufferLock = new Object[0];
byte buffer[] = new byte[32768];
byte[] buffer = new byte[32768];
int bufferIndex;
int bufferLast;
@@ -96,14 +97,14 @@ public class Client implements Runnable {
clientEventMethod =
parent.getClass().getMethod("clientEvent", Client.class);
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
// no such method, or an error... which is fine, just ignore
}
// do the same for disconnectEvent(Client c);
try {
disconnectEventMethod =
parent.getClass().getMethod("disconnectEvent", Client.class);
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
// no such method, or an error... which is fine, just ignore
}
} catch (IOException e) {
@@ -115,7 +116,6 @@ public class Client implements Runnable {
/**
* @param socket any object of type Socket
* @throws IOException
*/
public Client(PApplet parent, Socket socket) throws IOException {
this.parent = parent;
@@ -134,14 +134,14 @@ public class Client implements Runnable {
clientEventMethod =
parent.getClass().getMethod("clientEvent", Client.class);
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
// no such method, or an error... which is fine, just ignore
}
// do the same for disconnectEvent(Client c);
try {
disconnectEventMethod =
parent.getClass().getMethod("disconnectEvent", Client.class);
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
// no such method, or an error... which is fine, just ignore
}
}
@@ -368,7 +368,7 @@ public class Client implements Runnable {
*
* Returns a number between 0 and 255 for the next byte that's waiting in
* the buffer. Returns -1 if there is no byte, although this should be
* avoided by first cheacking <b>available()</b> to see if any data is available.
* avoided by first checking <b>available()</b> to see if any data is available.
*
* @webref client
* @usage application
@@ -430,7 +430,7 @@ public class Client implements Runnable {
if (bufferIndex == bufferLast) return null;
int length = bufferLast - bufferIndex;
byte outgoing[] = new byte[length];
byte[] outgoing = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex = 0; // rewind
@@ -456,7 +456,7 @@ public class Client implements Runnable {
int length = bufferLast - bufferIndex;
if (length > max) length = max;
byte outgoing[] = new byte[length];
byte[] outgoing = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex += length;
@@ -482,7 +482,7 @@ public class Client implements Runnable {
*
* @param bytebuffer passed in byte array to be altered
*/
public int readBytes(byte bytebuffer[]) {
public int readBytes(byte[] bytebuffer) {
synchronized (bufferLock) {
if (bufferIndex == bufferLast) return 0;
@@ -534,7 +534,7 @@ public class Client implements Runnable {
if (found == -1) return null;
int length = found - bufferIndex + 1;
byte outgoing[] = new byte[length];
byte[] outgoing = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex += length;
@@ -560,7 +560,7 @@ public class Client implements Runnable {
*
* @param byteBuffer passed in byte array to be altered
*/
public int readBytesUntil(int interesting, byte byteBuffer[]) {
public int readBytesUntil(int interesting, byte[] byteBuffer) {
byte what = (byte)interesting;
synchronized (bufferLock) {
@@ -596,7 +596,7 @@ public class Client implements Runnable {
/**
*
*
* Returns the all the data from the buffer as a <b>String</b>.
*
* In 4.0 beta 3, changed to using UTF-8 as the encoding,
@@ -607,8 +607,10 @@ public class Client implements Runnable {
* @webBrief Returns the buffer as a <b>String</b>
*/
public String readString() {
byte b[] = readBytes();
if (b == null) return null;
byte[] b = readBytes();
if (b == null) {
return null;
}
return new String(b, StandardCharsets.UTF_8);
}
@@ -628,15 +630,17 @@ public class Client implements Runnable {
* @param interesting character designated to mark the end of the data
*/
public String readStringUntil(int interesting) {
byte b[] = readBytesUntil(interesting);
if (b == null) return null;
byte[] b = readBytesUntil(interesting);
if (b == null) {
return null;
}
return new String(b, StandardCharsets.UTF_8);
}
/**
*
* Writes data to a server specified when constructing the client, or writes
*
* Writes data to a server specified when constructing the client, or writes
* data to the specific client obtained from the Server <b>available()</b>
* method.
*
@@ -650,26 +654,19 @@ public class Client implements Runnable {
output.write(data & 0xff); // for good measure do the &
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
//errorMessage("write", e);
//e.printStackTrace();
//dispose();
//disconnect(e);
} catch (Exception e) { // null pointer or serial port dead
e.printStackTrace();
stop();
}
}
public void write(byte data[]) {
public void write(byte[] data) {
try {
output.write(data);
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
//errorMessage("write", e);
//e.printStackTrace();
//disconnect(e);
} catch (Exception e) { // null pointer or serial port dead
e.printStackTrace();
stop();
}
@@ -683,27 +680,4 @@ public class Client implements Runnable {
public void write(String data) {
write(data.getBytes(StandardCharsets.UTF_8));
}
/**
* Handle disconnect due to an Exception being thrown.
*/
/*
protected void disconnect(Exception e) {
dispose();
if (e != null) {
e.printStackTrace();
}
}
*/
/**
* General error reporting, all corralled here just in case
* I think of something slightly more intelligent to do.
*/
//public void errorMessage(String where, Exception e) {
//parent.die("Error inside Client." + where + "()", e);
//e.printStackTrace(System.err);
//}
}