diff --git a/java/libraries/net/src/processing/net/Client.java b/java/libraries/net/src/processing/net/Client.java index c3a0f5449..11287c59e 100644 --- a/java/libraries/net/src/processing/net/Client.java +++ b/java/libraries/net/src/processing/net/Client.java @@ -406,6 +406,36 @@ public class Client implements Runnable { } + /** + *

Advanced

+ * Return a byte array of anything that's in the serial buffer + * up to the specified maximum number of bytes. + * Not particularly memory/speed efficient, because it creates + * a byte array on each read, but it's easier to use than + * readBytes(byte b[]) (see below). + * + * @param max the maximum number of bytes to read + */ + public byte[] readBytes(int max) { + if (bufferIndex == bufferLast) return null; + + synchronized (buffer) { + int length = bufferLast - bufferIndex; + if (length > max) length = max; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + + return outgoing; + } + } + + /** *

Advanced

* Grab whatever is in the serial buffer, and stuff it into a