Skip to content

Commit caed0d5

Browse files
committed
Code cleanups
1 parent c1ff1ed commit caed0d5

17 files changed

+32
-599
lines changed

src/main/java/org/java_websocket/SSLSocketChannel.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ public class SSLSocketChannel implements WrappedByteChannel, ByteChannel {
7272
*/
7373
private final SSLEngine engine;
7474

75-
/**
76-
* The selection key for this socket channel
77-
* Used to set interestOP SelectionKey.OP_WRITE for the underlying channel
78-
*/
79-
private SelectionKey selectionKey;
80-
8175

8276
/**
8377
* Will contain this peer's application data in plaintext, that will be later encrypted
@@ -130,7 +124,6 @@ public SSLSocketChannel( SocketChannel inputSocketChannel, SSLEngine inputEngine
130124
if( doHandshake() ) {
131125
if( key != null ) {
132126
key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
133-
this.selectionKey = key;
134127
}
135128
} else {
136129
try {
@@ -153,7 +146,7 @@ public synchronized int read( ByteBuffer dst ) throws IOException {
153146
peerNetData.compact();
154147

155148
int bytesRead = socketChannel.read( peerNetData );
156-
/**
149+
/*
157150
* If bytesRead are 0 put we still have some data in peerNetData still to an unwrap (for testcase 1.1.6)
158151
*/
159152
if( bytesRead > 0 || peerNetData.hasRemaining() ) {

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import org.java_websocket.drafts.Draft;
2929
import org.java_websocket.exceptions.InvalidDataException;
30-
import org.java_websocket.exceptions.InvalidHandshakeException;
3130
import org.java_websocket.framing.Framedata;
3231
import org.java_websocket.framing.PingFrame;
3332
import org.java_websocket.framing.PongFrame;
@@ -36,8 +35,6 @@
3635
import org.java_websocket.handshake.ServerHandshake;
3736
import org.java_websocket.handshake.ServerHandshakeBuilder;
3837

39-
import java.net.InetSocketAddress;
40-
4138
/**
4239
* This class default implements all methods of the WebSocketListener that can be overridden optionally when advances functionalities is needed.<br>
4340
**/
@@ -99,29 +96,4 @@ public void onWebsocketPing( WebSocket conn, Framedata f ) {
9996
public void onWebsocketPong( WebSocket conn, Framedata f ) {
10097
//To overwrite
10198
}
102-
103-
/**
104-
* Gets the XML string that should be returned if a client requests a Flash
105-
* security policy.
106-
* <p>
107-
* The default implementation allows access from all remote domains, but
108-
* only on the port that this WebSocketServer is listening on.
109-
* <p>
110-
* This is specifically implemented for gitime's WebSocket client for Flash:
111-
* http://github.com/gimite/web-socket-js
112-
*
113-
* @return An XML String that comforts to Flash's security policy. You MUST
114-
* not include the null char at the end, it is appended automatically.
115-
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained e.g because the websocket is not connected.
116-
*/
117-
@Override
118-
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
119-
InetSocketAddress adr = conn.getLocalSocketAddress();
120-
if( null == adr ) {
121-
throw new InvalidHandshakeException( "socket not bound" );
122-
}
123-
124-
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + adr.getPort() + "\" /></cross-domain-policy>\0";
125-
}
126-
12799
}

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void decode( ByteBuffer socketBuffer ) {
199199
assert ( socketBuffer.hasRemaining() );
200200

201201
if( DEBUG )
202-
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );
202+
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + '}' );
203203

