Java needs an immutable byte array wrapper

Keith Turner keith at
Sat Nov 12 17:16:37 UTC 2016

On Sat, Nov 12, 2016 at 12:04 PM, Roman Kennke <roman at> wrote:
> Am Samstag, den 12.11.2016, 11:45 -0500 schrieb Keith Turner:
>> While trying to design an API for Fluo, its become clear to me that
>> Java could really benefit from an immutable byte array wrapper.
>> Something like java.lang.String except for byte arrays instead of
>> char
>> arrays.  It would be nice if this new type interoperated well with
>> byte[], String, ByteBuffer, InputStream, OutputStream etc.
>> I wrote the following blog post about my experiences with this issue
>> while designing an API for Fluo.
>> Is there any reason something like this should not be added to Java?
> You mean something like NIO ByteBuffers and related APIs?

As I discussed in the blog post, ByteBuffers does not fit the bill of
what I need.  In the blog post I have the following little program as
an example to show that ByteBuffer is not immutable in the way String

  byte[] bytes1 = new byte[] {1,2,3,(byte)250};
  ByteBuffer bb1 = ByteBuffer.wrap(bytes1).asReadOnlyBuffer();


Would not want to use ByteBuffer as a map key.  Would be nice if Java
had something like ByteString[1] or Bytes[2].  Having a type like that
in Java would allow to be used in library APIs and avoid copies
between multiple implementations of an immutable byte array wrapper.


> Roman

More information about the discuss mailing list