Example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

Introduction

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

Prototype

public IndexOutOfBoundsException(int index) 

Source Link

Document

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

Usage

From source file:io.horizondb.io.buffers.CompositeBuffer.java

/**
 * {@inheritDoc}/*  www. j  ava2 s  .  co  m*/
 */
@Override
public byte getByte(int index) {

    int idx = index + this.offset;

    if (index < 0 || index > this.capacity) {

        @SuppressWarnings("boxing")
        String msg = format("Index: %d Expected: 0 <= index < capacity(%d)", index, this.capacity);

        throw new IndexOutOfBoundsException(msg);
    }

    int off = 0;

    for (int i = 0, m = this.buffers.size(); i < m; i++) {

        ReadableBuffer buffer = this.buffers.get(i);

        if (idx < off + buffer.readableBytes()) {

            return buffer.getByte(idx - off);
        }

        off += buffer.readableBytes();
    }

    throw new IllegalStateException();
}

From source file:moe.encode.airblock.commands.arguments.list.ArgumentList.java

@Override
@SuppressWarnings("unchecked")
public <E> E getRaw(int index, Type cls, String def) throws NumberFormatException {
    // Find the real index.
    int i = this.getRealIndex(index);

    String value;/*from www.  ja v a2 s .  c o  m*/
    // Check if the index exists.
    if (i == -1) {
        if (def == null)
            throw new IndexOutOfBoundsException(this.outOfBoundsMsg(index));
        value = def;
    } else {
        value = this.values[i];
    }

    if (cls == String.class) {
        return (E) value;
    }

    // Parse the values.
    return this.executor.getContext().getArgumentConverter().parse(this.executor, cls, value);
}

From source file:com.l2jfree.gameserver.idfactory.BitSetIDFactory.java

