Java Code Examples for java.util.Objects#checkFromIndexSize()

The following examples show how to use java.util.Objects#checkFromIndexSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelInputStream.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public synchronized int read(byte[] bs, int off, int len)
    throws IOException
{
    Objects.checkFromIndexSize(off, len, bs.length);
    if (len == 0)
        return 0;

    ByteBuffer bb = ((this.bs == bs)
                     ? this.bb
                     : ByteBuffer.wrap(bs));
    bb.limit(Math.min(off + len, bb.capacity()));
    bb.position(off);
    this.bb = bb;
    this.bs = bs;
    return read(bb);
}
 
Example 2
Source File: LittleEndianDataConverter.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void putBigInteger(byte[] b, int offset, int size, BigInteger value) {
	Objects.checkFromIndexSize(offset, size, b.length);

	int fillIndex = offset + size - 1; // start fill from MSB
	int srcIndex;

	byte[] valBytes = value.toByteArray();
	if (valBytes.length >= size) {
		srcIndex = valBytes.length - size;
	}
	else {
		srcIndex = 0;
		byte signbits = (value.signum() < 0) ? (byte) 0xff : 0;
		for (int i = valBytes.length; i < size; i++) {
			b[fillIndex--] = signbits;
		}
	}
	for (int i = srcIndex; i < valBytes.length; i++) {
		b[fillIndex--] = valBytes[i];
	}
}
 
Example 3
Source File: LittleEndianDataConverter.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public BigInteger getBigInteger(byte[] b, int offset, int size, boolean signed) {
	Objects.checkFromIndexSize(offset, size, b.length);

	int msbIndex = 0;
	if (!signed) {
		// prepend 0 byte
		++size;
		msbIndex = 1;
	}
	int bIndex = 0;
	byte[] bytes = new byte[size];
	for (int i = size - 1; i >= msbIndex; i--) {
		bytes[i] = b[offset + bIndex++];
	}
	return new BigInteger(bytes);
}
 
Example 4
Source File: NioSocketImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Reads bytes from the socket into the given byte array.
 * @return the number of bytes read or -1 at EOF
 * @throws IndexOutOfBoundsException if the bound checks fail
 * @throws SocketException if the socket is closed or a socket I/O error occurs
 * @throws SocketTimeoutException if the read timeout elapses
 */
private int read(byte[] b, int off, int len) throws IOException {
    Objects.checkFromIndexSize(off, len, b.length);
    if (len == 0) {
        return 0;
    } else {
        readLock.lock();
        try {
            // emulate legacy behavior to return -1, even if socket is closed
            if (readEOF)
                return -1;
            // read up to MAX_BUFFER_SIZE bytes
            int size = Math.min(len, MAX_BUFFER_SIZE);
            int n = implRead(b, off, size);
            if (n == -1)
                readEOF = true;
            return n;
        } finally {
            readLock.unlock();
        }
    }
}
 
Example 5
Source File: BigEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public int getInt(byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, Integer.BYTES, b.length);

	int v = b[offset];
	for (int i = 1; i < 4; i++) {
		v = (v << 8) | (b[offset + i] & 0xff);
	}
	return v;
}
 
Example 6
Source File: LittleEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void putValue(long value, int size, byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, size, b.length);
	Objects.checkIndex(size, Long.BYTES + 1);

	for (int i = 0; i < size; i++) {
		b[offset + i] = (byte) value;
		value >>= 8;
	}
}
 
Example 7
Source File: SocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public long write(ByteBuffer[] srcs, int offset, int length)
    throws IOException
{
    Objects.checkFromIndexSize(offset, length, srcs.length);

    writeLock.lock();
    try {
        boolean blocking = isBlocking();
        long n = 0;
        try {
            beginWrite(blocking);
            n = IOUtil.write(fd, srcs, offset, length, nd);
            if (blocking) {
                while (IOStatus.okayToRetry(n) && isOpen()) {
                    park(Net.POLLOUT);
                    n = IOUtil.write(fd, srcs, offset, length, nd);
                }
            }
        } finally {
            endWrite(blocking, n > 0);
            if (n <= 0 && isOutputClosed)
                throw new AsynchronousCloseException();
        }
        return IOStatus.normalize(n);
    } finally {
        writeLock.unlock();
    }
}
 
