Skip to content

Commit c9eab5c

Browse files
committed
Net-client: use final field for lock
1 parent 5a27dee commit c9eab5c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class Client implements Runnable {
5757
public InputStream input;
5858
public OutputStream output;
5959

60+
final Object bufferLock = new Object[0];
61+
6062
byte buffer[] = new byte[32768];
6163
int bufferIndex;
6264
int bufferLast;
@@ -230,7 +232,7 @@ public void run() {
230232
return;
231233
}
232234

233-
synchronized (buffer) {
235+
synchronized (bufferLock) {
234236
// todo: at some point buffer should stop increasing in size,
235237
// otherwise it could use up all the memory.
236238
if (bufferLast == buffer.length) {
@@ -341,7 +343,7 @@ public void clear() {
341343
public int read() {
342344
if (bufferIndex == bufferLast) return -1;
343345

344-
synchronized (buffer) {
346+
synchronized (bufferLock) {
345347
int outgoing = buffer[bufferIndex++] & 0xff;
346348
if (bufferIndex == bufferLast) { // rewind
347349
bufferIndex = 0;
@@ -394,7 +396,7 @@ public char readChar() {
394396
public byte[] readBytes() {
395397
if (bufferIndex == bufferLast) return null;
396398

397-
synchronized (buffer) {
399+
synchronized (bufferLock) {
398400
int length = bufferLast - bufferIndex;
399401
byte outgoing[] = new byte[length];
400402
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
@@ -419,7 +421,7 @@ public byte[] readBytes() {
419421
public byte[] readBytes(int max) {
420422
if (bufferIndex == bufferLast) return null;
421423

422-
synchronized (buffer) {
424+
synchronized (bufferLock) {
423425
int length = bufferLast - bufferIndex;
424426
if (length > max) length = max;
425427
byte outgoing[] = new byte[length];
@@ -451,7 +453,7 @@ public byte[] readBytes(int max) {
451453
public int readBytes(byte bytebuffer[]) {
452454
if (bufferIndex == bufferLast) return 0;
453455

454-
synchronized (buffer) {
456+
synchronized (bufferLock) {
455457
int length = bufferLast - bufferIndex;
456458
if (length > bytebuffer.length) length = bytebuffer.length;
457459
System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length);
@@ -490,7 +492,7 @@ public byte[] readBytesUntil(int interesting) {
490492
if (bufferIndex == bufferLast) return null;
491493
byte what = (byte)interesting;
492494

493-
synchronized (buffer) {
495+
synchronized (bufferLock) {
494496
int found = -1;
495497
for (int k = bufferIndex; k < bufferLast; k++) {
496498
if (buffer[k] == what) {
@@ -531,7 +533,7 @@ public int readBytesUntil(int interesting, byte byteBuffer[]) {
531533
if (bufferIndex == bufferLast) return 0;
532534
byte what = (byte)interesting;
533535

534-
synchronized (buffer) {
536+
synchronized (bufferLock) {
535537
int found = -1;
536538
for (int k = bufferIndex; k < bufferLast; k++) {
537539
if (buffer[k] == what) {

0 commit comments

Comments
 (0)