Skip to content

Commit 1bafce4

Browse files
committed
Merged branch 'master' of TooTallNate/Java-WebSocket
2 parents fa0eaa3 + bbf49f2 commit 1bafce4

File tree

7 files changed

+38
-26
lines changed

7 files changed

+38
-26
lines changed

README.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ connect to. Important events `onOpen`, `onClose`, `onMessage` and `onIOError`
6464
get fired throughout the life of the WebSocketClient, and must be implemented
6565
in **your** subclass.
6666

67+
Minimum Required JDK
68+
--------------------
69+
70+
`Java-WebSocket` is known to work with:
71+
72+
* Java 1.4 (aka SE 6)
73+
* Android 1.6 (API 4)
74+
75+
Other JRE implementations may work as well, but haven't been tested.
76+
6777
Testing in Android Emulator
6878
---------------------------
6979

dist/WebSocket.jar

1.05 KB
Binary file not shown.

src/net/tootallnate/websocket/Draft.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.tootallnate.websocket;
22

33
import java.nio.ByteBuffer;
4-
import java.nio.charset.Charset;
54
import java.util.Collections;
65
import java.util.Iterator;
76
import java.util.List;
@@ -24,11 +23,8 @@ public enum HandshakeState {
2423
// ,/**Can not yet say anything*/
2524
// PENDING not yet in use
2625
}
27-
/**
28-
* The WebSocket protocol expects UTF-8 encoded bytes.
29-
*/
30-
public static final Charset UTF8_CHARSET = Charset.forName( "UTF-8" );
31-
public static final Charset ASCII_CHARSET = Charset.forName( "US-ASCII" );
26+
27+
private static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>" );
3228

3329
public static ByteBuffer readLine( ByteBuffer buf ) {
3430
ByteBuffer sbuf = ByteBuffer.allocate( buf.remaining() );
@@ -42,14 +38,15 @@ public static ByteBuffer readLine( ByteBuffer buf ) {
4238
sbuf.limit( sbuf.position() - 2 );
4339
sbuf.position( 0 );
4440
return sbuf;
41+
4542
}
4643
}
4744
return null;
4845
}
4946

5047
public static String readStringLine( ByteBuffer buf ) {
5148
ByteBuffer b = readLine( buf );
52-
return b == null ? null : new String( b.array(), 0, b.limit(), ASCII_CHARSET );
49+
return b == null ? null : Charsetfunctions.stingAscii( b.array() );
5350
}
5451

5552
public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf ) throws InvalidHandshakeException {
@@ -118,7 +115,8 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
118115
bui.append( "\r\n" );
119116
}
120117
bui.append( "\r\n" );
121-
byte[] httpheader = bui.toString().getBytes( ASCII_CHARSET );
118+
byte[] httpheader = Charsetfunctions.asciiBytes( bui.toString() );
119+
122120
byte[] content = withcontent ? handshakedata.getContent() : null;
123121
ByteBuffer bytebuffer = ByteBuffer.allocate( ( content == null ? 0 : content.length ) + httpheader.length );
124122
bytebuffer.put( httpheader );

src/net/tootallnate/websocket/FramedataImpl1.java

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

8181
@Override
8282
public String toString() {
83-
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", masked:" + getTransfereMasked() + ", payloadlength:" + unmaskedpayload.limit() + "}";
83+
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", masked:" + getTransfereMasked() + ", payloadlength:" + unmaskedpayload.limit() + ", payload:" + Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) + "}";
8484
}
8585

