Java 17 Recipes


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

 Solution 1
Use java.io.InputStreamReader to decode a byte stream to Unicode characters. Use 
java.io.OutputStreamWriter to encode Unicode characters to a byte stream.
The following code uses InputStreamReader to read and convert a potentially large 
block of text bytes from a file in the classpath. The org.java17recipes.chapter11 
.recipe11_07.StreamConversion class provides the complete code for this example.
public static void main(String[] args) {
StreamConversion app = new StreamConversion();
app.run();
}
public void run() {
try {
String input = readStream();
System.out.printf("Input stream: %s\n", input);
writeStream(input);
} catch (IOException ex) {
Logger.getLogger(StreamConversion.class.getName()).log(Level.
SEVERE, null, ex);
}
}
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


419
public String readStream() throws IOException {
InputStream is = getClass().getResourceAsStream("/resources/helloworld.
sjis.txt");
StringBuilder sb = new StringBuilder();
if (is != null) {
try (InputStreamReader reader =
new InputStreamReader(is, Charset.forName("SJIS"))) {
int ch = reader.read();
while (ch != -1) {
sb.append((char) ch);
ch = reader.read();
}
}
}
return sb.toString();
}
Similarly, you can use an OutputStreamWriter to write text to a byte stream. The 
following code writes a string to a UTF-8 encoded byte stream.
public void writeStream(String text) throws IOException {
FileOutputStream fos = new FileOutputStream("helloworld.utf8.txt");
try (OutputStreamWriter writer
= new OutputStreamWriter(fos, Charset.forName("UTF-8"))) {
writer.write(text);
}
}
 Solution 2
Use java.nio.charset.CharsetEncoder and java.nio.charset.CharsetDecoder to 
convert Unicode character buffers to and from byte buffers. Retrieve an encoder or 
decoder from a charset instance with the newEncoder() or newDecoder() method. 
Then use the encoder’s encode() method to create byte buffers. Use the decoder’s 
decode() method to create character buffers. The following code from the
org.java17recipes.chapter11.recipe11_07.BufferConversion class encodes
and decodes character sets from buffers.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


420
public static void main(String[] args) {
BufferConversion app = new BufferConversion();
app.run();
}
public void run() {
try {
System.out.printf("Original string: %s\n", unicodeString);
CharBuffer srcBuffer = CharBuffer.wrap(unicodeString);
ByteBuffer targetBytes = encodeBuffer("UTF8", srcBuffer);
printBytes(targetBytes);
CharBuffer roundtripBuffer = decodeBuffer("UTF8", targetBytes);
printCharBuffer(roundtripBuffer);
} catch (CharacterCodingException ex) {
Logger.getLogger(BufferConversion.class.getName()).log(Level.
SEVERE, null, ex);
}
}
public ByteBuffer encodeBuffer(String charsetName, CharBuffer charBuffer)
throws CharacterCodingException {
Charset charset = Charset.forName(charsetName);
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer targetBuffer = encoder.encode(charBuffer);
return targetBuffer;
}
public CharBuffer decodeBuffer(String charsetName, ByteBuffer srcBuffer)
throws CharacterCodingException {
Charset charset = Charset.forName(charsetName);
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(srcBuffer);
return charBuffer;
}
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


421

Download 3.2 Mb.

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




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