2525
2626package org .java_websocket .util ;
2727
28- import java .io .UnsupportedEncodingException ;
2928import java .nio .ByteBuffer ;
3029import java .nio .charset .CharacterCodingException ;
31- import java .nio .charset .Charset ;
3230import java .nio .charset .CharsetDecoder ;
3331import java .nio .charset .CodingErrorAction ;
32+ import java .nio .charset .StandardCharsets ;
3433import org .java_websocket .exceptions .InvalidDataException ;
35- import org .java_websocket .exceptions .InvalidEncodingException ;
3634import org .java_websocket .framing .CloseFrame ;
3735
3836public class Charsetfunctions {
@@ -49,42 +47,30 @@ private Charsetfunctions() {
4947 * @return UTF-8 encoding in bytes
5048 */
5149 public static byte [] utf8Bytes (String s ) {
52- try {
53- return s .getBytes ("UTF8" );
54- } catch (UnsupportedEncodingException e ) {
55- throw new InvalidEncodingException (e );
56- }
50+ return s .getBytes (StandardCharsets .UTF_8 );
5751 }
5852
5953 /*
6054 * @return ASCII encoding in bytes
6155 */
6256 public static byte [] asciiBytes (String s ) {
63- try {
64- return s .getBytes ("ASCII" );
65- } catch (UnsupportedEncodingException e ) {
66- throw new InvalidEncodingException (e );
67- }
57+ return s .getBytes (StandardCharsets .US_ASCII );
6858 }
6959
7060 public static String stringAscii (byte [] bytes ) {
7161 return stringAscii (bytes , 0 , bytes .length );
7262 }
7363
7464 public static String stringAscii (byte [] bytes , int offset , int length ) {
75- try {
76- return new String (bytes , offset , length , "ASCII" );
77- } catch (UnsupportedEncodingException e ) {
78- throw new InvalidEncodingException (e );
79- }
65+ return new String (bytes , offset , length , StandardCharsets .US_ASCII );
8066 }
8167
8268 public static String stringUtf8 (byte [] bytes ) throws InvalidDataException {
8369 return stringUtf8 (ByteBuffer .wrap (bytes ));
8470 }
8571
8672 public static String stringUtf8 (ByteBuffer bytes ) throws InvalidDataException {
87- CharsetDecoder decode = Charset . forName ( "UTF8" ) .newDecoder ();
73+ CharsetDecoder decode = StandardCharsets . UTF_8 .newDecoder ();
8874 decode .onMalformedInput (codingErrorAction );
8975 decode .onUnmappableCharacter (codingErrorAction );
9076 String s ;
0 commit comments