public static long getVarInt(byte[] varInt) {
	long result=0;
	if (varInt.length==0) {
		 return result;
	}
	int unsignedByte=varInt[0] & 0xFF;
	if (unsignedByte<0xFD) {
		return unsignedByte;
	}
	int intSize=0;
	if (unsignedByte==0xFD) { 
		intSize=3;
	}
	else if (unsignedByte==0xFE) {
		intSize=5;
	}
	else if (unsignedByte==0XFF) {
		intSize=9;
	}
	byte[] rawDataInt=reverseByteArray(Arrays.copyOfRange(varInt, 1, intSize));
	ByteBuffer byteBuffer = ByteBuffer.wrap(rawDataInt);
	if (intSize==3) {
		 result=byteBuffer.getShort();
	}
	else if (intSize==5) {
		result=byteBuffer.getInt();
	}
	else if (intSize==9) {
		result=byteBuffer.getLong(); // Need to handle sign - available only in JDK8
	}
	return result;
}
public static long getVarInt ( byte [ ] varInt ) { long result = 0 ; if ( varInt . length == 0 ) { return result ; } int unsignedByte = varInt [ 0 ] & 0xFF ; if ( unsignedByte < 0xFD ) { return unsignedByte ; } int intSize = 0 ; if ( unsignedByte == 0xFD ) { intSize = 3 ; } else if ( unsignedByte == 0xFE ) { intSize = 5 ; } else if ( unsignedByte == 0XFF ) { intSize = 9 ; } byte [ ] rawDataInt = reverseByteArray ( Arrays . copyOfRange ( varInt , 1 , intSize ) ) ; ByteBuffer byteBuffer = ByteBuffer . wrap ( rawDataInt ) ; if ( intSize == 3 ) { result = byteBuffer . getShort ( ) ; } else if ( intSize == 5 ) { result = byteBuffer . getInt ( ) ; } else if ( intSize == 9 ) { result = byteBuffer . getLong ( ) ; } return result ; }
Converts a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) to long

@param varInt byte array containing variable length integer

@return long corresponding to variable length integer