Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  Copyright 2006-2015 WebPKI.org (http://webpki.org).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

public class Main {
    public static int firstDiff(byte[] a, int aOffset, byte[] b, int bOffset, int length) {
        if (a == null || b == null) {
            throw new IllegalArgumentException("Cannot compare null arrays.");
        }
        if (aOffset + length > a.length || bOffset + length > b.length) {
            throw new ArrayIndexOutOfBoundsException("Range to compare not contained in array.");
        }
        if (a == b && aOffset == bOffset) {
            return -1;
        }
        for (int i = 0; i < length; i++) {
            if (a[aOffset + i] != b[bOffset + i]) {
                System.out.println(i + ": " + Integer.toHexString(0xFF & a[aOffset + i]) + " "
                        + Integer.toHexString(0xFF & b[bOffset + i]));
                return i;
            }
        }
        return -1;
    }

    public static int firstDiff(byte[] a, byte[] b, int offset, int length) {
        return firstDiff(a, offset, b, offset, length);
    }

    public static int firstDiff(byte[] a, byte[] b) {
        return firstDiff(a, b, 0, Math.min(a.length, b.length));
    }

    public static String toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase,
            char separator) {
        if (maxLength == -1 || startOffset + maxLength > value.length) {
            maxLength = value.length - startOffset;
        }
        StringBuffer r = new StringBuffer(maxLength * (separator == -1 ? 2 : 3));
        for (int i = 0; i < maxLength; i++) {
            if (i > 0 && separator != 0) {
                r.append(separator);
            }
            String t = Integer.toHexString(value[i + startOffset] & 0xFF);
            if (t.length() == 1) {
                t = "0" + t;
            }
            if (uppercase) {
                t = t.toUpperCase();
            }
            r.append(t);
        }
        return r.toString();
    }

    public static String toHexString(byte[] value, int startOffset, int maxLength) {
        return toHexString(value, startOffset, maxLength, true, ' ');
    }

    public static String toHexString(byte[] value) {
        return toHexString(value, 0, -1, true, ' ');
    }

    public static String toHexString(int value, char byteSeparator) {
        return toHexString(new byte[] { (byte) ((value >> 24) & 0xFF), (byte) ((value >> 16) & 0xFF),
                (byte) ((value >> 8) & 0xFF), (byte) (value & 0xFF) }, 0, -1, true, byteSeparator);
    }

    public static int min(int[] a) {
        return a[indexOfMin(a)];
    }

    public static int indexOfMin(int[] a) {
        if (a.length == 0) {
            throw new IllegalArgumentException("Empty array.");
        } else {
            int r = 0;
            for (int i = 1; i < a.length; i++) {
                if (a[i] < a[r]) {
                    r = i;
                }
            }
            return r;
        }
    }
}