8686
}

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public enum Role {
9393

9494
public List<Draft> known_drafts;
9595

96-
private static final byte[] FLASH_POLICY_REQUEST = "<policy-file-request/>".getBytes( Draft.UTF8_CHARSET );
96+
private static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>" );
9797

9898
private int flash_policy_index = 0;
99-
99+
100100
SocketChannel sockchannel;
101101

102102
// CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -111,12 +111,12 @@ public enum Role {
111111
* The {@link WebSocketListener} to notify of events when
112112
* they occur.
113113
*/
114-
public WebSocket( WebSocketListener listener , Draft draft, SocketChannel sockchannel ) {
114+
public WebSocket( WebSocketListener listener , Draft draft , SocketChannel sockchannel ) {
115115
init( listener, draft, sockchannel );
116116
}
117117

118-
public WebSocket( WebSocketListener listener , List<Draft> drafts, SocketChannel sockchannel ) {
119-
init( listener, null, sockchannel );
118+
public WebSocket( WebSocketListener listener , List<Draft> drafts , SocketChannel sockchannel ) {
119+
init( listener, null, sockchannel );
120120
this.role = Role.SERVER;
121121
if( known_drafts == null || known_drafts.isEmpty() ) {
122122
known_drafts = new ArrayList<Draft>( 1 );
@@ -149,13 +149,14 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
149149
* @throws InterruptedException
150150
* @throws LimitExceededException
151151
*/
152-
public void handleRead( ) throws InterruptedException , IOException {
152+
public void handleRead() throws InterruptedException , IOException {
153153
if( !socketBuffer.hasRemaining() ) {
154154
socketBuffer.rewind();
155155
socketBuffer.limit( socketBuffer.capacity() );
156156
if( sockchannel.read( socketBuffer ) == -1 ) {
157157
close();
158158
}
159+
159160
socketBuffer.flip();
160161
}
161162

@@ -168,7 +169,7 @@ public void handleRead( ) throws InterruptedException , IOException {
168169

169170
handshakestate = isFlashEdgeCase( socketBuffer );
170171
if( handshakestate == HandshakeState.MATCHED ) {
171-
channelWrite( ByteBuffer.wrap( wsl.getFlashPolicy( this ).getBytes( Draft.UTF8_CHARSET ) ) );
172+
channelWrite( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
172173
return;
173174
}
174175
socketBuffer.mark();
@@ -253,7 +254,7 @@ else if( curop == Opcode.CLOSING ) {
253254
if( currentframe == null ) {
254255
if( f.isFin() ) {
255256
if( f.getOpcode() == Opcode.TEXT ) {
256-
wsl.onMessage( this, new String( f.getPayloadData(), Draft.UTF8_CHARSET ) );
257+
wsl.onMessage( this, Charsetfunctions.stingUtf8( f.getPayloadData() ) );
257258
} else if( f.getOpcode() == Opcode.BINARY ) {
258259
wsl.onMessage( this, f.getPayloadData() );
259260
} else {
@@ -271,7 +272,7 @@ else if( curop == Opcode.CLOSING ) {
271272
abort( "invalid frame: " + e.getMessage() );
272273
}
273274
if( f.isFin() ) {
274-
wsl.onMessage( this, new String( f.getPayloadData(), Draft.UTF8_CHARSET ) );
275+
wsl.onMessage( this, Charsetfunctions.stingUtf8( f.getPayloadData() ) );
275276
currentframe = null;
276277
}
277278
}
@@ -349,7 +350,7 @@ boolean hasBufferedData() {
349350
* @return True if all data has been sent to the client, false if there
350351
* is still some buffered.
351352
*/
352-
public void handleWrite( ) throws IOException {
353+
public void handleWrite() throws IOException {
353354
ByteBuffer buffer = this.bufferQueue.peek();
354355
while ( buffer != null ) {
355356
sockchannel.write( buffer );
@@ -402,17 +403,18 @@ private void open() throws InterruptedException , IOException {
402403
handshakeComplete = true;
403404
wsl.onOpen( this );
404405
}
405-
406-
public InetSocketAddress getRemoteSocketAddress( ){
406+
407+
public InetSocketAddress getRemoteSocketAddress() {
407408
return (InetSocketAddress) sockchannel.socket().getRemoteSocketAddress();
408409
}
409-
410-
public InetSocketAddress getLocalSocketAddress( ){
410+
411+
public InetSocketAddress getLocalSocketAddress() {
411412
return (InetSocketAddress) sockchannel.socket().getLocalSocketAddress();
412413
}
413414

414415
@Override
415416
public String toString() {
416417
return super.toString(); // its nice to be able to set breakpoints here
417418
}
419+
418420
}

src/net/tootallnate/websocket/drafts/Draft_10.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
import java.util.Random;
1010

1111
import net.tootallnate.websocket.Base64;
12+
import net.tootallnate.websocket.Charsetfunctions;
1213
import net.tootallnate.websocket.Draft;
1314
import net.tootallnate.websocket.FrameBuilder;
1415
import net.tootallnate.websocket.Framedata;
1516
import net.tootallnate.websocket.Framedata.Opcode;
1617
import net.tootallnate.websocket.FramedataImpl1;
1718
import net.tootallnate.websocket.HandshakeBuilder;
1819
import net.tootallnate.websocket.Handshakedata;
19-
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
2020
import net.tootallnate.websocket.exeptions.InvalidDataException;
21+
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
2122
import net.tootallnate.websocket.exeptions.LimitExedeedException;
2223

2324
public class Draft_10 extends Draft {
@@ -122,7 +123,7 @@ public List<Framedata> createFrames( byte[] binary, boolean mask ) {
122123
@Override
123124
public List<Framedata> createFrames( String text, boolean mask ) {
124125
FrameBuilder curframe = new FramedataImpl1();
125-
byte[] pay = text.getBytes( UTF8_CHARSET );
126+
byte[] pay = Charsetfunctions.utf8Bytes( text );
126127
curframe.setPayload( pay );
127128
curframe.setFin( true );
128129
curframe.setOptcode( Opcode.TEXT );

src/net/tootallnate/websocket/drafts/Draft_75.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.LinkedList;
66
import java.util.List;
77

8+
import net.tootallnate.websocket.Charsetfunctions;
89
import net.tootallnate.websocket.Draft;
910
import net.tootallnate.websocket.FrameBuilder;
1011
import net.tootallnate.websocket.Framedata;
@@ -70,7 +71,7 @@ public List<Framedata> createFrames( byte[] binary, boolean mask ) {
7071
@Override
7172
public List<Framedata> createFrames( String text, boolean mask ) {
7273
FrameBuilder frame = new FramedataImpl1();
73-
frame.setPayload( text.getBytes( UTF8_CHARSET ) );
74+
frame.setPayload( Charsetfunctions.utf8Bytes( text ) );
7475
frame.setFin( true );
7576
frame.setOptcode( Opcode.TEXT );
7677
frame.setTransferemasked( mask );

0 commit comments

Comments
 (0)