Hi, On Tue, Oct 20, 2015 at 2:57 PM, Pavel Rappo <pavel.ra...@oracle.com> wrote: > Hi Joakim, > >> On 20 Oct 2015, at 13:50, Joakim Erdfelt <joakim.erdf...@gmail.com> wrote: >> >> You know that CharBuffer doesn't actually do UTF8, right? >> It's just a ByteBuffer split into equal 2 byte segments. >> CharBuffer is a way to obtain char (the 2 byte number, not the character) >> from the ByteBuffer or String you created it from. >> CharBuffer is functionally no different than ShortBuffer. > > Yes, I'm fully aware of the difference between > java.nio.ByteBuffer#asCharBuffer > and java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer). > > What's your point?
So why choosing CharBuffer ? The only thing you can do with it is to copy, one more time and with no need to, the chars it contains before using them. Providing a CharSequence, instead, leaves the implementation much more room and has the potential to avoid unnecessary copies. The implementation could synthesize the bytes into a String, and pass that to the application without further copies (with some JDK help). Point being, if you use CharSequence, you will have much more freedom in the implementation, and you will not painting yourself in a corner with CharBuffer. Today you can use charset encoders, tomorrow you may use something more optimized that does not need CharBuffer. -- Simone Bordet http://bordet.blogspot.com --- Finally, no matter how good the architecture and design are, to deliver bug-free software with optimal performance and reliability, the implementation technique must be flawless. Victoria Livschitz