204204
if( getReadyState() != READYSTATE.NOT_YET_CONNECTED ) {
205205
if( getReadyState() == READYSTATE.OPEN ) {
@@ -241,20 +241,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
241241
}
242242
socketBuffer.mark();
243243
try {
244-
if( draft == null ) {
245-
HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
246-
if( isflashedgecase == HandshakeState.MATCHED ) {
247-
try {
248-
write( Collections.singletonList( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) ) );
249-
close( CloseFrame.FLASHPOLICY, "" );
250-
} catch ( InvalidDataException e ) {
251-
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
252-
}
253-
return false;
254-
}
255-
}
256244
HandshakeState handshakestate;
257-
258245
try {
259246
if( role == Role.SERVER ) {
260247
if( draft == null ) {
@@ -647,24 +634,6 @@ public boolean hasBufferedData() {
647634
return !this.outQueue.isEmpty();
648635
}
649636

650-
private HandshakeState isFlashEdgeCase( ByteBuffer request ) throws IncompleteHandshakeException {
651-
request.mark();
652-
if( request.limit() > Draft.FLASH_POLICY_REQUEST.length ) {
653-
return HandshakeState.NOT_MATCHED;
654-
} else if( request.limit() < Draft.FLASH_POLICY_REQUEST.length ) {
655-
throw new IncompleteHandshakeException( Draft.FLASH_POLICY_REQUEST.length );
656-
} else {
657-
658-
for( int flash_policy_index = 0; request.hasRemaining(); flash_policy_index++ ) {
659-
if( Draft.FLASH_POLICY_REQUEST[flash_policy_index] != request.get() ) {
660-
request.reset();
661-
return HandshakeState.NOT_MATCHED;
662-
}
663-
}
664-
return HandshakeState.MATCHED;
665-
}
666-
}
667-
668637
public void startHandshake( ClientHandshakeBuilder handshakedata ) throws InvalidHandshakeException {
669638
assert ( getReadyState() != READYSTATE.CONNECTING ) : "shall only be called once";
670639

@@ -691,7 +660,7 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali
691660

692661
private void write( ByteBuffer buf ) {
693662
if( DEBUG )
694-
System.out.println( "write(" + buf.remaining() + "): {" + ( buf.remaining() > 1000 ? "too big to display" : new String( buf.array() ) ) + "}" );
663+
System.out.println( "write(" + buf.remaining() + "): {" + ( buf.remaining() > 1000 ? "too big to display" : new String( buf.array() ) ) + '}' );
695664

696665
outQueue.add( buf );
697666
/*try {

src/main/java/org/java_websocket/WebSocketListener.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,6 @@ public interface WebSocketListener {
188188
**/
189189
void onWebsocketPong( WebSocket conn, Framedata f );
190190

191-
/**
192-
* @see WebSocketAdapter#getFlashPolicy(WebSocket)
193-
* @param conn The <tt>WebSocket</tt> instance this event is occuring on.
194-
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained.
195-
* @return An XML String that comforts to Flash's security policy. You MUST not include the null char at the end, it is appended automatically.
196-
*/
197-
@Deprecated
198-
String getFlashPolicy( WebSocket conn ) throws InvalidDataException;
199-
200191
/** This method is used to inform the selector thread that there is data queued to be written to the socket.
201192
* @param conn The <tt>WebSocket</tt> instance this event is occuring on.
202193
*/

src/main/java/org/java_websocket/WrappedByteChannel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public interface WrappedByteChannel extends ByteChannel {
3535
3636
* @return is a additional write needed
3737
*/
38-
public boolean isNeedWrite();
38+
boolean isNeedWrite();
3939

4040
/**
4141
* Gets called when {@link #isNeedWrite()} ()} requires a additional rite
4242
* @throws IOException may be thrown due to an error while writing
4343
*/
44-
public void writeMore() throws IOException;
44+
void writeMore() throws IOException;
4545

4646
/**
4747
* returns whether readMore should be called to fetch data which has been decoded but not yet been returned.
@@ -50,19 +50,19 @@ public interface WrappedByteChannel extends ByteChannel {
5050
* @see #readMore(ByteBuffer)
5151
* @return is a additional read needed
5252
**/
53-
public boolean isNeedRead();
53+
boolean isNeedRead();
5454
/**
5555
* This function does not read data from the underlying channel at all. It is just a way to fetch data which has already be received or decoded but was but was not yet returned to the user.
5656
* This could be the case when the decoded data did not fit into the buffer the user passed to {@link #read(ByteBuffer)}.
5757
* @param dst the destiny of the read
5858
* @return the amount of remaining data
5959
* @throws IOException when a error occurred during unwrapping
6060
**/
61-
public int readMore( ByteBuffer dst ) throws IOException;
61+
int readMore( ByteBuffer dst ) throws IOException;
6262

6363
/**
6464
* This function returns the blocking state of the channel
6565
* @return is the channel blocking
6666
*/
67-
public boolean isBlocking();
67+
boolean isBlocking();
6868
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
7171

7272
private Socket socket = null;
7373

74-
private InputStream istream;
75-
7674
private OutputStream ostream;
7775

7876
private Proxy proxy = Proxy.NO_PROXY;
@@ -230,6 +228,7 @@ public void sendPing() throws NotYetConnectedException {
230228

231229
public void run() {
232230

231+
InputStream istream;
233232
try {
234233
boolean isNewSocket = false;
235234

@@ -313,7 +312,7 @@ private void sendHandshake() throws InvalidHandshakeException {
313312
else
314313
path = part1;
315314
if( part2 != null )
316-
path += "?" + part2;
315+
path += '?' + part2;
317316
int port = getPort();
318317
String host = uri.getHost() + ( port != WebSocket.DEFAULT_PORT ? ":" + port : "" );
319318

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ public enum CloseHandshakeType {
7373
public static int MAX_FAME_SIZE = 1000;
7474
public static int INITIAL_FAMESIZE = 64;
7575

76-
public static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>\0" );
77-
7876
/** In some cases the handshake will be parsed different depending on whether */
7977
protected Role role = null;
8078

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandsha
182182
request.put( "Sec-WebSocket-Version", "13" );// overwriting the previous
183183
StringBuilder requestedExtensions = new StringBuilder();
184184
for( IExtension knownExtension : knownExtensions ) {
185-
if( knownExtension.getProvidedExtensionAsClient() != null && !knownExtension.getProvidedExtensionAsClient().equals( "" ) ) {
185+
if( knownExtension.getProvidedExtensionAsClient() != null && knownExtension.getProvidedExtensionAsClient().length() != 0 ) {
186186
requestedExtensions.append( knownExtension.getProvidedExtensionAsClient() ).append( "; " );
187187
}
188188
}
@@ -222,7 +222,7 @@ public Draft copyInstance() {
222222
public ByteBuffer createBinaryFrame( Framedata framedata ) {
223223
getExtension().encodeFrame( framedata );
224224
if( WebSocketImpl.DEBUG )
225-
System.out.println( "afterEnconding(" + framedata.getPayloadData().remaining() + "): {" + ( framedata.getPayloadData().remaining() > 1000 ? "too big to display" : new String( framedata.getPayloadData().array() ) ) + "}" );
225+
System.out.println( "afterEnconding(" + framedata.getPayloadData().remaining() + "): {" + ( framedata.getPayloadData().remaining() > 1000 ? "too big to display" : new String( framedata.getPayloadData().array() ) ) + '}' );
226226
return createByteBufferFromFramedata( framedata );
227227
}
228228

@@ -349,7 +349,7 @@ public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteExce
349349
getExtension().isFrameValid(frame);
350350
getExtension().decodeFrame(frame);
351351
if( WebSocketImpl.DEBUG )
352-
System.out.println( "afterDecoding(" + frame.getPayloadData().remaining() + "): {" + ( frame.getPayloadData().remaining() > 1000 ? "too big to display" : new String( frame.getPayloadData().array() ) ) + "}" );
352+
System.out.println( "afterDecoding(" + frame.getPayloadData().remaining() + "): {" + ( frame.getPayloadData().remaining() > 1000 ? "too big to display" : new String( frame.getPayloadData().array() ) ) + '}' );
353353
frame.isValid();
354354
return frame;
355355
}
@@ -541,14 +541,11 @@ public void processFrame( WebSocketImpl webSocketImpl, Framedata frame ) throws
541541
else
542542
webSocketImpl.flushAndClose( code, reason, false );
543543
}
544-
return;
545544
} else if( curop == Framedata.Opcode.PING ) {
546545
webSocketImpl.getWebSocketListener().onWebsocketPing( webSocketImpl, frame );
547-
return;
548546
} else if( curop == Framedata.Opcode.PONG ) {
549547
webSocketImpl.updateLastPong();
550548
webSocketImpl.getWebSocketListener().onWebsocketPong( webSocketImpl, frame );
551-
return;
552549
} else if( !frame.isFin() || curop == Framedata.Opcode.CONTINUOUS ) {
553550
if( curop != Framedata.Opcode.CONTINUOUS ) {
554551
if( current_continuous_frame != null )
@@ -591,7 +588,6 @@ public void processFrame( WebSocketImpl webSocketImpl, Framedata frame ) throws
591588
if( curop == Framedata.Opcode.CONTINUOUS && current_continuous_frame != null ) {
592589
byteBufferList.add( frame.getPayloadData() );
593590
}
594-
return;
595591
} else if( current_continuous_frame != null ) {
596592
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence not completed." );
597593
} else if( curop == Framedata.Opcode.TEXT ) {
@@ -600,7 +596,6 @@ public void processFrame( WebSocketImpl webSocketImpl, Framedata frame ) throws
600596
} catch ( RuntimeException e ) {
601597
webSocketImpl.getWebSocketListener().onWebsocketError( webSocketImpl, e );
602598
}
603-
return;
604599
} else if( curop == Framedata.Opcode.BINARY ) {
605600
try {
606601
webSocketImpl.getWebSocketListener().onWebsocketMessage( webSocketImpl, frame.getPayloadData() );

src/main/java/org/java_websocket/framing/FramedataImpl1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void append(Framedata nextframe) {
155155

156156
@Override
157157
public String toString() {
158-
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", rsv1:" + isRSV1() + ", rsv2:" + isRSV2() + ", rsv3:" + isRSV3() + ", payloadlength:[pos:" + unmaskedpayload.position() + ", len:" + unmaskedpayload.remaining() + "], payload:" + ( unmaskedpayload.remaining() > 1000 ? "(too big to display)" : new String( unmaskedpayload.array() ) ) + "}";
158+
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", rsv1:" + isRSV1() + ", rsv2:" + isRSV2() + ", rsv3:" + isRSV3() + ", payloadlength:[pos:" + unmaskedpayload.position() + ", len:" + unmaskedpayload.remaining() + "], payload:" + ( unmaskedpayload.remaining() > 1000 ? "(too big to display)" : new String( unmaskedpayload.array() ) ) + '}';
159159
}
160160

161161
/**

src/main/java/org/java_websocket/handshake/ClientHandshake.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public interface ClientHandshake extends Handshakedata {
3030
* returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2
3131
* @return the HTTP Request-URI
3232
*/
33-
public String getResourceDescriptor();
33+
String getResourceDescriptor();
3434
}

0 commit comments

Comments
 (0)