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:br.prof.salesfilho.oci.image.ImageProcessor.java

public List<BufferedImage> getSubImages(int size) {

    if (this.image.getWidth() % size != 0 || this.image.getHeight() % size != 0) {
        throw new ArrayIndexOutOfBoundsException("Image size and input size cause array index out of bounds!");
    }/* w  ww. j a va  2s  . c  o m*/

    List<BufferedImage> result = new ArrayList<>();
    BufferedImage subImage;
    for (int i = 0; i < this.image.getWidth(); i = i + size) {
        for (int j = 0; j < this.image.getHeight(); j = j + size) {
            subImage = image.getSubimage(i, j, size, size);
            result.add(subImage);
        }
    }
    return result;
}

From source file:hu.netmind.beankeeper.query.impl.LazyListImpl.java

/**
 * Try to load the entries around the given index.
 *///  w  w w  . j  a  v a2s.co  m
private void updateList(int index) {
    // Initialize lazy list
    initialize();
    // Keep track of linear iterations
    if (index == linearLastIndex + 1)
        linearCount++;
    else
        linearCount = 0;
    linearLastIndex = index;
    // Check bounds
    if (index < 0)
        throw new ArrayIndexOutOfBoundsException(
                "lazy list index given was: " + index + ", thats < 0 and illegal.");
    if ((list != null) && (index < offset + list.size()) && (index >= offset))
        return; // List has the desired item
    // Determine whether the update will get the next page linearly
    boolean nextPage = false;
    if (list != null)
        nextPage = (index == offset + list.size());
    // Determine the startindex and size of the current select
    int batchSize = BATCH_SIZE;
    if (list != null)
        batchSize = list.size();
    if (linearCount >= batchSize)
        batchSize = batchSize * BATCH_SIZE_LINEARMULTIPLIER;
    else
        batchSize = BATCH_SIZE;
    if (batchSize > BATCH_SIZE_MAX)
        batchSize = BATCH_SIZE_MAX;
    int startIndex = 0;
    if (index < offset)
        startIndex = (index / batchSize) * batchSize;
    else
        startIndex = index;
    if (startIndex < 0)
        startIndex = 0;
    if (logger.isDebugEnabled())
        logger.debug("list index: " + index + ", startindex: " + startIndex + ", batchsize: " + batchSize
                + ", linearcount was: " + linearCount);
    linearCount = 0;
    linearLastIndex = index;
    // Determine the statement to use for given index
    HashMap session = new HashMap();
    getStmtOffset(0); // Initialize offsets
    int stmtIndex = 0;
    long realOffset = 0;
    if (hooks != null)
        stmtIndex = hooks.preIndexing(session, startIndex);
    if (stmtIndex < 0) {
        stmtIndex = 0;
        while ((stmtIndex < stmts.size()) && (stmtOffsets[stmtIndex] >= 0) && (stmtOffsets[stmtIndex + 1] >= 0)
                && (stmtOffsets[stmtIndex + 1] <= startIndex)) {
            realOffset = stmtOffsets[stmtIndex];
            stmtIndex++;
        }
    }
    if (stmtIndex >= stmts.size())
        throw new ArrayIndexOutOfBoundsException(
                "Tried to reach index: " + index + ", but that was not available.");
    if (logger.isDebugEnabled())
        logger.debug("asked index is: " + index + ", real offset: " + realOffset + ", stmt index: " + stmtIndex
                + ", start index: " + startIndex);
    // Now load the result set, which is possibly distributed in multiple
    // queries.
    offset = startIndex;
    List previousList = new ArrayList();
    if (nextPage)
        previousList = new ArrayList(list);
    list = new ArrayList(batchSize);
    // Load the already unmarshalled objects. Load until
    // list vector is full, or out of result entries. Note: we load
    // plus one entry, so we know, that there is a next entry.
    boolean override = false;
    while ((stmtIndex < stmts.size()) && ((list.size() <= batchSize) || (override))) {
        if (logger.isDebugEnabled())
            logger.debug("lazy list statement iteration: " + stmtIndex + "/" + stmts.size() + ", current size: "
                    + list.size() + "/" + batchSize);
        override = false;
        // Get the query, and optimize it
        QueryStatement stmt = (QueryStatement) stmts.get(stmtIndex);
        Limits limits = new Limits((int) (startIndex - stmtOffsets[stmtIndex]), batchSize + 1 - list.size(), 0);
        if (limits.getOffset() < 0)
            limits.setOffset(0);
        // Compute the total join count of the selected term
        int totalJoinCount = 0;
        SpecifiedTableTerm mainTerm = stmt.getSpecifiedTerm((TableTerm) stmt.getSelectTerms().get(0));
        if (mainTerm.getRelatedLeftTerms().size() > MAX_JOINS) {
            // Modify limits, so it does not select more rows than left
            // table terms. This ensures, that the select will not contain
            // more left table terms.
            if (limits.getLimit() > MAX_JOINS) {
                limits.setLimit(MAX_JOINS + 1);
                batchSize = list.size() + MAX_JOINS;
                logger.debug("adjusting limits, so max joins can be suited, new batch size: " + batchSize
                        + ", list size is: " + list.size());
            }
            // If there are many left table terms, then optimize this select
            // locally. This means, eliminate all left table terms, which will
            // not be used.
            stmt = optimizeLocalStatement(stmt, limits);
        }
        // Make query
        if (hooks != null)
            stmt = hooks.preSelect(session, stmt, previousList, limits, new Limits(offset, batchSize + 1, 0));
        SearchResult result = queryService.find(stmt, limits, unmarshalledObjects);
        // Set for next iteration
        startIndex += result.getResult().size();
        list.addAll(result.getResult());
        if (hooks != null)
            override = hooks.postSelect(session, list, new Limits(offset, batchSize + 1, 0));
        // Postoperation adjustments
        if (list.size() > batchSize) {
            logger.debug("list size " + list.size() + " is greater than batchsize " + batchSize
                    + ", iteration complete");
            // This means, that the list contained enough items for
            // the query, which means return only the exact results.
            // The size can not be determined now.
            list = list.subList(0, batchSize);
            hasNext = true;
            // List is ok for now, we don't need more
            if (logger.isDebugEnabled())
                logger.debug(
                        "updated list with full window, size is: " + list.size() + ", index was: " + index);
            return;
        } else {
            hasNext = false;
            // Compute statement length if the length is not yet known
            if (stmtOffsets[stmtIndex + 1] < 0) {
                if (list.size() == 0) {
                    logger.debug("list size was 0 for this iteration");
                    // There is no result. This can be caused by two things:
                    // - This statement is really 0 length
                    // - Statement interval ends before this start index is reached,
                    // but may contain items.
                    // Let's just calculate the sizes up until now
                    getStmtOffset(stmtIndex + 1);
                } else if (list.size() <= batchSize) {
                    logger.debug("list size " + list.size() + " is not greater than batchsize " + batchSize);
                    // This means, that the list does not contain enough items,
                    // so the size can be exactly determined.
                    stmtOffsets[stmtIndex + 1] = startIndex;
                }
            }
        }
        // Decrease cycle invariant function (in english: increase index)
        stmtIndex++;
    }
    if (logger.isDebugEnabled())
        logger.debug("updated list, size is: " + list.size() + ", index was: " + index);
}

