/*
 * [TestAdler.java]
 *
 * Summary: Compute Adler 32-bit checksum digest.
 *
 * Copyright: (c) 2009-2017 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.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import com.mindprod.common18.EIO;

import java.io.UnsupportedEncodingException;
import java.util.zip.Adler32;

import static java.lang.System.*;

/**
 * Compute Adler 32-bit checksum digest.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestAdler
    {
    /**
     * Calc 32 bit Adler digest of an array of bytes.
     *
     * @param theTextToDigestAsBytes byte array to compute checksum on
     *
     * @return 32-bit signed Adler checksum
     */
    private static int digest( byte[] theTextToDigestAsBytes )
        {
        final Adler32 digester = new Adler32();
        digester.update( theTextToDigestAsBytes );
        // digester.update( int ) processes only the low order 8-bits. It actually expects an unsigned byte.
        // getValue produces a long to conform to the Checksum interface.
        // Actual result is 32 bits long not 64.
        return ( int ) digester.getValue();
        }

    /**
     * Test harness
     *
     * @param args not used
     *
     * @throws java.io.UnsupportedEncodingException in case UTF-8 support is missing.
     */
    public static void main( String[] args ) throws UnsupportedEncodingException
        {
        final String s = "The quick brown fox jumped over the lazy dog's back; sample url: http://mindprod.com/jgloss/digest.html";
        final byte[] theTextToDigestAsBytes = s.getBytes( EIO.UTF8 );
        out.println( digest( theTextToDigestAsBytes ) );
        }
    }