Skip to content

Commit 554ee52

Browse files
committed
Fixed some sonarqube issues
1 parent 99a9911 commit 554ee52

File tree

8 files changed

+80
-60
lines changed

8 files changed

+80
-60
lines changed

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public void run() {
191191
connections.clear();
192192
}
193193
};
194-
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
194+
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,1000L*connectionLostTimeout , 1000L*connectionLostTimeout );
195+
195196
}
196197

197198
/**

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@
6161
* text frames, and receiving frames through an event-based model.
6262
*/
6363
public class WebSocketImpl implements WebSocket {
64-
public static int RCVBUF = 16384;
64+
65+
/**
66+
* Initial buffer size
67+
*/
68+
public static final int RCVBUF = 16384;
6569

6670
/**
6771
* Activate debug mode for additional infos
@@ -186,16 +190,6 @@ public WebSocketImpl( WebSocketListener listener, Draft draft ) {
186190
this.draft = draft.copyInstance();
187191
}
188192

189-
@Deprecated
190-
public WebSocketImpl( WebSocketListener listener, Draft draft, Socket socket ) {
191-
this( listener, draft );
192-
}
193-
194-
@Deprecated
195-
public WebSocketImpl( WebSocketListener listener, List<Draft> drafts, Socket socket ) {
196-
this( listener, drafts );
197-
}
198-
199193
/**
200194
* Method to decode the provided ByteBuffer
201195
*
@@ -743,11 +737,6 @@ private void setReadyState( READYSTATE readystate ) {
743737
this.readystate = readystate;
744738
}
745739

746-
@Override
747-
public int hashCode() {
748-
return super.hashCode();
749-
}
750-
751740
@Override
752741
public String toString() {
753742
return super.toString(); // its nice to be able to set breakpoints here

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ public void run() {
665665
ostream.write( buffer.array(), 0, buffer.limit() );
666666
ostream.flush();
667667
}
668+
Thread.currentThread().interrupt();
668669
}
669670
} catch ( IOException e ) {
670671
handleIOException( e );

src/main/java/org/java_websocket/drafts/Draft.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public enum CloseHandshakeType {
7070
NONE, ONEWAY, TWOWAY
7171
}
7272

73-
public static int MAX_FAME_SIZE = 1000;
74-
public static int INITIAL_FAMESIZE = 64;
75-
7673
/** In some cases the handshake will be parsed different depending on whether */
7774
protected Role role = null;
7875

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -841,23 +841,7 @@ public void broadcast(ByteBuffer data, Collection<WebSocket> clients) {
841841
if (data == null || clients == null) {
842842
throw new IllegalArgumentException();
843843
}
844-
Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>();
845-
synchronized( clients ) {
846-
for( WebSocket client : clients ) {
847-
if( client != null ) {
848-
Draft draft = client.getDraft();
849-
if( !draftFrames.containsKey( draft ) ) {
850-
List<Framedata> frames = draft.createFrames( data, false );
851-
draftFrames.put( draft, frames );
852-
}
853-
try {
854-
client.sendFrame( draftFrames.get( draft ) );
855-
} catch ( WebsocketNotConnectedException e ) {
856-
//Ignore this exception in this case
857-
}
858-
}
859-
}
860-
}
844+
doBroadcast(data, clients);
861845
}
862846

863847
/**
@@ -869,21 +853,47 @@ public void broadcast(String text, Collection<WebSocket> clients) {
869853
if (text == null || clients == null) {
870854
throw new IllegalArgumentException();
871855
}
856+
doBroadcast(text, clients);
857+
}
858+
859+
/**
860+
* Private method to cache all the frames to improve memory footprint and conversion time
861+
* @param data the data to broadcast
862+
* @param clients the clients to send the message to
863+
*/
864+
private void doBroadcast(Object data, Collection<WebSocket> clients) {
865+
String sData = null;
866+
if (data instanceof String) {
867+
sData = (String)data;
868+
}
869+
ByteBuffer bData = null;
870+
if (data instanceof ByteBuffer) {
871+
bData = (ByteBuffer)data;
872+
}
873+
if (sData == null && bData == null) {
874+
return;
875+
}
872876
Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>();
873-
synchronized( clients ) {
874-
for( WebSocket client : clients ) {
875-
if( client != null ) {
876-
Draft draft = client.getDraft();
877-
if( !draftFrames.containsKey( draft ) ) {
878-
List<Framedata> frames = draft.createFrames( text, false );
879-
draftFrames.put( draft, frames );
877+
for( WebSocket client : clients ) {
878+
if( client != null ) {
879+
Draft draft = client.getDraft();
880+
if( !draftFrames.containsKey( draft ) ) {
881+
List<Framedata> frames = null;
882+
if (sData != null) {
883+
frames = draft.createFrames( sData, false );
880884
}
881-
try {
882-
client.sendFrame( draftFrames.get( draft ) );
883-
} catch ( WebsocketNotConnectedException e ) {
884-
//Ignore this exception in this case
885+
if (bData != null) {
886+
frames = draft.createFrames( bData, false );
887+
}
888+
if (frames != null) {
889+
draftFrames.put(draft, frames);
885890
}
886891
}
892+
try {
893+
client.sendFrame( draftFrames.get( draft ) );
894+
} catch ( WebsocketNotConnectedException e ) {
895+
//Ignore this exception in this case
896+
}
887897
}
888898
}
889899
}
@@ -933,6 +943,7 @@ public void run() {
933943
}
934944
}
935945
} catch ( InterruptedException e ) {
946+
Thread.currentThread().interrupt();
936947
} catch ( RuntimeException e ) {
937948
handleFatal( ws, e );
938949
}

src/main/java/org/java_websocket/util/Base64.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,9 @@ public static byte[] encodeBytesToBytes( byte[] source, int off, int len, int op
692692
throw e;
693693
} // end catch
694694
finally {
695-
try{ gzos.close(); } catch( Exception e ){}
696-
try{ b64os.close(); } catch( Exception e ){}
697-
try{ baos.close(); } catch( Exception e ){}
695+
try{ if (gzos != null) gzos.close(); } catch( Exception e ){}
696+
try{ if (b64os != null) b64os.close(); } catch( Exception e ){}
697+
try{ if (baos != null) baos.close(); } catch( Exception e ){}
698698
} // end finally
699699

700700
return baos.toByteArray();
@@ -1039,9 +1039,9 @@ public static byte[] decode( String s, int options ) throws java.io.IOException
10391039
// Just return originally-decoded bytes
10401040
} // end catch
10411041
finally {
1042-
try{ baos.close(); } catch( Exception e ){}
1043-
try{ gzis.close(); } catch( Exception e ){}
1044-
try{ bais.close(); } catch( Exception e ){}
1042+
try{ if (baos != null) baos.close(); } catch( Exception e ){}
1043+
try{ if (gzis != null) gzis.close(); } catch( Exception e ){}
1044+
try{ if (bais != null) bais.close(); } catch( Exception e ){}
10451045
} // end finally
10461046

10471047
} // end if: gzipped
@@ -1103,7 +1103,7 @@ public static byte[] decodeFromFile( String filename )
11031103
throw e; // Catch and release to execute finally{}
11041104
} // end catch: java.io.IOException
11051105
finally {
1106-
try{ bis.close(); } catch( Exception e) {}
1106+
try{ if (bis != null) bis.close(); } catch( Exception e) {}
11071107
} // end finally
11081108

11091109
return decodedData;
@@ -1156,7 +1156,7 @@ public static String encodeFromFile( String filename )
11561156
throw e; // Catch and release to execute finally{}
11571157
} // end catch: java.io.IOException
11581158
finally {
1159-
try{ bis.close(); } catch( Exception e) {}
1159+
try{ if (bis != null) bis.close(); } catch( Exception e) {}
11601160
} // end finally
11611161