@Override
public synchronized int getNextId() {
    int newID = _nextFreeId.get();
    _freeIds.set(newID);//from   w ww .  j  av a2 s  .co  m
    _freeIdCount.decrementAndGet();

    int nextFree = _freeIds.nextClearBit(newID);

    if (nextFree < 0) {
        nextFree = _freeIds.nextClearBit(0);
    }
    if (nextFree < 0) {
        if (_freeIds.size() < FREE_OBJECT_ID_SIZE) {
            increaseBitSetCapacity();
        } else {
            throw new IndexOutOfBoundsException("Ran out of valid Id's.");
        }
    }

    _nextFreeId.set(nextFree);

    return newID + FIRST_OID;
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java

/**
 *
 *
 * @param b64data//ww  w .  j  a  v  a2  s  . co m
 * @param start
 * @param length
 * @return
 */
public String getSubsetBase64(String b64data, int start, int length) {
    byte[] data = Base64.decodeBase64(b64data);
    if ((start + length) > data.length) {
        throw new IndexOutOfBoundsException("Length from start exceeds bounds of data");
    }
    byte[] subset = new byte[length];
    System.arraycopy(data, start, subset, 0, length);
    return Base64.encodeBase64String(subset);
}

From source file:Main.java

/**
 * Returns the <code>index</code>-th value in <code>object</code>, throwing
 * <code>IndexOutOfBoundsException</code> if there is no such element or
 * <code>IllegalArgumentException</code> if <code>object</code> is not an
 * instance of one of the supported types.
 * <p>/*from   w  w w  . j a  va2 s  .c o  m*/
 * The supported types, and associated semantics are:
 * <ul>
 * <li> Map -- the value returned is the <code>Map.Entry</code> in position
 *      <code>index</code> in the map's <code>entrySet</code> iterator,
 *      if there is such an entry.</li>
 * <li> List -- this method is equivalent to the list's get method.</li>
 * <li> Array -- the <code>index</code>-th array entry is returned,
 *      if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code>
 *      is thrown.</li>
 * <li> Collection -- the value returned is the <code>index</code>-th object
 *      returned by the collection's default iterator, if there is such an element.</li>
 * <li> Iterator or Enumeration -- the value returned is the
 *      <code>index</code>-th object in the Iterator/Enumeration, if there
 *      is such an element.  The Iterator/Enumeration is advanced to
 *      <code>index</code> (or to the end, if <code>index</code> exceeds the
 *      number of entries) as a side effect of this method.</li>
 * </ul>
 *
 * @param object  the object to get a value from
 * @param index  the index to get
 * @return the object at the specified index
 * @throws IndexOutOfBoundsException if the index is invalid
 * @throws IllegalArgumentException if the object type is invalid
 */
public static Object get(final Object object, final int index) {
    int i = index;
    if (i < 0) {
        throw new IndexOutOfBoundsException("Index cannot be negative: " + i);
    }
    if (object instanceof Map<?, ?>) {
        final Map<?, ?> map = (Map<?, ?>) object;
        final Iterator<?> iterator = map.entrySet().iterator();
        return get(iterator, i);
    } else if (object instanceof Object[]) {
        return ((Object[]) object)[i];
    } else if (object instanceof Iterator<?>) {
        final Iterator<?> it = (Iterator<?>) object;
        while (it.hasNext()) {
            i--;
            if (i == -1) {
                return it.next();
            }
            it.next();
        }
        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
    } else if (object instanceof Collection<?>) {
        final Iterator<?> iterator = ((Collection<?>) object).iterator();
        return get(iterator, i);
    } else if (object instanceof Enumeration<?>) {
        final Enumeration<?> it = (Enumeration<?>) object;
        while (it.hasMoreElements()) {
            i--;
            if (i == -1) {
                return it.nextElement();
            } else {
                it.nextElement();
            }
        }
        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
    } else if (object == null) {
        throw new IllegalArgumentException("Unsupported object type: null");
    } else {
        try {
            return Array.get(object, i);
        } catch (final IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }
}

From source file:hd3gtv.embddb.network.DataBlock.java

public void checkIfNotEmpty() {
    if (entries.isEmpty()) {
        throw new IndexOutOfBoundsException("No data entries in block");
    }/*  w w w .j  a va 2s . c om*/
}

From source file:io.horizondb.io.buffers.DirectBuffer.java

/**
 * {@inheritDoc}/* w w  w  .  j a  v a 2s.  c  om*/
 */
@Override
protected void checkSubRegion(int offset, int length) {

    if ((offset + length) > this.buffer.capacity()) {
        throw new IndexOutOfBoundsException(
                "Index: " + (offset + length) + " Capacity: " + this.buffer.capacity());
    }
}

From source file:net.ontopia.topicmaps.query.toma.impl.basic.ResultSet.java

/**
 * Returns the name of the column at the given index.
 * //  www.  ja  va2  s  .c  o  m
 * @param index the column index.
 * @return the name of the column.
 * @throws IndexOutOfBoundsException if the index is outside the range of the
 *           column definition (e.g. index < 0 or index >= getColumnCount()).
 */
public String getColumnName(int index) throws IndexOutOfBoundsException {
    if (index < 0 || index >= columns.size()) {
        throw new IndexOutOfBoundsException("No column available for index '" + index + "'");
    }
    return columns.get(index);
}

From source file:io.horizondb.io.buffers.HeapBuffer.java

/**
 * {@inheritDoc}/*from   ww  w .j  av  a  2  s .  co  m*/
 */
@Override
protected void checkSubRegion(int offset, int length) {

    if ((offset + length) > this.array.length) {
        throw new IndexOutOfBoundsException("Index: " + (offset + length) + " Capacity: " + this.array.length);
    }
}

From source file:LongList.java

/**
 * Returns the value at the given index.
 *
 * @param index the index//from  w  w  w.  ja va2s. co  m
 * @return the value at the given index
 * @throws IndexOutOfBoundsException if the index is greater or equal to the list size or if the index is negative.
 */
public long get(final int index) {
    if (index >= size || index < 0) {
        throw new IndexOutOfBoundsException("Illegal Index: " + index + " Max:" + size);
    }
    return data[index];
}