11package net .tootallnate .websocket ;
22
33import java .io .IOException ;
4+ import java .io .UnsupportedEncodingException ;
45import java .nio .ByteBuffer ;
56import java .nio .channels .NotYetConnectedException ;
67import java .nio .channels .SocketChannel ;
7- import java .nio .charset .Charset ;
88import java .security .NoSuchAlgorithmException ;
99import java .util .ArrayList ;
1010import 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}
0 commit comments