package com.sibilantsolutions.iptools.util;

import java.nio.ByteOrder;

public abstract class Convert
{

    private Convert() {}    //Prevent instantiation.

    static public long toNum( byte[] bytes, int offset, int length )
    {
        return toNum( bytes, offset, length, ByteOrder.BIG_ENDIAN );
    }

    static public long toNum( byte[] bytes, int offset, int length, ByteOrder byteOrder )
    {
        long num = 0;

        final int endIndex = offset + length;

        if ( byteOrder == ByteOrder.BIG_ENDIAN )
        {
            while ( offset < endIndex )
            {
                char b = (char)( bytes[offset++] & 0xFF );

                num <<= 8;

                num += b;
            }
        }
        else if ( byteOrder == ByteOrder.LITTLE_ENDIAN )
        {
            for ( int i = endIndex - 1; i >= offset; i-- )
            {
                char b = (char)( bytes[i] & 0xFF );

                num <<= 8;

                num += b;
            }
        }
        else
        {
            throw new IllegalArgumentException( "Unexpected byte order=" + byteOrder );
        }

        return num;
    }

}

@@ -38,16 +38,44 @@
 .util;%0A%0A
+import java.nio.ByteBuffer;%0A
 import j
@@ -181,16 +181,392 @@
 ation.%0A%0A
+    static public int byteToNum( byte b )%0A    %7B%0A        return b & 0xFF;%0A    %7D%0A%0A    static public int getByte( ByteBuffer bb )%0A    %7B%0A        return byteToNum( bb.get() );%0A    %7D%0A%0A    static public long toNum( ByteBuffer bb, int numBytes )%0A    %7B%0A        byte%5B%5D bytes = new byte%5BnumBytes%5D;%0A        bb.get( bytes );%0A%0A        return toNum( bytes, 0, numBytes, bb.order() );%0A    %7D%0A%0A
     stat

Add methods to work with retrieving bytes from ByteBuffer.