The java.io.UnsupportedEncodingException occurs when an unsupported character encoding scheme is used in java strings or bytes. The java String getBytes method converts the requested string to bytes in the specified encoding format. If the java does not support the encoding format, the method String getBytes throws java.io.UnsupportedEncodingException with the encoding format given.
The character encoding is used to determine how the raw binary is to be interpreted to character. The default encoding for English Windows systems in CP1252. Other languages and systems can use a different default encoding. The UTF-8 encoding scheme is generally used as a character encoding scheme. In java, String.getBytes() and StringCoding.encode() methods are used to interpret between raw bytes and java strings.
Exception
The java.io.UnsupportedEncodingException will be thrown as a stack trace below. The exception java.io.UnsupportedEncodingException shows the name of the encoding scheme used to interpret between bytes and character.
Exception in thread "main" java.io.UnsupportedEncodingException: UTF
at java.lang.StringCoding.encode(StringCoding.java:341)
at java.lang.String.getBytes(String.java:918)
at com.yawintutor.StringGetBytes.main(StringGetBytes.java:8)
Root Cause
The given encoding format name in the String.getBytes method is incorrect or not supported by the java. The supported encoding formats are available in https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html. The UTF-8 encoding scheme is commonly used as a character encoding scheme.
How to reproduce this issue
The example below will throw java.io.UnsupportedEncodingException. The “UTF” encoding scheme is an invalid encoding scheme name. Java can not interpret the string to bytes if the encoding scheme is unknown or not supported. Java will throw java.io. UnsupportedEncodingException if an unknown or unsupported encoding method is identified.
package com.yawintutor;
public class StringGetBytes {
public static void main(String[] args) throws Exception {
String str = "Bangalore";
byte[] bytes;
bytes = str.getBytes("UTF");
System.out.println("Given String : " + str);
System.out.println("Output bytes : " + bytes);
}
}
Output
Exception in thread "main" java.io.UnsupportedEncodingException: UTF
at java.lang.StringCoding.encode(StringCoding.java:341)
at java.lang.String.getBytes(String.java:918)
at com.yawintutor.StringGetBytes.main(StringGetBytes.java:8)
Solution
The java supported encoding scheme name should be provided in String.getBytes method. The list of java supported methods are available in https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html. The CharsetEncoder
class should be used when more control over the encoding process is required. The String.getBytes method returns with array of bytes
package com.yawintutor;
public class StringGetBytes {
public static void main(String[] args) throws Exception {
String str = "Bangalore";
byte[] bytes;
bytes = str.getBytes("UTF-16");
System.out.println("Given String : " + str);
System.out.println("Output bytes : " + bytes);
}
}
Output
Given String : Bangalore
Output bytes : [B@7852e922