Example usage for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

List of usage examples for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException.

Prototype

public ArrayIndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new ArrayIndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:Main.java

public static void checkOffsetAndCount(int arrayLength, int offset, int count) {
    if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) {
        throw new ArrayIndexOutOfBoundsException(
                "length=" + arrayLength + "; regionStart=" + offset + "; regionLength=" + count);
    }//from   w  ww  .  j ava 2s.com
}

From source file:Main.java

public static void checkOffsetAndCount(int arrayLength, int offset, int count)
        throws ArrayIndexOutOfBoundsException {
    if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) {
        throw new ArrayIndexOutOfBoundsException(offset);
    }/*from  ww w .j a v a 2  s. c o  m*/
}

From source file:Main.java

public static int indexOf(int[] array, int value) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == value) {
            return i;
        }//from   w ww. j  a  v a  2s.c  o  m
    }
    throw new ArrayIndexOutOfBoundsException(value + "is not in " + Arrays.toString(array));
}

From source file:Main.java

public static final boolean equals(final byte[] pArrayA, final int pOffsetA, final byte[] pArrayB,
        final int pOffsetB, final int pLength) {
    final int lastIndexA = pOffsetA + pLength;
    if (lastIndexA > pArrayA.length) {
        throw new ArrayIndexOutOfBoundsException(pArrayA.length);
    }//from   www.  j a  v a  2s . com

    final int lastIndexB = pOffsetB + pLength;
    if (lastIndexB > pArrayB.length) {
        throw new ArrayIndexOutOfBoundsException(pArrayB.length);
    }

    for (int a = pOffsetA, b = pOffsetB; a < lastIndexA; a++, b++) {
        if (pArrayA[a] != pArrayB[b]) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static <T> int indexOf(T[] array, T value) {
    for (int i = 0; i < array.length; i++) {
        if (array[i].equals(value)) {
            return i;
        }/*from  w  w  w  . j  ava2  s.  com*/
    }
    throw new ArrayIndexOutOfBoundsException(value.toString() + "is not in " + Arrays.toString(array));
}

From source file:Main.java

public static void checkOffsetAndCount(long size, long offset, long byteCount) {
    if ((offset | byteCount) < 0 || offset > size || size - offset < byteCount) {
        throw new ArrayIndexOutOfBoundsException(
                String.format("size=%s offset=%s byteCount=%s", size, offset, byteCount));
    }/*from  w  w  w . j av a  2  s  .c  om*/
}

From source file:Main.java

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.");
    }//from www .jav a  2s . c o  m
    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;
}

From source file:Main.java

public static float squareDistance(float[] p1, float[] p2) throws ArrayIndexOutOfBoundsException {
    if (p1.length != p2.length) {
        throw new ArrayIndexOutOfBoundsException(Math.max(p1.length, p2.length));
    }/* w  w  w .  j av a 2 s  . com*/
    float sum = 0;
    for (int i = 0; i < p1.length; i++) {
        float d = p1[i] - p2[i];
        sum += d * d;
    }
    return sum;
}

From source file:halive.shootinoutside.common.util.Vector2D.java

public Vector2D(byte[] b, int offset) {
    if (offset + 8 > b.length)
        throw new ArrayIndexOutOfBoundsException("Invalid Length");
    byte[] d = new byte[8];
    int ptr = 0;//from  w  w  w .ja va 2  s.  c o  m
    for (int i = offset; i < offset + 8; i++) {
        d[ptr] = b[i];
        ptr++;
    }
    deserialize(d);
}

From source file:HexDump.java

/**
 * Dump an array of bytes to an OutputStream.
 *
 * @param data  the byte array to be dumped
 * @param offset  its offset, whatever that might mean
 * @param stream  the OutputStream to which the data is to be
 *               written/*from   w w w .  j  a  va  2s. c om*/
 * @param index initial index into the byte array
 *
 * @throws IOException is thrown if anything goes wrong writing
 *         the data to stream
 * @throws ArrayIndexOutOfBoundsException if the index is
 *         outside the data array's bounds
 * @throws IllegalArgumentException if the output stream is null
 */

public static void dump(byte[] data, long offset, OutputStream stream, int index)
        throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {

    if ((index < 0) || (index >= data.length)) {
        throw new ArrayIndexOutOfBoundsException(
                "illegal index: " + index + " into array of length " + data.length);
    }
    if (stream == null) {
        throw new IllegalArgumentException("cannot write to nullstream");
    }
    long display_offset = offset + index;
    StringBuffer buffer = new StringBuffer(74);

    for (int j = index; j < data.length; j += 16) {
        int chars_read = data.length - j;

        if (chars_read > 16) {
            chars_read = 16;
        }
        dump(buffer, display_offset).append(' ');
        for (int k = 0; k < 16; k++) {
            if (k < chars_read) {
                dump(buffer, data[k + j]);
            } else {
                buffer.append("  ");
            }
            buffer.append(' ');
        }
        for (int k = 0; k < chars_read; k++) {
            if ((data[k + j] >= ' ') && (data[k + j] < 127)) {
                buffer.append((char) data[k + j]);
            } else {
                buffer.append('.');
            }
        }
        buffer.append(EOL);
        stream.write(buffer.toString().getBytes());
        stream.flush();
        buffer.setLength(0);
        display_offset += chars_read;
    }
}