Example 8
Source File: LittleEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public int getInt(byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, Integer.BYTES, b.length);

	int v = b[offset + 3];
	for (int i = 2; i >= 0; i--) {
		v = (v << 8) | (b[offset + i] & 0xff);
	}
	return v;
}
 
Example 9
Source File: AESCrypt.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Encrypt exactly one block of plaintext.
 */
void encryptBlock(byte[] in, int inOffset,
                  byte[] out, int outOffset) {
    Objects.checkFromIndexSize(inOffset, AES_BLOCK_SIZE, in.length);
    Objects.checkFromIndexSize(outOffset, AES_BLOCK_SIZE, out.length);
    implEncryptBlock(in, inOffset, out, outOffset);
}
 
Example 10
Source File: BigEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void putInt(byte[] b, int offset, int value) {
	Objects.checkFromIndexSize(offset, Integer.BYTES, b.length);

	b[offset + 3] = (byte) (value);
	for (int i = 2; i >= 0; i--) {
		value >>= 8;
		b[offset + i] = (byte) (value);
	}
}
 
Example 11
Source File: Poly1305.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Update the MAC with bytes from an array.
 *
 * @param input the input bytes.
 * @param offset the starting index from which to update the MAC.
 * @param len the number of bytes to process.
 */
void engineUpdate(byte[] input, int offset, int len) {
    Objects.checkFromIndexSize(offset, len, input.length);
    if (blockOffset > 0) {
        // We have some left-over data from previous updates
        int blockSpaceLeft = BLOCK_LENGTH - blockOffset;
        if (len < blockSpaceLeft) {
            System.arraycopy(input, offset, block, blockOffset, len);
            blockOffset += len;
            return; // block wasn't filled
        } else {
            System.arraycopy(input, offset, block, blockOffset,
                    blockSpaceLeft);
            offset += blockSpaceLeft;
            len -= blockSpaceLeft;
            processBlock(block, 0, BLOCK_LENGTH);
            blockOffset = 0;
        }
    }
    while (len >= BLOCK_LENGTH) {
        processBlock(input, offset, BLOCK_LENGTH);
        offset += BLOCK_LENGTH;
        len -= BLOCK_LENGTH;
    }
    if (len > 0) { // and len < BLOCK_LENGTH
        System.arraycopy(input, offset, block, 0, len);
        blockOffset = len;
    }
}
 
Example 12
Source File: BigEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public long getValue(byte[] b, int offset, int size) {
	Objects.checkFromIndexSize(offset, size, b.length);
	Objects.checkIndex(size, Long.BYTES + 1);

	long val = 0;
	for (int i = 0; i < size; i++) {
		val = (val << 8) | (b[offset + i] & 0xff);
	}
	return val;
}
 
Example 13
Source File: BigEndianDataConverter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public long getLong(byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, Long.BYTES, b.length);

	long v = b[offset];
	for (int i = 1; i < 8; i++) {
		v = (v << 8) | (b[offset + i] & 0xff);
	}
	return v;
}
 
Example 14
Source File: LittleEndianDataConverter.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public short getShort(byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, Short.BYTES, b.length);

	return (short) (((b[offset + 1] & 0xff) << 8) | (b[offset] & 0xff));
}
 
Example 15
Source File: ByteArrayInputStream.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Reads up to {@code len} bytes of data into an array of bytes from this
 * input stream.  If {@code pos} equals {@code count}, then {@code -1} is
 * returned to indicate end of file.  Otherwise, the  number {@code k} of
 * bytes read is equal to the smaller of {@code len} and {@code count-pos}.
 * If {@code k} is positive, then bytes {@code buf[pos]} through
 * {@code buf[pos+k-1]} are copied into {@code b[off]} through
 * {@code b[off+k-1]} in the manner performed by {@code System.arraycopy}.
 * The value {@code k} is added into {@code pos} and {@code k} is returned.
 * <p>
 * This {@code read} method cannot block.
 *
 * @param   b     the buffer into which the data is read.
 * @param   off   the start offset in the destination array {@code b}
 * @param   len   the maximum number of bytes read.
 * @return  the total number of bytes read into the buffer, or
 *          {@code -1} if there is no more data because the end of
 *          the stream has been reached.
 * @throws  NullPointerException If {@code b} is {@code null}.
 * @throws  IndexOutOfBoundsException If {@code off} is negative,
 * {@code len} is negative, or {@code len} is greater than
 * {@code b.length - off}
 */
