/*
 * [TestSunCRC32.java]
 *
 * Summary: 32-bit Sun CRC cyclic redundancy checks.
 *
 * Copyright: (c) 2009-2012 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.7+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
// TestSunCRC32
package com.mindprod.example;

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

import static java.lang.System.out;

/**
 * 32-bit Sun CRC cyclic redundancy checks.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestSunCRC32
    {
    // -------------------------- STATIC METHODS --------------------------

    /**
     * Calc CRC-32 with Sun method
     *
     * @param ba byte array to compute CRC on
     *
     * @return 32-bit CRC, signed
     */
    private static int sunCRC32( byte[] ba )
        {
        // create a new CRC-calculating object
        final CRC32 crc = new CRC32();
        crc.update( ba );
        // crc.update( int ) processes only the low order 8-bits. It actually expects an unsigned byte.
        return ( int ) crc.getValue();
        }

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

    /**
     * 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";
        final byte[] ba = s.getBytes( "UTF-8"/* encoding */ );
        out.println( sunCRC32( ba ) );
        }
    }