Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet212/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   208   209   210   211   212   213   214   215   ...   245
Bog'liq
Java 17 Recipes

 How It Works
The Java platform provides conversion support for many legacy character set encodings. 
When you create a String instance from a byte array, you must provide a charset 
argument to the String constructor so that the platform knows how to perform the 
mapping from the legacy encoding to Unicode. All Java strings use Unicode as their 
native encoding.
The number of bytes in the original array does not usually equal the number of 
characters in the result string. In this recipe’s example, the original array contains 18 
bytes. The Shift-JIS encoding needs the 18 bytes to represent the Japanese text. However, 
after conversion, the result string contains nine characters. There is not a 1:1 relationship 
between bytes and characters. Each character requires two bytes in the original Shift-JIS 
encoding in this example.
There are hundreds of different charset encodings. The number of encodings is 
dependent on your Java platform implementation. However, you are guaranteed support 
of several of the most common encodings, and your platform most likely contains many 
more than this minimal set.
US-ASCII
• ISO-8859-1
• UTF-8
• UTF-16BE
• UTF-16LE
• UTF-16
When constructing a charset, you should be prepared to handle the possible 
exceptions when the character set is not supported.
• java.nio.charset.IllegalCharsetNameException, thrown when 
the charset name is illegal
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


418
• java.lang.IllegalArgumentException, thrown when the charset 
name is null
• java.nio.charset.UnsupportedCharsetException, thrown when 
your JVM doesn’t support the targeted charset
11-7. Converting Character Streams and Buffers
 Problem
You need to convert large blocks of Unicode character text to and from an arbitrary byte-
oriented encoding. Large blocks of text may come from streams or files.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   208   209   210   211   212   213   214   215   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling