fluo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ctubbsii <...@git.apache.org>
Subject [GitHub] incubator-fluo-website pull request #41: Blog post about immutable bytes
Date Wed, 09 Nov 2016 21:28:55 GMT
Github user ctubbsii commented on a diff in the pull request:

    --- Diff: _posts/blog/2016-11-09-immutable-bytes.md ---
    @@ -0,0 +1,180 @@
    +title: "Java needs an immutable byte string"
    +date: 2016-11-09 11:43:00 +0000
    +author: Keith Turner
    +## Byte Sequences in Java
    +Working with byte arrays in Java can be painful.  To work around this Fluo created [Bytes]
    +[BytesBuilder].  Bytes is an immutable wrapper around a byte array.  Bytes has good implementations
    +of `hashCode()`, `equals()`, and `compareTo()`.  These functions and its immutability
make it
    +suitable to use as a map key.  If you have ever tried to use a byte array as a map key
in Java you
    +quickly realize that you need to write a wrapper class.
    +Fluo should not have to create these classes. Java really should offer something out
the box as part
    +of its standard library.  However it does not offer any good class for this common case.
    +## Why not use String?
    +Trying to stuff arbitrary binary data in a String can corrupt the data.  The following
    +program shows this, it will print `false`.
    +    byte bytes1[] = new byte[256];
    +    for(int i = 0; i<255; i++)
    +      bytes1[i] = (byte)i;
    +    byte bytes2[] = new String(bytes1).getBytes();
    +    System.out.println(Arrays.equals(bytes1, bytes2));
    +String can be made to work by specifying a character set. The following program will
print `true`.
    +However this is error prone and inefficient.  Using this method results in copying between
byte arrays
    --- End diff --

If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.

View raw message