Skip to content

Commit d65683a

Browse files
committed
Code cleanup
1 parent 0c724ec commit d65683a

File tree

11 files changed

+416
-435
lines changed

11 files changed

+416
-435
lines changed

src/main/java/org/java_websocket/AbstractWrappedByteChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public int write( ByteBuffer src ) throws IOException {
4242

4343
@Override
4444
public boolean isNeedWrite() {
45-
return channel instanceof WrappedByteChannel ? ( (WrappedByteChannel) channel ).isNeedWrite() : false;
45+
return channel instanceof WrappedByteChannel && ((WrappedByteChannel) channel).isNeedWrite();
4646
}
4747

4848
@Override
@@ -54,7 +54,7 @@ public void writeMore() throws IOException {
5454

5555
@Override
5656
public boolean isNeedRead() {
57-
return channel instanceof WrappedByteChannel ? ( (WrappedByteChannel) channel ).isNeedRead() : false;
57+
return channel instanceof WrappedByteChannel && ((WrappedByteChannel) channel).isNeedRead();
5858

5959
}
6060

src/main/java/org/java_websocket/SocketChannelIOHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.nio.ByteBuffer;
55
import java.nio.channels.ByteChannel;
6-
import java.nio.channels.spi.AbstractSelectableChannel;
76

87
import org.java_websocket.WebSocket.Role;
98

@@ -66,6 +65,6 @@ public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws
6665
ws.closeConnection();
6766
}
6867
}
69-
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
68+
return c == null || !((WrappedByteChannel) sockchannel).isNeedWrite();
7069
}
7170
}

src/main/java/org/java_websocket/WebSocket.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum Role {
1414
}
1515

1616
public enum READYSTATE {
17-
NOT_YET_CONNECTED, CONNECTING, OPEN, CLOSING, CLOSED;
17+
NOT_YET_CONNECTED, CONNECTING, OPEN, CLOSING, CLOSED
1818
}
1919

2020
/**
@@ -45,17 +45,16 @@ public enum READYSTATE {
4545

4646
/**
4747
* Send Text data to the other end.
48-
*
49-
* @throws IllegalArgumentException
50-
* @throws NotYetConnectedException
48+
*
49+
* @throws NotYetConnectedException websocket is not yet connected
5150
*/
5251
public abstract void send( String text ) throws NotYetConnectedException;
5352

5453
/**
5554
* Send Binary data (plain bytes) to the other end.
5655
*
57-
* @throws IllegalArgumentException
58-
* @throws NotYetConnectedException
56+
* @throws IllegalArgumentException the data is null
57+
* @throws NotYetConnectedException websocket is not yet connected
5958
*/
6059
public abstract void send( ByteBuffer bytes ) throws IllegalArgumentException , NotYetConnectedException;
6160

src/main/java/org/java_websocket/WebSocketAdapter.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,8 @@ public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
9292
if(null == adr){
9393
throw new InvalidHandshakeException( "socket not bound" );
9494
}
95-
96-
StringBuffer sb = new StringBuffer( 90 );
97-
sb.append( "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" );
98-
sb.append(adr.getPort());
99-
sb.append( "\" /></cross-domain-policy>\0" );
100-
101-
return sb.toString();
95+
96+
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + adr.getPort() +"\" /></cross-domain-policy>\0";
10297
}
10398

10499
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void decode( ByteBuffer socketBuffer ) {
155155
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );
156156

157157
if( readystate != READYSTATE.NOT_YET_CONNECTED ) {
158-
decodeFrames( socketBuffer );;
158+
decodeFrames( socketBuffer );
159159
} else {
160160
if( decodeHandshake( socketBuffer ) ) {
161161
assert ( tmpHandshakeBytes.hasRemaining() != socketBuffer.hasRemaining() || !socketBuffer.hasRemaining() ); // the buffers will never have remaining bytes at the same time
@@ -203,7 +203,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
203203
return false;
204204
}
205205
}
206-
HandshakeState handshakestate = null;
206+
HandshakeState handshakestate;
207207

