-
Notifications
You must be signed in to change notification settings - Fork 0
Boon Byte Builder Round 2 (read and write primitives to a byte array)
Boon adds slice notation and a few other tricks to Java. If you are new to boon start here:
- Java Boon Byte Buffer Builder
- Java Boon Slice Notation
- Java Boon Slice's work with TreeSets
- Java Boon Description
If you are new to Java Boon and the BBBB Boon Byte Buffer Builder then you need to start with this:Java Boon Byte Buffer Builder
boolean works = true;
byte[] bytes = bytes(new byte[]{0x01, 0x02, 0x03});
bytes = add(bytes, (byte)0x04);
works |=
bytes[3] == 0x04 || die("byte 3 not 0x04");
/* Add an int and read it back. */
bytes = addInt(bytes, 1);
works |=
len( bytes ) == 8 || die("length should be 8");
works |=
idxInt( bytes, 4 ) == 1 || die("read int back as 1");
/* Write and read in a Long. */
bytes = addLong(bytes, 0xFFFEFAFBFCL);
works |=
len( bytes ) == 16 || die("length should be 16");
works |=
idxLong( bytes, 8 ) == 0xFFFEFAFBFCL
|| die("read int back as 0xFFFEFAFBFCL");
/* Write and read in a Short. */
bytes = addShort(bytes, (short)0x0FED);
works |=
len( bytes ) == 18 || die("length should be 18");
works |=
idxShort( bytes, 16 ) == 0x0FED
|| die("read short back as 0x0FED");
/* Write and read in a char. */
bytes = addChar(bytes, 'a');
works |=
len( bytes ) == 20 || die("length should be 20");
works |=
idxChar( bytes, 18 ) == 'a' || die("read char back as 'a'");
/* Write and read in a float. */
bytes = addFloat(bytes, 99.00f);
works |=
len( bytes ) == 24 || die("length should be 24");
works |=
idxFloat( bytes, 20 ) == 99.00f
|| die("read float back as 99.00f");
By the way this syntax:
works |=
bytes[3] == 0x04 || die("byte 3 not 0x04");
Works a lot like this:
assert bytes[3] == 0x04 : "byte 3 not 0x04";
The big difference is that the latter can be disable which is not what I wanted for my unit tests. (I got sick of assertTrue().)
There is also an insert (that grows the array), and insert/replace.
//Insert and grow
public static byte[] insertIntInto(byte[] array, int index, int v) {...
//Insert into array
public static void intTo(byte[] b, int off, int val) {...
//Add int to array (and grow it)
public static byte[] addInt(byte[] array, int v) {...
//Add int to array (and grow it)
public static byte[] addInts(byte[] array, int[] values) {...
Repeat the above for long, short, char, float and double.
I am considering adding BigDecimal, BigInteger, StringBuilder, String, Date.
Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).
If you are new to boon start here:
- Java Boon Byte Buffer Builder
- Java Boon Slice Notation
- Java Boon Slice's work with TreeSets
- Java Boon Description
- More...
- Boon Home
- Boon Source
- Introducing Boon October 2013
- Java Slice Notation
- What if Java collections were easy to search and sort?
- Boon HTTP utils
- Boon Java JSON parser Benchmarks or hell yeah JSON parsing is damn fast!
- Boon JSON parser is really damn fast! Part II
- Boon JSON parser Round III now just not fast as but much faster than other Java JSON parsers
- Boon World's fastest Java JSON parser Round IV from fast to blazing to rocket fuel aka Braggers going to brag
- Boon gets adopted by JSON Path as the default Java JSON parser
- Boon graphics showing just how fast Boon JSON parsing is - about 50% to 200% faster than the graphs shown here now so wicked fast became wickeder - just got sick of making graphics
- 10 minute guide to Boon JSON parsing after I added @JsonIgnore, @JsonProperty, @JsonView, @Exposes, etc.
- Hightower speaks to the master of Java JSON parsing, the king of speed The COW TOWN CODER!
- Boon provides easy Java objects from lists, from maps and from JSON.
Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.
Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.
===
Contact Info
blog|[twitter](https://twitter.com/RickHigh|[infoq]http://www.infoq.com/author/Rick-Hightower|[stackoverflow](http://stackoverflow.com/users/2876739/rickhigh)|[java lobby](http://java.dzone.com/users/rhightower)|Other | richard high tower AT g mail dot c-o-m (Rick Hightower)|work|cloud|nosql
YourKit supports Boon open source project with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .Net profiler.