import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.ParseException;
class Main{
StringBuilder sb1 = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
for (int i=0;i<15;i++) {
sb1.append(sb1);
}
System.
out.
println("Size of buffer: "+sb1.
length()); byte[] src = sb1.toString().getBytes("UTF-8");
StringBuilder res = null;
long startTime
= System.
currentTimeMillis(); for (int i=0;i<100;i++)
res = testStringConvert(src);
System.
out.
println("Conversion using String time (msec): "+(System.
currentTimeMillis()-startTime
)); if (!res.toString().equals(sb1.toString())) {
System.
err.
println("Conversion error"); }
startTime
= System.
currentTimeMillis(); for (int i=0;i<100;i++)
res = testCBConvert(src);
System.
out.
println("Conversion using CharBuffer time (msec): "+(System.
currentTimeMillis()-startTime
)); if (!res.toString().equals(sb1.toString())) {
System.
err.
println("Conversion error"); }
}
StringBuilder b = new StringBuilder(s);
return b;
}
private static StringBuilder testCBConvert(byte[] src) throws CharacterCodingException {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer srcBuffer = ByteBuffer.wrap(src);
CharBuffer resBuffer = decoder.decode(srcBuffer);
StringBuilder b = new StringBuilder(resBuffer);
return b;
}
}