208208
try {
209209
if( role == Role.SERVER ) {
@@ -214,7 +214,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
214214
d.setParseMode( role );
215215
socketBuffer.reset();
216216
Handshakedata tmphandshake = d.translateHandshake( socketBuffer );
217-
if( tmphandshake instanceof ClientHandshake == false ) {
217+
if(!(tmphandshake instanceof ClientHandshake)) {
218218
flushAndClose( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
219219
return false;
220220
}
@@ -249,7 +249,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
249249
} else {
250250
// special case for multiple step handshakes
251251
Handshakedata tmphandshake = draft.translateHandshake( socketBuffer );
252-
if( tmphandshake instanceof ClientHandshake == false ) {
252+
if(!(tmphandshake instanceof ClientHandshake)) {
253253
flushAndClose( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
254254
return false;
255255
}
@@ -267,7 +267,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
267267
} else if( role == Role.CLIENT ) {
268268
draft.setParseMode( role );
269269
Handshakedata tmphandshake = draft.translateHandshake( socketBuffer );
270-
if( tmphandshake instanceof ServerHandshake == false ) {
270+
if(!(tmphandshake instanceof ServerHandshake)) {
271271
flushAndClose( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
272272
return false;
273273
}
@@ -397,7 +397,7 @@ private void close( int code, String message, boolean remote ) {
397397
if( readystate != READYSTATE.CLOSING && readystate != READYSTATE.CLOSED ) {
398398
if( readystate == READYSTATE.OPEN ) {
399399
if( code == CloseFrame.ABNORMAL_CLOSE ) {
400-
assert ( remote == false );
400+
assert (!remote);
401401
readystate = READYSTATE.CLOSING;
402402
flushAndClose( code, message, false );
403403
return;
@@ -539,9 +539,7 @@ public void close( InvalidDataException e ) {
539539

540540
/**
541541
* Send Text data to the other end.
542-
*
543-
* @throws IllegalArgumentException
544-
* @throws NotYetConnectedException
542+
* @throws NotYetConnectedException websocket is not yet connected
545543
*/
546544
@Override
547545
public void send( String text ) throws WebsocketNotConnectedException {
@@ -552,9 +550,9 @@ public void send( String text ) throws WebsocketNotConnectedException {
552550

553551
/**
554552
* Send Binary data (plain bytes) to the other end.
555-
*
556-
* @throws IllegalArgumentException
557-
* @throws NotYetConnectedException
553+
*
554+
* @throws IllegalArgumentException the data is null
555+
* @throws NotYetConnectedException websocket is not yet connected
558556
*/
559557
@Override
560558
public void send( ByteBuffer bytes ) throws IllegalArgumentException , WebsocketNotConnectedException {
@@ -669,13 +667,13 @@ private void open( Handshakedata d ) {
669667

670668
@Override
671669
public boolean isConnecting() {
672-
assert ( flushandclosestate ? readystate == READYSTATE.CONNECTING : true );
670+
assert (!flushandclosestate || readystate == READYSTATE.CONNECTING);
673671
return readystate == READYSTATE.CONNECTING; // ifflushandclosestate
674672
}
675673

676674
@Override
677675
public boolean isOpen() {
678-
assert ( readystate == READYSTATE.OPEN ? !flushandclosestate : true );
676+
assert (readystate != READYSTATE.OPEN || !flushandclosestate);
679677
return readystate == READYSTATE.OPEN;
680678
}
681679

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum CloseHandshakeType {
4141
NONE, ONEWAY, TWOWAY
4242
}
4343

44-
public static int MAX_FAME_SIZE = 1000 * 1;
44+
public static int MAX_FAME_SIZE = 1000;
4545
public static int INITIAL_FAMESIZE = 64;
4646

4747
public static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>\0" );
@@ -129,7 +129,7 @@ protected boolean basicAccept( Handshakedata handshakedata ) {
129129
public abstract List<Framedata> createFrames( String text, boolean mask );
130130

131131
public List<Framedata> continuousFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
132-
if( op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT ) {
132+
if(op != Opcode.BINARY && op != Opcode.TEXT) {
133133
throw new IllegalArgumentException( "Only Opcode.BINARY or Opcode.TEXT are allowed" );
134134
}
135135

@@ -167,7 +167,7 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
167167
bui.append( ( (ClientHandshake) handshakedata ).getResourceDescriptor() );
168168
bui.append( " HTTP/1.1" );
169169
} else if( handshakedata instanceof ServerHandshake ) {
170-
bui.append( "HTTP/1.1 101 " + ( (ServerHandshake) handshakedata ).getHttpStatusMessage() );
170+
bui.append("HTTP/1.1 101 ").append(((ServerHandshake) handshakedata).getHttpStatusMessage());
171171
} else {
172172
throw new RuntimeException( "unknown role" );
173173
}

0 commit comments

Comments
 (0)