11621162
return encodedData;

src/main/java/org/java_websocket/util/Charsetfunctions.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ public class Charsetfunctions {
4040
/**
4141
* Private constructor for real static class
4242
*/
43-
private Charsetfunctions() {
43+
private Charsetfunctions() {}
4444

45-
}
46-
47-
public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
45+
private static final CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
4846

4947
/*
5048
* @return UTF-8 encoding in bytes

src/test/java/org/java_websocket/framing/CloseFrameTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,28 @@ public void testIsValid() {
213213
} catch (InvalidDataException e) {
214214
//fine
215215
}
216+
frame.setCode(CloseFrame.NOCODE);
217+
try {
218+
frame.isValid();
219+
fail("InvalidDataException should be thrown");
220+
} catch (InvalidDataException e) {
221+
//fine
222+
}
223+
frame.setCode(CloseFrame.NO_UTF8);
224+
frame.setReason(null);
225+
try {
226+
frame.isValid();
227+
fail("InvalidDataException should be thrown");
228+
} catch (InvalidDataException e) {
229+
//fine
230+
}
231+
frame.setCode(CloseFrame.NOCODE);
232+
frame.setReason("Close");
233+
try {
234+
frame.isValid();
235+
fail("InvalidDataException should be thrown");
236+
} catch (InvalidDataException e) {
237+
//fine
238+
}
216239
}
217240
}

0 commit comments

Comments
 (0)