Skip to content

Commit 65dca5a

Browse files
committed
Merge pull request TooTallNate#55 from donpdonp/master
Avoids Android v2.3 helpers
2 parents fdc1bd5 + d701a89 commit 65dca5a

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

‎src/net/tootallnate/websocket/Draft.java‎

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

33
import java.nio.ByteBuffer;
4-
import java.nio.charset.Charset;
5-
import java.util.Arrays;
64
import java.util.Collections;
75
import java.util.Iterator;
86
import java.util.List;
@@ -26,9 +24,7 @@ public enum HandshakeState{
2624
/**
2725
* The WebSocket protocol expects UTF-8 encoded bytes.
2826
*/
29-
public static final Charset UTF8_CHARSET = Charset.forName ( "UTF-8" );
30-
31-
private static final byte[] FLASH_POLICY_REQUEST = "<policy-file-request/>".getBytes( UTF8_CHARSET );
27+
private static final byte[] FLASH_POLICY_REQUEST = WebSocket.utf8Bytes("<policy-file-request/>");
3228

3329
/**will return the index of the first \r\n or the index off the last element in arr*/
3430
public static int findNewLine( byte[] arr , int offset ) {
@@ -132,7 +128,7 @@ else if( ownrole == Role.SERVER ){
132128
bui.append ( "\r\n" );
133129
}
134130
bui.append ( "\r\n" );
135-
byte[] httpheader = bui.toString ().getBytes ( UTF8_CHARSET );
131+
byte[] httpheader = WebSocket.utf8Bytes( bui.toString ());
136132
byte[] content = withcontent ? handshakedata.getContent() : null;
137133
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length );
138134
bytebuffer.put ( httpheader );

‎src/net/tootallnate/websocket/FramedataImpl1.java‎

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

7777
@Override
7878
public String toString( ) {
79-
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", masked:" + getTransfereMasked() +", payloadlength:"+unmaskedpayload.limit()+", payload:" + new String ( unmaskedpayload.array () , Draft.UTF8_CHARSET ) + "}" ;
79+
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", masked:" + getTransfereMasked() +", payloadlength:"+unmaskedpayload.limit()+", payload:" + WebSocket.utf8Bytes(new String ( unmaskedpayload.array () )) + "}" ;
8080
}
8181

8282
}

‎src/net/tootallnate/websocket/WebSocket.java‎

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

33
import java.io.IOException;
4+
import java.io.UnsupportedEncodingException;
45
import java.nio.ByteBuffer;
56
import java.nio.channels.NotYetConnectedException;
67
import java.nio.channels.SocketChannel;
7-
import java.nio.charset.Charset;
88
import java.security.NoSuchAlgorithmException;
99
import java.util.ArrayList;
1010
import java.util.List;
@@ -42,8 +42,8 @@ public enum Role{
4242
public static final int DEFAULT_PORT = 80;
4343
/**
4444
* The WebSocket protocol expects UTF-8 encoded bytes.
45-
*/
46-
public final static Charset UTF8_CHARSET = Charset.forName ( "UTF-8" );
45+
*/
46+
public final static String UTF8 = "UTF-8";
4747
/**
4848
* The byte representing CR, or Carriage Return, or \r
4949
*/
@@ -176,7 +176,7 @@ else if ( bytesRead > 0) {
176176
if(DEBUG) System.out.println( "got: {" + new String( socketBuffer.array() , 0 , bytesRead ) + "}" );
177177
if( !handshakeComplete ){
178178
if(draft.isFlashEdgeCase( socketBuffer.array() , bytesRead )){
179-
channelWrite( ByteBuffer.wrap( wsl.getFlashPolicy( this ).getBytes( UTF8_CHARSET ) ) );
179+
channelWrite( ByteBuffer.wrap( utf8Bytes(wsl.getFlashPolicy( this )) ) );
180180
return;
181181
}
182182
try{
@@ -264,7 +264,7 @@ else if( curop == Opcode.PONG){
264264
if( currentframe == null){
265265
if( f.isFin () ){
266266
if( f.getOpcode () == Opcode.TEXT ){
267-
wsl.onMessage ( this , new String ( f.getPayloadData () , UTF8_CHARSET ) );
267+
wsl.onMessage ( this , new String ( f.getPayloadData (), UTF8 ) );
268268
}
269269
else if( f.getOpcode () == Opcode.BINARY ){
270270
wsl.onMessage ( this , f.getPayloadData () );
@@ -285,7 +285,7 @@ else if( f.getOpcode() == Opcode.CONTINIOUS ){
285285
abort( "invalid frame: " +e.getMessage () );
286286
}
287287
if( f.isFin () ){
288-
wsl.onMessage ( this , new String ( f.getPayloadData () , UTF8_CHARSET ) );
288+
wsl.onMessage ( this , new String ( f.getPayloadData (), UTF8 ) );
289289
currentframe = null;
290290
}
291291
}
@@ -416,4 +416,15 @@ public int getPort(){
416416
return socketChannel.socket().getLocalPort();
417417
}
418418

419+
/*
420+
* @return UTF-8 encoding in bytes
421+
*/
422+
public static byte[] utf8Bytes(String s) {
423+
try {
424+
return s.getBytes(UTF8);
425+
} catch (UnsupportedEncodingException e) {
426+
throw new RuntimeException("UTF-8 Charset not available");
427+
}
428+
}
429+
419430
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.tootallnate.websocket.FramedataImpl1;
1717
import net.tootallnate.websocket.HandshakeBuilder;
1818
import net.tootallnate.websocket.Handshakedata;
19+
import net.tootallnate.websocket.WebSocket;
1920
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
2021

2122

@@ -33,7 +34,7 @@ public int getPreferedSize(){
3334

3435
public static int readVersion(Handshakedata handshakedata){
3536
String vers = handshakedata.getFieldValue ( "Sec-WebSocket-Version" );
36-
if( !vers.isEmpty () ){
37+
if( vers.length() > 0 ){
3738
int v;
3839
try {
3940
v = new Integer ( vers.trim () );
@@ -125,7 +126,7 @@ public List<Framedata> createFrames( byte[] binary , boolean mask ) {
125126
@Override
126127
public List<Framedata> createFrames( String text , boolean mask ) {
127128
FrameBuilder curframe = new FramedataImpl1();
128-
byte[] pay = text.getBytes ( UTF8_CHARSET );
129+
byte[] pay = WebSocket.utf8Bytes(text);
129130
curframe.setPayload ( pay );
130131
curframe.setFin ( true );
131132
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
@@ -13,6 +13,7 @@
1313
import net.tootallnate.websocket.FramedataImpl1;
1414
import net.tootallnate.websocket.HandshakeBuilder;
1515
import net.tootallnate.websocket.Handshakedata;
16+
import net.tootallnate.websocket.WebSocket;
1617
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
1718

1819
public class Draft_75 extends Draft {
@@ -76,7 +77,7 @@ public List<Framedata> createFrames( byte[] binary , boolean mask ) {
7677
@Override
7778
public List<Framedata> createFrames( String text , boolean mask ) {
7879
FrameBuilder frame = new FramedataImpl1();
79-
frame.setPayload ( text.getBytes(UTF8_CHARSET) );
80+
frame.setPayload ( WebSocket.utf8Bytes(text) );
8081
frame.setFin ( true );
8182
frame.setOptcode ( Opcode.TEXT );
8283
frame.setTransferemasked ( mask );

0 commit comments

Comments
 (0)