public synchronized int read(byte b[], int off, int len) {
    Objects.checkFromIndexSize(off, len, b.length);

    if (pos >= count) {
        return -1;
    }

    int avail = count - pos;
    if (len > avail) {
        len = avail;
    }
    if (len <= 0) {
        return 0;
    }
    System.arraycopy(buf, pos, b, off, len);
    pos += len;
    return len;
}
 
Example 16
Source File: InputStream.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Reads up to {@code len} bytes of data from the input stream into
 * an array of bytes.  An attempt is made to read as many as
 * {@code len} bytes, but a smaller number may be read.
 * The number of bytes actually read is returned as an integer.
 *
 * <p> This method blocks until input data is available, end of file is
 * detected, or an exception is thrown.
 *
 * <p> If {@code len} is zero, then no bytes are read and
 * {@code 0} is returned; otherwise, there is an attempt to read at
 * least one byte. If no byte is available because the stream is at end of
 * file, the value {@code -1} is returned; otherwise, at least one
 * byte is read and stored into {@code b}.
 *
 * <p> The first byte read is stored into element {@code b[off]}, the
 * next one into {@code b[off+1]}, and so on. The number of bytes read
 * is, at most, equal to {@code len}. Let <i>k</i> be the number of
 * bytes actually read; these bytes will be stored in elements
 * {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},
 * leaving elements {@code b[off+}<i>k</i>{@code ]} through
 * {@code b[off+len-1]} unaffected.
 *
 * <p> In every case, elements {@code b[0]} through
 * {@code b[off-1]} and elements {@code b[off+len]} through
 * {@code b[b.length-1]} are unaffected.
 *
 * <p> The {@code read(b, off, len)} method
 * for class {@code InputStream} simply calls the method
 * {@code read()} repeatedly. If the first such call results in an
 * {@code IOException}, that exception is returned from the call to
 * the {@code read(b,} {@code off,} {@code len)} method.  If
 * any subsequent call to {@code read()} results in a
 * {@code IOException}, the exception is caught and treated as if it
 * were end of file; the bytes read up to that point are stored into
 * {@code b} and the number of bytes read before the exception
 * occurred is returned. The default implementation of this method blocks
 * until the requested amount of input data {@code len} has been read,
 * end of file is detected, or an exception is thrown. Subclasses are
 * encouraged to provide a more efficient implementation of this method.
 *
 * @param      b     the buffer into which the data is read.
 * @param      off   the start offset in array {@code b}
 *                   at which the data is written.
 * @param      len   the maximum number of bytes to read.
 * @return     the total number of bytes read into the buffer, or
 *             {@code -1} if there is no more data because the end of
 *             the stream has been reached.
 * @throws     IOException If the first byte cannot be read for any reason
 *             other than end of file, or if the input stream has been closed,
 *             or if some other I/O error occurs.
 * @throws     NullPointerException If {@code b} is {@code null}.
 * @throws     IndexOutOfBoundsException If {@code off} is negative,
 *             {@code len} is negative, or {@code len} is greater than
 *             {@code b.length - off}
 * @see        java.io.InputStream#read()
 */
public int read(byte b[], int off, int len) throws IOException {
    Objects.checkFromIndexSize(off, len, b.length);
    if (len == 0) {
        return 0;
    }

    int c = read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len ; i++) {
            c = read();
            if (c == -1) {
                break;
            }
            b[off + i] = (byte)c;
        }
    } catch (IOException ee) {
    }
    return i;
}
 
Example 17
Source File: CharTermAttributeImpl.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public final CharTermAttribute setLength(int length) {
  Objects.checkFromIndexSize(0, length, termBuffer.length);
  termLength = length;
  return this;
}
 