From source file:plugin.notes.gui.NotesTreeNode.java

/**
 * Gets the child At a certain index of the NotesTreeNode object
 *
 * @param index/*  w  w  w.  j ava 2  s .c o m*/
 *          index to get the child from
 * @return The child At value
 */
@Override
public TreeNode getChildAt(int index) {
    if (!hasBeenPopulated) {
        populate();
    }

    if (children == null) {
        throw new ArrayIndexOutOfBoundsException("node has no children"); //$NON-NLS-1$
    }

    return children.elementAt(index);
}

From source file:org.pentaho.di.trans.steps.excelinput.staxpoi.StaxPoiSheet.java

@Override
public KCell[] getRow(int rownr) {
    // xlsx raw row numbers are 1-based index, KSheet is 0-based

    if (rownr < 0 || rownr >= numRows) {
        // KSheet requires out of bounds here
        throw new ArrayIndexOutOfBoundsException(rownr);
    }/* w ww  .j av a 2s  .c  om*/
    if (rownr + 1 < firstRow) {
        // before first non-empty row
        return new KCell[0];
    }
    if (rownr > 0 && currentRow == rownr + 1) {
        return currentRowCells;
    }
    try {
        if (currentRow >= rownr + 1) {
            // allow random access per api despite performance hit
            resetSheetReader();
        }
        while (sheetReader.hasNext()) {
            int event = sheetReader.next();
            if (event == XMLStreamConstants.START_ELEMENT && sheetReader.getLocalName().equals("row")) {
                String rowIndicator = sheetReader.getAttributeValue(null, "r");
                currentRow = Integer.parseInt(rowIndicator);
                if (currentRow < rownr + 1) {
                    continue;
                }
                currentRowCells = parseRow();
                return currentRowCells;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    numRows = currentRow;
    return new KCell[] {};
}

From source file:CharBuf.java

/**
 * Remove a range of value from the array. The index positions for values
 * above the range removed are decreased by the number of values removed.
 *
 * @param from index number of first value to be removed
 * @param to index number past last value to be removed
 */// ww  w  .j  ava 2  s  .co m
public void remove(int from, int to) {
    if (from >= 0 && to <= size && from <= to) {
        if (to < size) {
            int change = from - to;
            System.arraycopy(buf, to, buf, from, size - to);
            size += change;
        }
    } else {
        throw new ArrayIndexOutOfBoundsException("Invalid remove range");
    }
}

From source file:org.jboss.dashboard.dataset.AbstractDataSet.java

public DataProperty getPropertyByColumn(int column) {
    if (column < 0 || column >= properties.length) {
        throw new ArrayIndexOutOfBoundsException(
                "Column out of bounds: " + column + "(must be between 0 and " + (properties.length - 1) + ")");
    }/* w  w w  .ja va2  s . c o  m*/
    return properties[column];
}

From source file:org.anarres.lzo.LzoOutputStream.java

@Override
public void write(byte[] b, int off, int len) throws IOException {
    // logState("Before setInput");
    if (b == null)
        throw new NullPointerException();
    if (off < 0 || len < 0 || off > b.length - len)
        throw new ArrayIndexOutOfBoundsException(
                "Illegal range in buffer: Buffer length=" + b.length + ", offset=" + off + ", length=" + len);
    if (inputHoldoverBuffer != null)
        throw new IllegalStateException("Cannot accept input while holdover is present.");

    inputHoldoverBuffer = b;/*from  w ww  .  j a va2  s  . com*/
    inputHoldoverBufferPos = off;
    inputHoldoverBufferLen = len;
    compact();

    while (inputHoldoverBuffer != null || inputBufferLen == inputBuffer.length)
        compress();

    // logState("After setInput");
}

From source file:ObjectStack.java

/**
 * Pop a value from the stack./*from   w  ww.j a va2 s.  c  om*/
 *
 * @return value from top of stack
 * @exception ArrayIndexOutOfBoundsException on attempt to pop empty stack
 */
public Object pop() {
    if (m_countPresent > 0) {
        Object value = m_baseArray[--m_countPresent];
        m_baseArray[m_countPresent] = null;
        return value;
    } else {
        throw new ArrayIndexOutOfBoundsException("Attempt to pop empty stack");
    }
}

From source file:StringStack.java

/**
 * Pop a value from the stack.//from  ww w. j  a v a 2  s .c om
 *
 * @return value from top of stack
 * @exception ArrayIndexOutOfBoundsException on attempt to pop empty stack
 */
public String pop() {
    if (m_countPresent > 0) {
        String value = m_baseArray[--m_countPresent];
        m_baseArray[m_countPresent] = null;
        return value;
    } else {
        throw new ArrayIndexOutOfBoundsException("Attempt to pop empty stack");
    }
}

From source file:br.prof.salesfilho.oci.util.OCIUtils.java

/**
 * @param larger the larger array to be split into sub arrays of size
 * chunksize/*from  w  w w . ja v  a  2 s . c  o  m*/
 * @param subArraySize the size of each sub array
 * @precond chunksize > 0 && larger != null
 * @throws ArrayIndexOutOfBoundsException, NullPointerException
 */
public static List<double[][]> splitMatrix(double[][] larger, int subArraySize)
        throws ArrayIndexOutOfBoundsException, NullPointerException {
    if (subArraySize <= 0) {
        throw new ArrayIndexOutOfBoundsException("Chunks must be atleast 1x1");
    }
    int size = larger.length / subArraySize * (larger[0].length / subArraySize);
    List<double[][]> subArrays = new ArrayList<>();

    for (int c = 0; c < size; c++) {
        double[][] sub = new double[subArraySize][subArraySize];
        int startx = (subArraySize * (c / subArraySize)) % larger.length;
        int starty = (subArraySize * c) % larger[0].length;

        if (starty + subArraySize > larger[0].length) {
            starty = 0;
        }

        for (int row = 0; row < subArraySize; row++) {
            for (int col = 0; col < subArraySize; col++) {
                sub[row][col] = larger[startx + row][col + starty];
            }
        }
        subArrays.add(sub);
    }

    return subArrays;
}