Java Code Examples for com.google.common.base.Preconditions#checkPositionIndexes()

The following examples show how to use com.google.common.base.Preconditions#checkPositionIndexes() . These examples are extracted from open source projects. 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 Project: aion   File: Slice.java    License: MIT License 6 votes vote down vote up
/**
 * Transfers the content of the specified source stream to this buffer starting at the specified
 * absolute {@code index}.
 *
 * @param length the number of bytes to transfer
 * @return the actual number of bytes read in from the specified channel. {@code -1} if the
 *     specified channel is closed.
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or if
 *     {@code index + length} is greater than {@code this.capacity}
 * @throws java.io.IOException if the specified stream threw an exception during I/O
 */
public int setBytes(int index, InputStream in, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    int readBytes = 0;
    do {
        int localReadBytes = in.read(data, index, length);
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        }
        readBytes += localReadBytes;
        index += localReadBytes;
        length -= localReadBytes;
    } while (length > 0);

    return readBytes;
}
 
Example 2
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a slice of this buffer's sub-region. Modifying the content of the returned buffer or
 * this buffer affects each other's content while they maintain separate indexes and marks.
 */
public Slice slice(int index, int length) {
    if (index == 0 && length == this.length) {
        return this;
    }

    Preconditions.checkPositionIndexes(index, index + length, this.length);
    if (index >= 0 && length == 0) {
        return Slices.EMPTY_SLICE;
    }
    return new Slice(data, offset + index, length);
}
 
Example 3
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a copy of this buffer's sub-region. Modifying the content of the returned buffer or
 * this buffer does not affect each other at all.
 */
public Slice copySlice(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);

    index += offset;
    byte[] copiedArray = new byte[length];
    System.arraycopy(data, index, copiedArray, 0, length);
    return new Slice(copiedArray);
}
 
Example 4
Source Project: joyqueue   File: SnappyCompressor.java    License: Apache License 2.0 5 votes vote down vote up
static boolean equals(final byte[] left, final int leftIndex, final byte[] right, final int rightIndex,
        final int length) {
    Preconditions.checkPositionIndexes(leftIndex, leftIndex + length, left.length);
    Preconditions.checkPositionIndexes(rightIndex, rightIndex + length, right.length);

    for (int i = 0; i < length; i++) {
        if (left[leftIndex + i] != right[rightIndex + i]) {
            return false;
        }
    }
    return true;
}
 
Example 5
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
public byte[] copyBytes(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    if (index == 0) {
        return Arrays.copyOf(data, length);
    } else {
        byte[] value = new byte[length];
        System.arraycopy(data, index, value, 0, length);
        return value;
    }
}
 
Example 6
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Gets a 32-bit integer at the specified absolute {@code index} in this buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 4} is greater than {@code this.capacity}
 */
public int getInt(int index) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_INT, this.length);
    index += offset;
    return (data[index] & 0xff)
            | (data[index + 1] & 0xff) << 8
            | (data[index + 2] & 0xff) << 16
            | (data[index + 3] & 0xff) << 24;
}
 
Example 7
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Gets a 64-bit long integer at the specified absolute {@code index} in this buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 8} is greater than {@code this.capacity}
 */
public long getLong(int index) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_LONG, this.length);
    index += offset;
    return ((long) data[index] & 0xff)
            | ((long) data[index + 1] & 0xff) << 8
            | ((long) data[index + 2] & 0xff) << 16
            | ((long) data[index + 3] & 0xff) << 24
            | ((long) data[index + 4] & 0xff) << 32
            | ((long) data[index + 5] & 0xff) << 40
            | ((long) data[index + 6] & 0xff) << 48
            | ((long) data[index + 7] & 0xff) << 56;
}
 
