Java Reference
In-Depth Information
bBuffer.clear();
newBuffer.clear();
return newBuffer;
}
public String getText() {
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append("My horse moved on; hoof after hoof");
sb.append(newLine);
sb.append("He raised, and never stopped:");
sb.append(newLine);
sb.append("When down behind the cottage roof,");
sb.append(newLine);
sb.append("At once, the bright moon dropped.");
return sb.toString();
}
}
Listing 9-5. Charset Encoder and Decoder Using a DataSourceSink as a Data Supplier/Consumer for
Encoding/Decoding
// CharEncoderDecoder.java
package com.jdojo.nio;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
public class CharEncoderDecoder {
public static void main(String[] args) throws Exception {
DataSourceSink dss = new DataSourceSink();
// Display the text we are going to encode
System.out.println("Original Text:");
System.out.println(dss.getText());
System.out.println("--------------------");
// Encode the text using UTF-8 encoding. We will store
// encoded bytes in the dss object during the encoding process
encode(dss, "UTF-8");
// Decode bytes stored in the dss object using UTF-8 encoding
System.out.println("Decoded Text:");
decode(dss, "UTF-8");
}
Search WWH ::




Custom Search