Java needs an immutable byte array wrapper
Keith Turner
keith at deenlo.com
Sat Nov 12 18:46:37 UTC 2016
On Sat, Nov 12, 2016 at 12:53 PM, Peter Lawrey <peter.lawrey at gmail.com> wrote:
> Java 9 String has a byte [] at its core. I suspect it's not appropriate but
> worth thinking about.
I am not sure, I would have to look into it.
Would there always be a conversions to/from char when creating Strings
from byte[] and when calling String.getBytes()? Also would like
something that interoperates well with ByteBuffer, InputStream,
OutputStream for byte sequence data, like protobuf's ByteString and
Fluo's Bytes do.
>
> We have a BytesStore class which wraps bytes on or off heap which can be
> used for keys.
I suspect many project roll their own thing for this.
>
>
> On 12 Nov 2016 17:17, "Keith Turner" <keith at deenlo.com> wrote:
>>
>> On Sat, Nov 12, 2016 at 12:04 PM, Roman Kennke <roman at kennke.org> 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.
>> >>
>> >> http://fluo.apache.org/blog/2016/11/10/immutable-bytes/
>> >>
>> >> 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
>> is.
>>
>> byte[] bytes1 = new byte[] {1,2,3,(byte)250};
>> ByteBuffer bb1 = ByteBuffer.wrap(bytes1).asReadOnlyBuffer();
>>
>> System.out.println(bb1.hashCode());
>> bytes1[2]=89;
>> System.out.println(bb1.hashCode());
>> bb1.get();
>> System.out.println(bb1.hashCode());
>>
>> 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.
>>
>> [1]:
>> https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/ByteString
>> [2]:
>> https://static.javadoc.io/org.apache.fluo/fluo-api/1.0.0-incubating/org/apache/fluo/api/data/Bytes.html
>>
>> >
>> > Roman
>> >
More information about the discuss
mailing list