Skip to content

Commit 962f901

Browse files
committed
added strict utf8 decoding
1 parent 00749b1 commit 962f901

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

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

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

33
import java.io.UnsupportedEncodingException;
4+
import java.nio.ByteBuffer;
5+
import java.nio.charset.CharacterCodingException;
6+
import java.nio.charset.Charset;
7+
import java.nio.charset.CharsetDecoder;
8+
import java.nio.charset.CodingErrorAction;
49

510
public class Charsetfunctions {
611

12+
public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
13+
714
/*
815
* @return UTF-8 encoding in bytes
916
*/
@@ -34,19 +41,17 @@ public static String stingAscii( byte[] bytes ) {
3441
}
3542
}
3643

37-
public static String stingAscii( byte[] bytes, int offset, int length ) {
44+
public static String stingAscii( byte[] bytes, int offset, int length ){
3845
try {
3946
return new String( bytes, offset, length, "ASCII" );
4047
} catch ( UnsupportedEncodingException e ) {
4148
throw new RuntimeException( e );
4249
}
4350
}
4451

45-
public static String stingUtf8( byte[] bytes ) {
46-
try {
47-
return new String( bytes, "UTF8" );
48-
} catch ( UnsupportedEncodingException e ) {
49-
throw new RuntimeException( e );
50-
}
52+
public static String stingUtf8( byte[] bytes ) throws CharacterCodingException {
53+
CharsetDecoder encode = Charset.forName("UTF8").newDecoder();
54+
encode.onMalformedInput( codingErrorAction );
55+
return encode.decode( ByteBuffer.wrap( bytes ) ).toString();
5156
}
5257
}

0 commit comments

Comments
 (0)