Skip to content

Commit b9de830

Browse files
committed
Acted on errors identified by FindBugs.
1 parent e2a017f commit b9de830

File tree

8 files changed

+52
-25
lines changed

8 files changed

+52
-25
lines changed

src/org/java_websocket/WebSocket.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.concurrent.BlockingQueue;
1212
import java.util.concurrent.LinkedBlockingQueue;
13+
import java.util.concurrent.atomic.AtomicLong;
1314

1415
import org.java_websocket.drafts.Draft;
1516
import org.java_websocket.drafts.Draft.CloseHandshakeType;
@@ -90,12 +91,13 @@ public enum Role {
9091
* Queue of buffers that need to be sent to the client.
9192
*/
9293
private BlockingQueue<ByteBuffer> bufferQueue;
94+
9395
/**
9496
* The amount of bytes still in queue to be sent, at every given time.
9597
* It's updated at every send/sent operation.
9698
*/
97-
private Long bufferQueueTotalAmount = (long) 0;
98-
99+
private AtomicLong bufferQueueTotalAmount = new AtomicLong(0l);
100+
99101
private Draft draft = null;
100102

101103
private Role role;
@@ -495,7 +497,7 @@ boolean hasBufferedData() {
495497
* @return Amount of Data still in Queue and not sent yet of the socket
496498
*/
497499
long bufferedDataAmount() {
498-
return bufferQueueTotalAmount;
500+
return bufferQueueTotalAmount.get();
499501
}
500502

501503
/**
@@ -508,10 +510,9 @@ public void flush() throws IOException {
508510
if( buffer.remaining() > 0 ) {
509511
continue;
510512
} else {
511-
synchronized ( bufferQueueTotalAmount ) {
512-
// subtract this amount of data from the total queued (synchronized over this object)
513-
bufferQueueTotalAmount -= buffer.limit();
514-
}
513+
// subtract this amount of data from the total queued (synchronized over this object)
514+
bufferQueueTotalAmount.addAndGet(-buffer.limit());
515+
515516
this.bufferQueue.poll(); // Buffer finished. Remove it.
516517
buffer = this.bufferQueue.peek();
517518
}
@@ -559,10 +560,10 @@ private void channelWrite( ByteBuffer buf ) throws InterruptedException {
559560
if( DEBUG )
560561
System.out.println( "write(" + buf.limit() + "): {" + ( buf.limit() > 1000 ? "too big to display" : new String( buf.array() ) ) + "}" );
561562
buf.rewind(); // TODO rewinding should not be nessesary
562-
synchronized ( bufferQueueTotalAmount ) {
563-
// add up the number of bytes to the total queued (synchronized over this object)
564-
bufferQueueTotalAmount += buf.limit();
565-
}
563+
564+
// add up the number of bytes to the total queued (synchronized over this object)
565+
bufferQueueTotalAmount.addAndGet(buf.limit());
566+
566567
if( !bufferQueue.offer( buf ) ) {
567568
try {
568569
flush();

src/org/java_websocket/WebSocketClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ public void close() {
107107
if( thread != null ) {
108108
thread.interrupt();
109109
closelock.lock();
110-
if( selector != null )
111-
selector.wakeup();
112-
closelock.unlock();
110+
try {
111+
if( selector != null )
112+
selector.wakeup();
113+
} finally {
114+
closelock.unlock();
115+
}
113116
}
114117

115118
}

src/org/java_websocket/drafts/Draft_10.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
public class Draft_10 extends Draft {
3232

3333
private class IncompleteException extends Throwable {
34+
35+
/**
36+
* It's Serializable.
37+
*/
38+
private static final long serialVersionUID = 7330519489840500997L;
39+
40+
3441
private int preferedsize;
3542
public IncompleteException( int preferedsize ) {
3643
this.preferedsize = preferedsize;
@@ -56,6 +63,8 @@ public static int readVersion( Handshakedata handshakedata ) {
5663

5764
private ByteBuffer incompleteframe;
5865
private Framedata fragmentedframe = null;
66+
67+
private final Random reuseableRandom = new Random();
5968

6069
@Override
6170
public HandshakeState acceptHandshakeAsClient( ClientHandshake request, ServerHandshake response ) throws InvalidHandshakeException {
@@ -106,7 +115,7 @@ public ByteBuffer createBinaryFrame( Framedata framedata ) {
106115

107116
if( mask ) {
108117
ByteBuffer maskkey = ByteBuffer.allocate( 4 );
109-
maskkey.putInt( new Random().nextInt() );
118+
maskkey.putInt( reuseableRandom.nextInt() );
110119
buf.put( maskkey.array() );
111120
for( int i = 0 ; i < mes.length ; i++ ) {
112121
buf.put( (byte) ( mes[ i ] ^ maskkey.get( i % 4 ) ) );
@@ -184,7 +193,7 @@ public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandsha
184193
request.put( "Sec-WebSocket-Version", "8" );
185194

186195
byte[] random = new byte[ 16 ];
187-
new Random().nextBytes( random );
196+
reuseableRandom.nextBytes( random );
188197
request.put( "Sec-WebSocket-Key", Base64.encodeBytes( random ) );
189198

190199
return request;

src/org/java_websocket/drafts/Draft_75.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class Draft_75 extends Draft {
4444
private boolean inframe = false;
4545
protected List<Framedata> readyframes = new LinkedList<Framedata>();
4646
protected ByteBuffer currentFrame;
47+
48+
49+
private final Random reuseableRandom = new Random();
50+
4751

4852
@Override
4953
public HandshakeState acceptHandshakeAsClient( ClientHandshake request, ServerHandshake response ) {
@@ -97,7 +101,7 @@ public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandsha
97101
request.put( "Upgrade", "WebSocket" );
98102
request.put( "Connection", "Upgrade" );
99103
if( !request.hasFieldValue( "Origin" ) ) {
100-
request.put( "Origin", "random" + new Random().nextInt() );
104+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
101105
}
102106

103107
return request;

src/org/java_websocket/drafts/Draft_76.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
public class Draft_76 extends Draft_75 {
2929
private boolean failed = false;
3030
private static final byte[] closehandshake = { -1, 0 };
31+
32+
private final Random reuseableRandom = new Random();
33+
3134

3235
public static byte[] createChallenge( String key1, String key2, byte[] key3 ) throws InvalidHandshakeException {
3336
byte[] part1 = getPart( key1 );
@@ -137,15 +140,15 @@ public HandshakeState acceptHandshakeAsServer( ClientHandshake handshakedata ) {
137140
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandshakeBuilder request ) {
138141
request.put( "Upgrade", "WebSocket" );
139142
request.put( "Connection", "Upgrade" );
140-
request.put( "Sec-WebSocket-Key1", this.generateKey() );
141-
request.put( "Sec-WebSocket-Key2", this.generateKey() );
143+
request.put( "Sec-WebSocket-Key1", generateKey() );
144+
request.put( "Sec-WebSocket-Key2", generateKey() );
142145

143146
if( !request.hasFieldValue( "Origin" ) ) {
144-
request.put( "Origin", "random" + new Random().nextInt() );
147+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
145148
}
146149

147150
byte[] key3 = new byte[ 8 ];
148-
new Random().nextBytes( key3 );
151+
reuseableRandom.nextBytes( key3 );
149152
request.setContent( key3 );
150153
return request;
151154

src/org/java_websocket/exeptions/InvalidDataException.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.java_websocket.exeptions;
22

33
public class InvalidDataException extends Exception {
4+
/**
5+
* Serializable
6+
*/
7+
private static final long serialVersionUID = 3731842424390998726L;
8+
49
private int closecode;
10+
511
public InvalidDataException( int closecode ) {
612
this.closecode = closecode;
713
}
@@ -14,14 +20,14 @@ public InvalidDataException( int closecode , String s ) {
1420
public InvalidDataException( int closecode , Throwable t ) {
1521
super( t );
1622
if( t instanceof InvalidDataException ) {
17-
closecode = ( (InvalidDataException) t ).getCloseCode();
23+
this.closecode = ( (InvalidDataException) t ).getCloseCode();
1824
}
1925
}
2026

2127
public InvalidDataException( int closecode , String s , Throwable t ) {
2228
super( s, t );
2329
if( t instanceof InvalidDataException ) {
24-
closecode = ( (InvalidDataException) t ).getCloseCode();
30+
this.closecode = ( (InvalidDataException) t ).getCloseCode();
2531
}
2632
}
2733

src/org/java_websocket/framing/FramedataImpl1.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.java_websocket.framing;
22

33
import java.nio.ByteBuffer;
4+
import java.util.Arrays;
45

56
import org.java_websocket.exeptions.InvalidDataException;
67
import org.java_websocket.exeptions.InvalidFrameException;
@@ -84,7 +85,7 @@ public void append( Framedata nextframe ) throws InvalidFrameException {
8485

8586
@Override
8687
public String toString() {
87-
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", payloadlength:" + unmaskedpayload.limit() + ", payload:" + Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) + "}";
88+
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", payloadlength:" + unmaskedpayload.limit() + ", payload:" + Arrays.toString(Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) ) + "}";
8889
}
8990

9091
}

src/org/java_websocket/util/Base64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ public static Object decodeToObject(
13591359
@Override
13601360
public Class<?> resolveClass(java.io.ObjectStreamClass streamClass)
13611361
throws java.io.IOException, ClassNotFoundException {
1362-
Class c = Class.forName(streamClass.getName(), false, loader);
1362+
Class<?> c = Class.forName(streamClass.getName(), false, loader);
13631363
if( c == null ){
13641364
return super.resolveClass(streamClass);
13651365
} else {

0 commit comments

Comments
 (0)