Java 17 Recipes


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

 How It Works
The java.io and java.nio.charset packages contain several classes that can help you 
perform encoding conversions on large text streams or buffers. Streams are convenient 
abstractions that can assist you in converting text using a variety of sources and targets.
A stream can represent incoming or outgoing text in an HTTP connection or even a file.
If you use an InputStream to represent the underlying source text, you wrap that 
stream in an InputStreamReader to perform conversions from a byte stream. The reader 
instance performs the conversion from bytes to Unicode characters.
Using an OutputStream instance to represent the target text, wrap the stream in an 
OutputStreamWriter. A writer converts your Unicode text to a byte-oriented encoding in 
the target stream.
To effectively use either an OutputStreamWriter or an InputStreamReader, you 
must know the character encoding of your target or source text. When you use an 
OutputStreamWriter, the source text is always Unicode, and you must supply a charset 
argument to tell the writer how to convert to the target byte-oriented text encoding. 
When you use an InputStreamReader, the target encoding is always Unicode. You must 
supply the source text encoding as an argument so that the reader understands how to 
convert the text.
Note the Java platform’s String represents characters in the UtF-16 
encoding of Unicode. Unicode can have several encodings, including UtF-16, 
UtF-8, and even UtF-32. Converting to Unicode in this discussion always means 
converting to UtF-16. Converting to a byte-oriented encoding usually means 
a legacy non–Unicode charset encoding. however, a common byte-oriented 
encoding is UtF-8, and it is entirely reasonable to convert Java’s “native” 
UtF-16 Unicode characters to or from UtF-8 using the InputStreamReader or 
OutputStreamWriter class.
Yet another way to perform encoding conversions is to use the CharsetEncoder 
and CharsetDecoder classes. CharsetEncoder encodes your Unicode CharBuffer 
instances to ByteBuffer instances. CharsetDecoder decodes ByteBuffer instances into 
CharBuffer instances. In either case, you must provide a charset argument.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


422
11-8. Summary
Internationalization is a key to developing culturally responsive applications. It 
allows application text to be changed to adhere to the culture and language in which 
the application is being used. This chapter provided some examples of how to use 
internationalization techniques to overcome the nuances of cross-culture development.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


423
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_12

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   210   211   212   213   214   215   216   217   ...   245




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