/*
 * @(#)Volser.java
 *
 * Summary: Access Windows 32-bit volume serial number of a drive.
 *
 * Copyright: (c) 2007-2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.1+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2007-12-17 - initial release
 *  1.1 2008-09-23 - fix problem with Microsoft C++ runtime library
 */
package com.mindprod.volser;

/**
 * Access Windows 32-bit volume serial number of a drive.
 * <p/>
 * Uses JNI native C++/ASM code that only works on Vista, XP, Windows 2000.
 * Do not confuse this with the case-sensitive volume label.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1 2008-09-23 - fix problem with Microsoft C++ runtime library
 * @since 2007-12-17
 */
public final class Volser
    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * true if you want to include the debugging harness code.
     */
    private static final boolean DEBUGGING = true;

    /**
     * undisplayed embedded copyright notice
     */
    public static final String EMBEDDED_COPYRIGHT =
            "copyright (c) 2007-2009 Roedy Green, Canadian Mind Products, http://mindprod.com";

    /**
     * undisplayed embedded release date
     */
    private static final String RELEASE_DATE = "2008-09-23";

    /**
     * undisplayed embedded version string.
     */
    public static final String VERSION_STRING = "1.1";
    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * Get the volume serial number.
     *
     * @param rootPath drive with trailing colon and backslash e.g. "C:\\". Can also be a network drive.
     *
     * @return 32-bit volume serial number as an int. Normally displayed in hex in two pieces separated by a colon or
     *         dash.
     */
    public static native int getVolser( String rootPath );

    // -------------------------- STATIC METHODS --------------------------

    static
        {
        // get DLL loaded from somewhere on java.library path.
        System.loadLibrary( "nativevolser" );
        // if have troubles change this code to use
        // System.load( "E:\\com\\mindprod\\volser\\nativevolser.dll" );
        }

    // --------------------------- main() method ---------------------------

    /**
     * test debug harness
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            final int volser = Volser.getVolser( "C:\\" );
            System.out.println( Integer.toHexString( volser ) );
            final int high = volser >>> 16;
            final int low = volser & 0xffff;
            // This code does not apply lead zeros on each half.
            // See http://mindprod.com/jgloss/hex.html for how to add that refinement.
            final String hexVolser = Integer.toHexString( high ) + ":" + Integer.toHexString( low );
            System.out.println( "4-byte Volume serial number for drive C: is " + hexVolser );
            }
        }
    }