Example 18
Source File: Reader.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new {@code Reader} that reads no characters. The returned
 * stream is initially open.  The stream is closed by calling the
 * {@code close()} method.  Subsequent calls to {@code close()} have no
 * effect.
 *
 * <p> While the stream is open, the {@code read()}, {@code read(char[])},
 * {@code read(char[], int, int)}, {@code read(Charbuffer)}, {@code
 * ready()}, {@code skip(long)}, and {@code transferTo()} methods all
 * behave as if end of stream has been reached. After the stream has been
 * closed, these methods all throw {@code IOException}.
 *
 * <p> The {@code markSupported()} method returns {@code false}.  The
 * {@code mark()} and {@code reset()} methods throw an {@code IOException}.
 *
 * <p> The {@link #lock object} used to synchronize operations on the
 * returned {@code Reader} is not specified.
 *
 * @return a {@code Reader} which reads no characters
 *
 * @since 11
 */
public static Reader nullReader() {
    return new Reader() {
        private volatile boolean closed;

        private void ensureOpen() throws IOException {
            if (closed) {
                throw new IOException("Stream closed");
            }
        }

        @Override
        public int read() throws IOException {
            ensureOpen();
            return -1;
        }

        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
            Objects.checkFromIndexSize(off, len, cbuf.length);
            ensureOpen();
            if (len == 0) {
                return 0;
            }
            return -1;
        }

        @Override
        public int read(CharBuffer target) throws IOException {
            Objects.requireNonNull(target);
            ensureOpen();
            if (target.hasRemaining()) {
                return -1;
            }
            return 0;
        }

        @Override
        public boolean ready() throws IOException {
            ensureOpen();
            return false;
        }

        @Override
        public long skip(long n) throws IOException {
            ensureOpen();
            return 0L;
        }

        @Override
        public long transferTo(Writer out) throws IOException {
            Objects.requireNonNull(out);
            ensureOpen();
            return 0L;
        }

        @Override
        public void close() {
            closed = true;
        }
    };
}
 
Example 19
Source File: BigInteger.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Translates the sign-magnitude representation of a BigInteger into a
 * BigInteger.  The sign is represented as an integer signum value: -1 for
 * negative, 0 for zero, or 1 for positive.  The magnitude is a sub-array of
 * a byte array in <i>big-endian</i> byte-order: the most significant byte
 * is the element at index {@code off}.  A zero value of the length
 * {@code len} is permissible, and will result in a BigInteger value of 0,
 * whether signum is -1, 0 or 1.  The {@code magnitude} array is assumed to
 * be unchanged for the duration of the constructor call.
 *
 * An {@code IndexOutOfBoundsException} is thrown if the length of the array
 * {@code magnitude} is non-zero and either {@code off} is negative,
 * {@code len} is negative, or {@code off+len} is greater than the length of
 * {@code magnitude}.
 *
 * @param  signum signum of the number (-1 for negative, 0 for zero, 1
 *         for positive).
 * @param  magnitude big-endian binary representation of the magnitude of
 *         the number.
 * @param  off the start offset of the binary representation.
 * @param  len the number of bytes to use.
 * @throws NumberFormatException {@code signum} is not one of the three
 *         legal values (-1, 0, and 1), or {@code signum} is 0 and
 *         {@code magnitude} contains one or more non-zero bytes.
 * @throws IndexOutOfBoundsException if the provided array offset and
 *         length would cause an index into the byte array to be
 *         negative or greater than or equal to the array length.
 * @since 9
 */
public BigInteger(int signum, byte[] magnitude, int off, int len) {
    if (signum < -1 || signum > 1) {
        throw(new NumberFormatException("Invalid signum value"));
    }
    Objects.checkFromIndexSize(off, len, magnitude.length);

    // stripLeadingZeroBytes() returns a zero length array if len == 0
    this.mag = stripLeadingZeroBytes(magnitude, off, len);

    if (this.mag.length == 0) {
        this.signum = 0;
    } else {
        if (signum == 0)
            throw(new NumberFormatException("signum-magnitude mismatch"));
        this.signum = signum;
    }
    if (mag.length >= MAX_MAG_LENGTH) {
        checkRange();
    }
}
 
Example 20
Source File: BigEndianDataConverter.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public short getShort(byte[] b, int offset) {
	Objects.checkFromIndexSize(offset, Short.BYTES, b.length);

	return (short) (((b[offset] & 0xff) << 8) | (b[offset + 1] & 0xff));
}