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() 

Source Link

Document

Constructs an IndexOutOfBoundsException with no detail message.

Usage

From source file:FastBufferedWriter.java

/**
 * Writes a portion of an array of characters.
 *
 * <p> Ordinarily this method stores characters from the given array into
 * this stream's buffer, flushing the buffer to the underlying stream as
 * needed.  If the requested length is at least as large as the buffer,
 * however, then this method will flush the buffer and write the characters
 * directly to the underlying stream.  Thus redundant
 * <code>BufferedWriter</code>s will not copy data unnecessarily.
 *
 * @param  cbuf  A character array/*from   w ww .j  a  v a  2 s  . c om*/
 * @param  off   Offset from which to start reading characters
 * @param  len   Number of characters to write
 *
 * @exception  IOException  If an I/O error occurs
 */
public void write(char cbuf[], int off, int len) throws IOException {
    if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return;
    }

    if (len >= nChars) {
        /* If the request length exceeds the size of the output buffer,
         flush the buffer and then write the data directly.  In this
         way buffered streams will cascade harmlessly. */
        flushBuffer();
        out.write(cbuf, off, len);
        return;
    }

    int b = off, t = off + len;
    while (b < t) {
        int d = min(nChars - nextChar, t - b);
        System.arraycopy(cbuf, b, cb, nextChar, d);
        b += d;
        nextChar += d;
        if (nextChar >= nChars)
            flushBuffer();
    }
}

From source file:org.crazydog.util.spring.FastByteArrayOutputStream.java

@Override
public void write(byte[] data, int offset, int length) throws IOException {
    if (data == null) {
        throw new NullPointerException();
    } else if (offset < 0 || offset + length > data.length || length < 0) {
        throw new IndexOutOfBoundsException();
    } else if (this.closed) {
        throw new IOException("Stream closed");
    } else {/*from  ww w .ja v a2 s. co  m*/
        if (this.buffers.peekLast() == null || this.buffers.getLast().length == this.index) {
            addBuffer(length);
        }
        if (this.index + length > this.buffers.getLast().length) {
            int pos = offset;
            do {
                if (this.index == this.buffers.getLast().length) {
                    addBuffer(length);
                }
                int copyLength = this.buffers.getLast().length - this.index;
                if (length < copyLength) {
                    copyLength = length;
                }
                System.arraycopy(data, pos, this.buffers.getLast(), this.index, copyLength);
                pos += copyLength;
                this.index += copyLength;
                length -= copyLength;
            } while (length > 0);
        } else {
            // copy in the sub-array
            System.arraycopy(data, offset, this.buffers.getLast(), this.index, length);
            this.index += length;
        }
    }
}

From source file:com.opengamma.util.timeseries.fast.integer.object.FastMapIntObjectTimeSeries.java

@Override
public int getTimeFast(final int index) {
    if (index >= _map.size()) {
        throw new IndexOutOfBoundsException();
    }// w  w  w . j  a  v  a2s.  c  om
    final IntBidirectionalIterator iterator = _map.keySet().iterator();
    iterator.skip(index);
    return iterator.nextInt();
}

From source file:cat.calidos.morfeu.model.injection.DocumentModule.java

@Produces
@Named("PrefixURI")
public static URI documentPrefix(@Named("ParsedDocument") Document doc, @Named("Prefix") String prefix)
        throws ParsingException {
    URI prefixURI = null;/*w w  w.ja v a 2 s .c o  m*/
    if (prefix == null || prefix.length() == 0) {
        // we make a best effort to guess the prefix
        String uri = doc.getURI().toString();
        try {
            int index = uri.lastIndexOf("/");
            if (index == -1) {
                throw new ParsingException("Problem guessing prefix as no / found on '" + uri + "'",
                        new IndexOutOfBoundsException());
            }
            prefixURI = new URI(uri.substring(0, index + 1));
        } catch (URISyntaxException e) {
            throw new ParsingException("Problem guessing prefix of '" + uri + "'", e);
        }
    } else {
        try {
            prefixURI = new URI(prefix);
        } catch (URISyntaxException e) {
            throw new ParsingException("Problem with invalid URI of prefix '" + prefix + "'", e);
        }
    }

    return prefixURI;

}

From source file:com.github.rvesse.airline.restrictions.common.OccurrencesRestriction.java

@Override
public String[] getContentBlock(int blockNumber) {
    if (blockNumber != 0)
        throw new IndexOutOfBoundsException();
    if (this.maximum) {
        return new String[] { String.format("This option may occur a maximum of %d times", this.occurrences) };
    } else {/*from   ww  w .j a va  2  s .  c  o  m*/
        return new String[] { String.format("This option must occur a minimum of %d times", this.occurrences) };
    }
}

From source file:fr.inria.atlanmod.neoemf.graph.blueprints.datastore.estores.impl.DirectWriteBlueprintsResourceEStoreImpl.java

