Skip to content

Commit 2a49ceb

Browse files
committed
Code cleanups
1 parent cf55a49 commit 2a49ceb

File tree

8 files changed

+70
-70
lines changed

8 files changed

+70
-70
lines changed

src/main/java/org/java_websocket/exceptions/NotSendableException.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ public class NotSendableException extends RuntimeException {
3535
*/
3636
private static final long serialVersionUID = -6468967874576651628L;
3737

38-
/**
39-
* constructor for a NotSendableException
40-
*/
41-
public NotSendableException() {
42-
super();
43-
}
44-
4538
/**
4639
* constructor for a NotSendableException
4740
*

src/main/java/org/java_websocket/exceptions/WebsocketNotConnectedException.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,4 @@ public class WebsocketNotConnectedException extends RuntimeException {
3434
* Serializable
3535
*/
3636
private static final long serialVersionUID = -785314021592982715L;
37-
38-
/**
39-
* constructor for a WebsocketNotConnectedException
40-
*/
41-
public WebsocketNotConnectedException() {
42-
super();
43-
}
4437
}

src/main/java/org/java_websocket/extensions/CompressionExtension.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ public abstract class CompressionExtension extends DefaultExtension {
3838

3939
@Override
4040
public void isFrameValid( Framedata inputFrame ) throws InvalidDataException {
41-
if( inputFrame instanceof DataFrame ) {
42-
if( inputFrame.isRSV2() || inputFrame.isRSV3() ) {
43-
throw new InvalidFrameException( "bad rsv RSV1: " + inputFrame.isRSV1() + " RSV2: " + inputFrame.isRSV2() + " RSV3: " + inputFrame.isRSV3() );
44-
}
41+
if(( inputFrame instanceof DataFrame ) && ( inputFrame.isRSV2() || inputFrame.isRSV3() )) {
42+
throw new InvalidFrameException( "bad rsv RSV1: " + inputFrame.isRSV1() + " RSV2: " + inputFrame.isRSV2() + " RSV3: " + inputFrame.isRSV3() );
4543
}
46-
if( inputFrame instanceof ControlFrame ) {
47-
if( inputFrame.isRSV1() || inputFrame.isRSV2() || inputFrame.isRSV3() ) {
48-
throw new InvalidFrameException( "bad rsv RSV1: " + inputFrame.isRSV1() + " RSV2: " + inputFrame.isRSV2() + " RSV3: " + inputFrame.isRSV3() );
49-
}
44+
if(( inputFrame instanceof ControlFrame ) && ( inputFrame.isRSV1() || inputFrame.isRSV2() || inputFrame.isRSV3() )) {
45+
throw new InvalidFrameException( "bad rsv RSV1: " + inputFrame.isRSV1() + " RSV2: " + inputFrame.isRSV2() + " RSV3: " + inputFrame.isRSV3() );
5046
}
5147
}
5248
}

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -793,19 +793,24 @@ public void broadcast(byte[] data) {
793793
* @param clients a collection of endpoints to whom the text has to be send
794794
*/
795795
public void broadcast(byte[] data, Collection<WebSocket> clients) {
796+
if (data == null || clients == null) {
797+
throw new IllegalArgumentException();
798+
}
796799
Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>();
797800
ByteBuffer byteBufferData = ByteBuffer.wrap( data );
798801
synchronized( clients ) {
799802
for( WebSocket client : clients ) {
800-
Draft draft = client.getDraft();
801-
if( !draftFrames.containsKey( draft ) ) {
802-
List<Framedata> frames = draft.createFrames( byteBufferData, false );
803-
draftFrames.put( draft, frames );
804-
}
805-
try {
806-
client.sendFrame( draftFrames.get( draft ) );
807-
} catch ( WebsocketNotConnectedException e) {
808-
//Ignore this exception in this case
803+
if( client != null ) {
804+
Draft draft = client.getDraft();
805+
if( !draftFrames.containsKey( draft ) ) {
806+
List<Framedata> frames = draft.createFrames( byteBufferData, false );
807+
draftFrames.put( draft, frames );
808+
}
809+
try {
810+
client.sendFrame( draftFrames.get( draft ) );
811+
} catch ( WebsocketNotConnectedException e ) {
812+
//Ignore this exception in this case
813+
}
809814
}
810815
}
811816
}
@@ -817,18 +822,23 @@ public void broadcast(byte[] data, Collection<WebSocket> clients) {
817822
* @param clients a collection of endpoints to whom the text has to be send
818823
*/
819824
public void broadcast(String text, Collection<WebSocket> clients) {
825+
if (text == null || clients == null) {
826+
throw new IllegalArgumentException();
827+
}
820828
Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>();
821829
synchronized( clients ) {
822830
for( WebSocket client : clients ) {
823-
Draft draft = client.getDraft();
824-
if( !draftFrames.containsKey( draft ) ) {
825-
List<Framedata> frames = draft.createFrames( text, false );
826-
draftFrames.put( draft, frames );
827-
}
828-
try {
829-
client.sendFrame( draftFrames.get( draft ) );
830-
} catch ( WebsocketNotConnectedException e) {
831-
//Ignore this exception in this case
831+
if( client != null ) {
832+
Draft draft = client.getDraft();
833+
if( !draftFrames.containsKey( draft ) ) {
834+
List<Framedata> frames = draft.createFrames( text, false );
835+
draftFrames.put( draft, frames );
836+
}
837+
try {
838+
client.sendFrame( draftFrames.get( draft ) );
839+
} catch ( WebsocketNotConnectedException e ) {
840+
//Ignore this exception in this case
841+
}
832842
}
833843
}
834844
}

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
* it should have been done this way to begin with.</li>
109109
* <li><em>Removed all references to System.out, System.err, and the like.</em>
110110
* Shame on me. All I can say is sorry they were ever there.</li>
111-
* <li><em>Throws NullPointerExceptions and IllegalArgumentExceptions</em> as needed
111+
* <li><em>Throws IllegalArgumentExceptions</em> as needed
112112
* such as when passed arrays are null or offsets are invalid.</li>
113113
* <li>Cleaned up as much javadoc as I could to avoid any javadoc warnings.
114114
* This was especially annoying before for people who were thorough in their
@@ -635,7 +635,7 @@ public static void encode( java.nio.ByteBuffer raw, java.nio.CharBuffer encoded
635635
* @param serializableObject The object to encode
636636
* @return The Base64-encoded object
637637
* @throws java.io.IOException if there is an error
638-
* @throws NullPointerException if serializedObject is null
638+
* @throws IllegalArgumentException if serializedObject is null
639639
* @since 1.4
640640
*/
641641
public static String encodeObject( java.io.Serializable serializableObject )
@@ -678,7 +678,7 @@ public static String encodeObject( java.io.Serializable serializableObject, int
678678
throws java.io.IOException {
679679

680680
if( serializableObject == null ){
681-
throw new NullPointerException( "Cannot serialize a null object." );
681+
throw new IllegalArgumentException( "Cannot serialize a null object." );
682682
} // end if: null
683683

684684
// Streams
@@ -733,7 +733,7 @@ public static String encodeObject( java.io.Serializable serializableObject, int
733733
*
734734
* @param source The data to convert
735735
* @return The data in Base64-encoded form
736-
* @throws NullPointerException if source array is null
736+
* @throws IllegalArgumentException if source array is null
737737
* @since 1.4
738738
*/
739739
public static String encodeBytes( byte[] source ) {
@@ -778,7 +778,7 @@ public static String encodeBytes( byte[] source ) {
778778
* @see Base64#GZIP
779779
* @see Base64#DO_BREAK_LINES
780780
* @throws java.io.IOException if there is an error
781-
* @throws NullPointerException if source array is null
781+
* @throws IllegalArgumentException if source array is null
782782
* @since 2.0
783783
*/
784784
public static String encodeBytes( byte[] source, int options ) throws java.io.IOException {
@@ -800,8 +800,7 @@ public static String encodeBytes( byte[] source, int options ) throws java.io.IO
800800
* @param off Offset in array where conversion should begin
801801
* @param len Length of data to convert
802802
* @return The Base64-encoded data as a String
803-
* @throws NullPointerException if source array is null
804-
* @throws IllegalArgumentException if source array, offset, or length are invalid
803+
* @throws IllegalArgumentException if source array is null if source array, offset, or length are invalid
805804
* @since 1.4
806805
*/
807806
public static String encodeBytes( byte[] source, int off, int len ) {
@@ -848,8 +847,7 @@ public static String encodeBytes( byte[] source, int off, int len ) {
848847
* @see Base64#GZIP
849848
* @see Base64#DO_BREAK_LINES
850849
* @throws java.io.IOException if there is an error
851-
* @throws NullPointerException if source array is null
852-
* @throws IllegalArgumentException if source array, offset, or length are invalid
850+
* @throws IllegalArgumentException if source array is null, if source array, offset, or length are invalid
853851
* @since 2.0
854852
*/
855853
public static String encodeBytes( byte[] source, int off, int len, int options ) throws java.io.IOException {
@@ -876,7 +874,7 @@ public static String encodeBytes( byte[] source, int off, int len, int options )
876874
*
877875
* @param source The data to convert
878876
* @return The Base64-encoded data as a byte[] (of ASCII characters)
879-
* @throws NullPointerException if source array is null
877+
* @throws IllegalArgumentException if source array is null
880878
* @since 2.3.1
881879
*/
882880
public static byte[] encodeBytesToBytes( byte[] source ) {
@@ -904,14 +902,13 @@ public static byte[] encodeBytesToBytes( byte[] source ) {
904902
* @see Base64#GZIP
905903
* @see Base64#DO_BREAK_LINES
906904
* @throws java.io.IOException if there is an error
907-
* @throws NullPointerException if source array is null
908-
* @throws IllegalArgumentException if source array, offset, or length are invalid
905+
* @throws IllegalArgumentException if source array is null, if source array, offset, or length are invalid
909906
* @since 2.3.1
910907
*/
911908
public static byte[] encodeBytesToBytes( byte[] source, int off, int len, int options ) throws java.io.IOException {
912909

913910
if( source == null ){
914-
throw new NullPointerException( "Cannot serialize a null array." );
911+
throw new IllegalArgumentException( "Cannot serialize a null array." );
915912
} // end if: null
916913

917914
if( off < 0 ){
@@ -1047,8 +1044,7 @@ public static byte[] encodeBytesToBytes( byte[] source, int off, int len, int op
10471044
* @param destOffset the index where output will be put
10481045
* @param options alphabet type is pulled from this (standard, url-safe, ordered)
10491046
* @return the number of decoded bytes converted
1050-
* @throws NullPointerException if source or destination arrays are null
1051-
* @throws IllegalArgumentException if srcOffset or destOffset are invalid
1047+
* @throws IllegalArgumentException if source or destination arrays are null, if srcOffset or destOffset are invalid
10521048
* or there is not enough room in the array.
10531049
* @since 1.3
10541050
*/
@@ -1058,10 +1054,10 @@ private static int decode4to3(
10581054

10591055
// Lots of error checking and exception throwing
10601056
if( source == null ){
1061-
throw new NullPointerException( "Source array was null." );
1057+
throw new IllegalArgumentException( "Source array was null." );
10621058
} // end if
10631059
if( destination == null ){
1064-
throw new NullPointerException( "Destination array was null." );
1060+
throw new IllegalArgumentException( "Destination array was null." );
10651061
} // end if
10661062
if( srcOffset < 0 || srcOffset + 3 >= source.length ){
10671063
throw new IllegalArgumentException( String.format(
@@ -1167,7 +1163,7 @@ public static byte[] decode( byte[] source, int off, int len, int options )
11671163

11681164
// Lots of error checking and exception throwing
11691165
if( source == null ){
1170-
throw new NullPointerException( "Cannot decode null source array." );
1166+
throw new IllegalArgumentException( "Cannot decode null source array." );
11711167
} // end if
11721168
if( off < 0 || off + len > source.length ){
11731169
throw new IllegalArgumentException( String.format(
@@ -1251,13 +1247,13 @@ public static byte[] decode( String s ) throws java.io.IOException {
12511247
* @param options encode options such as URL_SAFE
12521248
* @return the decoded data
12531249
* @throws java.io.IOException if there is an error
1254-
* @throws NullPointerException if <tt>s</tt> is null
1250+
* @throws IllegalArgumentException if <tt>s</tt> is null
12551251
* @since 1.4
12561252
*/
12571253
public static byte[] decode( String s, int options ) throws java.io.IOException {
12581254

12591255
if( s == null ){
1260-
throw new NullPointerException( "Input string was null." );
1256+
throw new IllegalArgumentException( "Input string was null." );
12611257
} // end if
12621258

12631259
byte[] bytes;
@@ -1322,7 +1318,7 @@ public static byte[] decode( String s, int options ) throws java.io.IOException
13221318
*
13231319
* @param encodedObject The Base64 data to decode
13241320
* @return The decoded and deserialized object
1325-
* @throws NullPointerException if encodedObject is null
1321+
* @throws IllegalArgumentException if encodedObject is null
13261322
* @throws java.io.IOException if there is a general error
13271323
* @throws ClassNotFoundException if the decoded object is of a
13281324
* class that cannot be found by the JVM
@@ -1344,7 +1340,7 @@ public static Object decodeToObject( String encodedObject )
13441340
* @param options Various parameters related to decoding
13451341
* @param loader Optional class loader to use in deserializing classes.
13461342
* @return The decoded and deserialized object
1347-
* @throws NullPointerException if encodedObject is null
1343+
* @throws IllegalArgumentException if encodedObject is null
13481344
* @throws java.io.IOException if there is a general error
13491345
* @throws ClassNotFoundException if the decoded object is of a
13501346
* class that cannot be found by the JVM
@@ -1415,14 +1411,14 @@ public Class<?> resolveClass(java.io.ObjectStreamClass streamClass)
14151411
* @param dataToEncode byte array of data to encode in base64 form
14161412
* @param filename Filename for saving encoded data
14171413
* @throws java.io.IOException if there is an error
1418-
* @throws NullPointerException if dataToEncode is null
1414+
* @throws IllegalArgumentException if dataToEncode is null
14191415
* @since 2.1
14201416
*/
14211417
public static void encodeToFile( byte[] dataToEncode, String filename )
14221418
throws java.io.IOException {
14231419

14241420
if( dataToEncode == null ){
1425-
throw new NullPointerException( "Data to encode was null." );
1421+
throw new IllegalArgumentException( "Data to encode was null." );
14261422
} // end iff
14271423

14281424
Base64.OutputStream bos = null;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
package org.java_websocket.framing;
2727

2828
import org.java_websocket.exceptions.InvalidDataException;
29-
import org.java_websocket.util.ByteBufferUtils;
3029
import org.junit.Test;
3130

32-
import java.nio.ByteBuffer;
33-
3431
import static org.junit.Assert.assertEquals;
3532
import static org.junit.Assert.fail;
3633

@@ -64,6 +61,11 @@ public void testExtends() {
6461

6562
@Test
6663
public void testIsValid() {
67-
//Nothing specific to test
64+
BinaryFrame frame = new BinaryFrame();
65+
try {
66+
frame.isValid();
67+
} catch (InvalidDataException e) {
68+
fail("InvalidDataException should not be thrown");
69+
}
6870
}
6971
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void testExtends() {
6161

6262
@Test
6363
public void testIsValid() {
64-
//Nothing specific to test
64+
ContinuousFrame frame = new ContinuousFrame();
65+
try {
66+
frame.isValid();
67+
} catch (InvalidDataException e) {
68+
fail("InvalidDataException should not be thrown");
69+
}
6570
}
6671
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void testExtends() {
6161

6262
@Test
6363
public void testIsValid() {
64-
//Nothing specific to test
64+
TextFrame frame = new TextFrame();
65+
try {
66+
frame.isValid();
67+
} catch (InvalidDataException e) {
68+
fail("InvalidDataException should not be thrown");
69+
}
6570
}
6671
}

0 commit comments

Comments
 (0)