Skip to main content

Posts

Endianness of Java

In Java, multibyte data items or streams are stored in big endian order. Using Java to write bytes directly to the wire will require byte-order transformation if the recipient talks in little endian. The same is true if you are reading directly from the wire from a sender who talks in little endian. If you are using a byte array wrapped in ByteBuffer, byte[] data = new byte[] { 12, 19, 83, 05, -05, 55, -55 }; ByteBuffer byteBuffer = ByteBuffer.wrap(data); Transforming from big endian to little endian is easy, byteBuffer.order(java.nio.ByteOrder.LITTLE_ENDIAN); If you are unsure of the endianness of the underlying platform, just invoke the native order method. java.nio.ByteOrder.nativeOrder(); Aside from streams, operands in classes that are above 1 byte in size are in big endian order.

Java Lambda Expressions / Closures

Lambda expressions are just (anonymous) functions. f(x) = x can be expressed as x→x f(x, y) = x*x + y*y can be expressed as x, y→x*x + y*y In simple Java terms, 1. Return an int 23 with a void argument int j = { => 23 }.invoke(); 2. Closure with 1 argument, convert 23 kilograms to pounds double j = { double kilograms => kilograms*2.2D }.invoke(23); 3. Closure with 2 arguments, get the sum of the 2 squares int squareSum = { int x, int y => (x*x) + (y*y) }.invoke(2,3); 4. Closure returning an instance and invoking an instance method { new RocketLauncher() }.invoke().fire(); Every time a closure is created at compile time, an interface is automatically created for each expression having an invoke method with a signature depending on the number of arguments, then an anonymous subclass of this interface is constructed. Enjoy!

Tips for Reading Source Code

Reading time is longer than the writing time and most programmers will lose focus along the way (some even fall asleep) especially if they are reading monstrous legacy code. There are several reasons why, in no particular order, 1. Procrastination Programmer: [Wandering mind… Facebook… Twitter…] “Oh man, I have a brilliant idea, I’m going to read more on that… I’ll set aside this crap first and try to come up something innovative.” 2. Impatient Programmer: “I hate this crap, ugly legacy code, it sucks! Can I just have the documents and rewrite everything from scratch?” Project Manager: “Unfortunately, the source code is the only document we have.” 3. Interruptions Team mate: “Hey! Can I have 5 seconds from you? I’m getting a NullPointerException, this is weird, I had everything initialized!” Programmer: “Are you sure? Hold on, let me check that, you might have some methods returning nul...

Installing Go in Ubuntu Jaunty

Processor: x86 Operating System: Ubuntu Linux 9.04 Jaunty Jackalope 1. CONFIGURE THE ENVIRONMENT Set the necessary variables in your .bashrc that is if you are using bash. Assuming you have a 386-descendant processor and you want to keep the installation clean in an external disk (/media/disk). # Google Go Programming Language Settings export GOROOT=/media/disk/go export GOARCH=386 export GOOS=linux export GOBIN=/media/disk/go/bin 2. INSTALL MERCURIAL. Easy. $ sudo easy_install mercurial Install process, Searching for mercurial Reading http://pypi.python.org/simple/mercurial/ Reading http://www.selenic.com/mercurial Best match: mercurial 1.3.1 Downloading http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz Processing mercurial-1.3.1.tar.gz Running mercurial-1.3.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-a3YaRd/mercurial-1.3.1/egg-dist-tmp-VyNqd2 zip_safe flag not set; analyzing archive contents... mercurial.lsprof: module references __file__ mercurial.temp...