Example 8
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Transfers the content of the specified source channel to this buffer starting at the
 * specified absolute {@code index}.
 *
 * @param length the maximum number of bytes to transfer
 * @return the actual number of bytes read in from the specified channel. {@code -1} if the
 *     specified channel is closed.
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or if
 *     {@code index + length} is greater than {@code this.capacity}
 * @throws java.io.IOException if the specified channel threw an exception during I/O
 */
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    ByteBuffer buf = ByteBuffer.wrap(data, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example 9
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
public int setBytes(int index, FileChannel in, int position, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    ByteBuffer buf = ByteBuffer.wrap(data, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf, position + readBytes);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example 10
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Sets the specified 32-bit integer at the specified absolute {@code index} in this buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 4} is greater than {@code this.capacity}
 */
public void setInt(int index, int value) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_INT, this.length);
    index += offset;
    data[index] = (byte) (value);
    data[index + 1] = (byte) (value >>> 8);
    data[index + 2] = (byte) (value >>> 16);
    data[index + 3] = (byte) (value >>> 24);
}
 
Example 11
Source Project: aion   File: Slice.java    License: MIT License 5 votes vote down vote up
/**
 * Sets the specified 64-bit long integer at the specified absolute {@code index} in this
 * buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 8} is greater than {@code this.capacity}
 */
public void setLong(int index, long value) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_LONG, this.length);
    index += offset;
    data[index] = (byte) (value);
    data[index + 1] = (byte) (value >>> 8);
    data[index + 2] = (byte) (value >>> 16);
    data[index + 3] = (byte) (value >>> 24);
    data[index + 4] = (byte) (value >>> 32);
    data[index + 5] = (byte) (value >>> 40);
    data[index + 6] = (byte) (value >>> 48);
    data[index + 7] = (byte) (value >>> 56);
}
 
Example 12
Source Project: joyqueue   File: SnappyCompressor.java    License: Apache License 2.0 4 votes vote down vote up
private static int emitLiteral(byte[] output, int outputIndex, byte[] literal, final int literalIndex,
        final int length, final boolean allowFastPath) {
    Preconditions.checkPositionIndexes(literalIndex, literalIndex + length, literal.length);

    int n = length - 1;      // Zero-length literals are disallowed
    if (n < 60) {
        // Size fits in tag byte
        output[outputIndex++] = (byte) (LITERAL | n << 2);

        // The vast majority of copies are below 16 bytes, for which a
        // call to memcpy is overkill. This fast path can sometimes
        // copy up to 15 bytes too much, but that is okay in the
        // main loop, since we have a bit to go on for both sides:
        //
        //   - The input will always have kInputMarginBytes = 15 extra
        //     available bytes, as long as we're in the main loop, and
        //     if not, allowFastPath = false.
        //   - The output will always have 32 spare bytes (see
        //     MaxCompressedLength).
        if (allowFastPath && length <= 16) {
            Systems.copyLong(literal, literalIndex, output, outputIndex);
            Systems.copyLong(literal, literalIndex + 8, output, outputIndex + 8);
            outputIndex += length;
            return outputIndex;
        }
    } else if (n < (1 << 8)) {
        output[outputIndex++] = (byte) (LITERAL | 59 + 1 << 2);
        output[outputIndex++] = (byte) (n);
    } else if (n < (1 << 16)) {
        output[outputIndex++] = (byte) (LITERAL | 59 + 2 << 2);
        output[outputIndex++] = (byte) (n);
        output[outputIndex++] = (byte) (n >>> 8);
    } else if (n < (1 << 24)) {
        output[outputIndex++] = (byte) (LITERAL | 59 + 3 << 2);
        output[outputIndex++] = (byte) (n);
        output[outputIndex++] = (byte) (n >>> 8);
        output[outputIndex++] = (byte) (n >>> 16);
    } else {
        output[outputIndex++] = (byte) (LITERAL | 59 + 4 << 2);
        output[outputIndex++] = (byte) (n);
        output[outputIndex++] = (byte) (n >>> 8);
        output[outputIndex++] = (byte) (n >>> 16);
        output[outputIndex++] = (byte) (n >>> 24);
    }

    System.arraycopy(literal, literalIndex, output, outputIndex, length);
    outputIndex += length;
    return outputIndex;
}
 
Example 13
Source Project: aion   File: Slice.java    License: MIT License 4 votes vote down vote up
/**
 * Converts this buffer's sub-region into a NIO buffer. The returned buffer shares the content
 * with this buffer.
 */
