RFR: JDK-8021560,(str) String constructors that take ByteBuffer

Stuart Marks stuart.marks at oracle.com
Fri Feb 16 21:56:28 UTC 2018

On 2/16/18 6:14 AM, Alan Bateman wrote:
> On 15/02/2018 21:55, Stuart Marks wrote:
>> I'd also suggest adding a CharBuffer constructor:
>>     public String(CharBuffer cbuf)
>> This would be semantically equivalent to
>>     public String(char[] value, int offset, int count)
>> except using the chars from the CharBuffer between the buffer's position and 
>> its limit.

> CharBuffer::toString already does this so adding this constructor may not be as 
> useful as it initially looks.

Seems like an argument that this function is in the wrong location.

(Not joking.)

I think CharBuffer.toString() is actually quite obscure. I note that most of the 
Buffer classes have toString() methods that report the *status* of the buffer, e.g.

     java.nio.HeapByteBuffer[pos=0 lim=1000 cap=1000]

Compared to other Buffers' toString() methods, CharBuffer is the outlier: its 
toString() produces only the contents of the CharBuffer without any 
meta-information. This is rather surprising.


More information about the core-libs-dev mailing list