protected Object get(InternalEObject object, EReference eReference, int index) {
    Vertex vertex = graph.getVertex(object);
    if (!eReference.isMany()) {
        Iterator<Vertex> iterator = vertex.getVertices(Direction.OUT, eReference.getName()).iterator();
        if (iterator.hasNext()) {
            Vertex referencedVertex = iterator.next();
            return reifyVertex(referencedVertex);
        } else {// w  w w .  j  a  v  a2 s . c  o  m
            return null;
        }
    } else {
        Integer size = getSize(vertex, eReference);
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        } else {
            Iterator<Vertex> iterator = vertex.query().labels(eReference.getName()).direction(Direction.OUT)
                    .has(POSITION, index).vertices().iterator();
            if (iterator.hasNext()) {
                Vertex referencedVertex = iterator.next();
                return reifyVertex(referencedVertex);
            } else {
                return null;
            }
        }
    }
}

From source file:imgb64.BaseNCodecInputStream.java

/**
 * Attempts to read <code>len</code> bytes into the specified <code>b</code> array starting at <code>offset</code>
 * from this InputStream./*from   w ww  . j  av a2  s. c om*/
 *
 * @param b
 *            destination byte array
 * @param offset
 *            where to start writing the bytes
 * @param len
 *            maximum number of bytes to read
 *
 * @return number of bytes read
 * @throws IOException
 *             if an I/O error occurs.
 * @throws NullPointerException
 *             if the byte array parameter is null
 * @throws IndexOutOfBoundsException
 *             if offset, len or buffer size are invalid
 */
@Override
public int read(final byte b[], final int offset, final int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (offset < 0 || len < 0) {
        throw new IndexOutOfBoundsException();
    } else if (offset > b.length || offset + len > b.length) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    } else {
        int readLen = 0;
        /*
         Rationale for while-loop on (readLen == 0):
         -----
         Base32.readResults() usually returns > 0 or EOF (-1).  In the
         rare case where it returns 0, we just keep trying.
                
         This is essentially an undocumented contract for InputStream
         implementors that want their code to work properly with
         java.io.InputStreamReader, since the latter hates it when
         InputStream.read(byte[]) returns a zero.  Unfortunately our
         readResults() call must return 0 if a large amount of the data
         being decoded was non-base32, so this while-loop enables proper
         interop with InputStreamReader for that scenario.
         -----
         This is a fix for CODEC-101
        */
        while (readLen == 0) {
            if (!baseNCodec.hasData(context)) {
                final byte[] buf = new byte[doEncode ? 4096 : 8192];
                final int c = in.read(buf);
                if (doEncode) {
                    baseNCodec.encode(buf, 0, c, context);
                } else {
                    baseNCodec.decode(buf, 0, c, context);
                }
            }
            readLen = baseNCodec.readResults(b, offset, len, context);
        }
        return readLen;
    }
}

From source file:com.nxp.ltsm.ltsmclient.tools.Utils.java

/**
 * Convert the byte array to an int starting from the given offset.
 *
 * @param b/*from   w w  w. j  av a2  s .c o  m*/
 *            The byte array
 * @param offset
 *            The array offset
 * @return The integer
 */
public static int byteArrayToInt(byte[] b) {
    if (b.length == 1) {
        return b[0] & 0xFF;
    } else if (b.length == 2) {
        return ((b[0] & 0xFF) << 8) + (b[1] & 0xFF);
    } else if (b.length == 3) {
        return ((b[0] & 0xFF) << 16) + ((b[1] & 0xFF) << 8) + (b[2] & 0xFF);
    } else if (b.length == 4)
        return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
    else
        throw new IndexOutOfBoundsException();
}

From source file:fr.limsi.ARViewer.CharArrayBuffer.java

public void append(final byte[] b, int off, int len) {
    if (b == null) {
        return;// www  .  jav a2 s . c  o  m
    }
    if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) {
        throw new IndexOutOfBoundsException();
    }
    if (len == 0) {
        return;
    }
    int oldlen = this.len;
    int newlen = oldlen + len;
    if (newlen > this.buffer.length) {
        expand(newlen);
    }
    for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
        int ch = b[i1];
        if (ch < 0) {
            ch = 256 + ch;
        }
        this.buffer[i2] = (char) ch;
    }
    this.len = newlen;
}

From source file:Base64InputStream.java

@Override
public int read(byte[] buffer, int offset, int length) throws IOException {
    if (closed)/*w  w w . j a va2  s.  c  om*/
        throw new IOException("Base64InputStream has been closed");

    if (buffer == null)
        throw new NullPointerException();

    if (offset < 0 || length < 0 || offset + length > buffer.length)
        throw new IndexOutOfBoundsException();

    if (length == 0)
        return 0;

    return read0(buffer, offset, offset + length);
}