public ByteBuffer toByteBuffer(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    return ByteBuffer.wrap(data, index, length).order(LITTLE_ENDIAN);
}
 
Example 14
Source Project: zetasketch   File: ByteSlice.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Wraps a byte array into a new byte slice. The slice's position will be zero, its limit will be
 * {@code array.length}. The backing array will be the given array and its array offset will be
 * zero.
 *
 * <p>The byte slice will never modify the byte array itself. Instead, it will create a copy as
 * soon as the first modification takes place. However, external changes to the byte array will be
 * reflected on the byte slice's read methods for the duration it is aliasing the data.
 *
 * <p>This is equivalent to <code>{@link #copyOnWrite(byte[], int, int) copyOnWrite}(data, 0,
 * data.length)</code>.
 */
public static ByteSlice copyOnWrite(byte[] array, int offset, int length) {
  int limit = offset + length;
  Preconditions.checkPositionIndexes(offset, limit, array.length);
  ByteSlice slice = new ByteSlice();
  slice.initForCopyOnWrite(array, 0, offset, limit);
  return slice;
}
 
Example 15
Source Project: exonum-java-binding   File: HashCode.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Copies bytes from this hash code into {@code dest}.
 *
 * @param dest the byte array into which the hash code will be written
 * @param offset the start offset in the data
 * @param maxLength the maximum number of bytes to write
 * @return the number of bytes written to {@code dest}
 * @throws IndexOutOfBoundsException if there is not enough room in {@code dest}
 */
@CanIgnoreReturnValue
public int writeBytesTo(byte[] dest, int offset, int maxLength) {
  maxLength = Ints.min(maxLength, bits() / 8);
  Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
  writeBytesToImpl(dest, offset, maxLength);
  return maxLength;
}
 
Example 16
Source Project: aion   File: Slice.java    License: MIT License 3 votes vote down vote up
/**
 * Transfers the specified source array's data to this buffer starting at the specified absolute
 * {@code index}.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, if
 *     the specified {@code srcIndex} is less than {@code 0}, if {@code index + length} is
 *     greater than {@code this.capacity}, or if {@code srcIndex + length} is greater than
 *     {@code src.length}
 */
public void setBytes(int index, byte[] source, int sourceIndex, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    Preconditions.checkPositionIndexes(sourceIndex, sourceIndex + length, source.length);
    index += offset;
    System.arraycopy(source, sourceIndex, data, index, length);
}
 
Example 17
Source Project: aion   File: Slice.java    License: MIT License 2 votes vote down vote up
/**
 * Transfers the specified source buffer's data to this buffer starting at the specified
 * absolute {@code index} until the source buffer's position reaches its limit.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or if
 *     {@code index + src.remaining()} is greater than {@code this.capacity}
 */
public void setBytes(int index, ByteBuffer source) {
    Preconditions.checkPositionIndexes(index, index + source.remaining(), this.length);
    index += offset;
    source.get(data, index, source.remaining());
}
 
Example 18
Source Project: aion   File: Slice.java    License: MIT License 2 votes vote down vote up
/**
 * Gets a byte at the specified absolute {@code index} in this buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 1} is greater than {@code this.capacity}
 */
public byte getByte(int index) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_BYTE, this.length);
    index += offset;
    return data[index];
}
 
Example 19
Source Project: aion   File: Slice.java    License: MIT License 2 votes vote down vote up
/**
 * Transfers this buffer's data to the specified stream starting at the specified absolute
 * {@code index}.
 *
 * @param length the number of bytes to transfer
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or if
 *     {@code index + length} is greater than {@code this.capacity}
 * @throws java.io.IOException if the specified stream threw an exception during I/O
 */
public void getBytes(int index, OutputStream out, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    out.write(data, index, length);
}
 
Example 20
Source Project: aion   File: Slice.java    License: MIT License 2 votes vote down vote up
/**
 * Sets the specified byte at the specified absolute {@code index} in this buffer. The 24
 * high-order bits of the specified value are ignored.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 *     {@code index + 1} is greater than {@code this.capacity}
 */
public void setByte(int index, int value) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_BYTE, this.length);
    index += offset;
    data[index] = (byte) value;
}