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
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
Read and Write primitives to byte arrays
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?
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?
It's too bad to check your article late. I wonder what it would be if we met a little faster. I want to exchange a little more, but please visit my site 온라인카지노 and leave a message!!
ReplyDeleteThank you.. Get the Azerbaijan electronic visa through online azerbaijan electronic visa application to travel to Azerbaijan. Just follow 3 steps, fill application, upload document and make online payment for Azerbaijan e visa.
ReplyDeleteสล็อตเเตกง่ายที่สุดเว็บสล็อต ambbets.com เว็บสล็อตที่ดีที่สุด สล็อตค่ายใหญ่ 20 ค่ายชั้นนำ เล่นได้ทุกเกม รวมไว้ให้ในเว็บเดียว ลูกค้าสามารถเล่นได้ทุกเกม อัพเดทใหม่ก่อนใคร เกมสล็อตลิขสิทธิ์เเท้ 100% เติมเงินเล่นสล็อต รับโบนัสฟรี เครดิตฟรีไม่อั้น กดรับได้เอง มีโปรให้เลือกเยอะที่สุด ฝากถอนอัตโนมัติ รวดเร็ว ปลดภัย 100%
ReplyDeleteเว็บตรง เว็บใหญ่ ปลอดภัย มั่นคงเล่นง่าย ได้เงินจริง จ่ายชัวร์ ไม่ผ่านเอเย่นต์ ไม่ต้องทำเทิร์น ไม่ต้องทำยอด การันตีความชัวร์คนเล่นเยอะที่สุดเว็บคาสิโนออนไลน์ ที่ดีที่สุดสำหรับนักเดิมพัน เว็บตรง
ReplyDelete