io.pravega.common.Exceptions.checkArrayRange()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(131)

本文整理了Java中io.pravega.common.Exceptions.checkArrayRange()方法的一些代码示例,展示了Exceptions.checkArrayRange()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Exceptions.checkArrayRange()方法的具体详情如下:
包路径:io.pravega.common.Exceptions
类名称:Exceptions
方法名:checkArrayRange

Exceptions.checkArrayRange介绍

[英]Throws an appropriate exception if the given range is not included in the given array interval.
[中]

代码示例

代码示例来源:origin: pravega/pravega

@Override
public InputStream getReader(int offset, int length) {
  Exceptions.checkArrayRange(offset, length, this.length, "offset", "length");
  return new ByteArrayInputStream(this.array, this.startOffset + offset, length);
}

代码示例来源:origin: pravega/pravega

/**
 * Creates a new instance of the FixedByteArrayOutputStream class.
 *
 * @param array  The array to wrap.
 * @param offset The offset to start the OutputStream at.
 * @param length The maximum length of the OutputStream.
 * @throws NullPointerException           If array is null.
 * @throws ArrayIndexOutOfBoundsException If offset and/or length are invalid.
 */
public FixedByteArrayOutputStream(byte[] array, int offset, int length) {
  Preconditions.checkNotNull(array, "array");
  Exceptions.checkArrayRange(offset, length, array.length, "offset", "length");
  this.array = array;
  this.offset = offset;
  this.length = length;
  this.position = 0;
}

代码示例来源:origin: pravega/pravega

/**
 * Creates a new instance of the ByteArraySegment class that wraps the given array range.
 *
 * @param array       The array to wrap.
 * @param startOffset The offset within the array to start the segment at.
 * @param length      The length of the segment.
 * @param readOnly    If true, no modifications will be allowed on the segment.
 * @throws NullPointerException           If the array is null.
 * @throws ArrayIndexOutOfBoundsException If StartOffset or Length have invalid values.
 */
public ByteArraySegment(byte[] array, int startOffset, int length, boolean readOnly) {
  Preconditions.checkNotNull(array, "array");
  Exceptions.checkArrayRange(startOffset, length, array.length, "startOffset", "length");
  this.array = array;
  this.startOffset = startOffset;
  this.length = length;
  this.readOnly = readOnly;
}

代码示例来源:origin: pravega/pravega

int read(long startOffset, byte[] target, int targetOffset, int length) {
  synchronized (this.lock) {
    Exceptions.checkArrayRange(targetOffset, length, target.length, "targetOffset", "length");
    Exceptions.checkArrayRange(startOffset, length, this.length, "startOffset", "length");
    long offset = startOffset;
    int readBytes = 0;
    while (readBytes < length) {
      OffsetLocation ol = getOffsetLocation(offset);
      int bytesToCopy = Math.min(BUFFER_SIZE - ol.bufferOffset, length - readBytes);
      System.arraycopy(this.data.get(ol.bufferSequence), ol.bufferOffset, target, targetOffset + readBytes, bytesToCopy);
      readBytes += bytesToCopy;
      offset += bytesToCopy;
    }
    return readBytes;
  }
}

代码示例来源:origin: pravega/pravega

@Override
public void copyTo(byte[] target, int targetOffset, int length) {
  Preconditions.checkElementIndex(length, this.length + 1, "length");
  Exceptions.checkArrayRange(targetOffset, length, target.length, "index", "values.length");
  System.arraycopy(this.array, this.startOffset, target, targetOffset, length);
}

代码示例来源:origin: pravega/pravega

/**
 * Returns a new ByteArraySegment that is a sub-segment of this ByteArraySegment. The new ByteArraySegment wraps
 * the same underlying byte array that this ByteArraySegment does.
 *
 * @param offset   The offset within this ByteArraySegment where the new ByteArraySegment starts.
 * @param length   The length of the new ByteArraySegment.
 * @param readOnly Whether the resulting sub-segment should be read-only.
 *                 Note: if this ByteArraySegment is already read-only, this argument is ignored and the resulting
 *                 segment is read-only
 * @return The new ByteArraySegment.
 * @throws ArrayIndexOutOfBoundsException If offset or length are invalid.
 */
public ByteArraySegment subSegment(int offset, int length, boolean readOnly) {
  Exceptions.checkArrayRange(offset, length, this.length, "offset", "length");
  return new ByteArraySegment(this.array, this.startOffset + offset, length, readOnly || this.readOnly);
}

代码示例来源:origin: pravega/pravega

/**
 * Copies a specified number of bytes from the given ByteArraySegment into this ByteArraySegment.
 *
 * @param source       The ByteArraySegment to copy bytes from.
 * @param sourceOffset The offset within source to start copying from.
 * @param targetOffset The offset within this ByteArraySegment to start copying at.
 * @param length       The number of bytes to copy.
 * @throws IllegalStateException          If the ByteArraySegment is readonly.
 * @throws ArrayIndexOutOfBoundsException If targetOffset or length are invalid.
 */
public void copyFrom(ByteArraySegment source, int sourceOffset, int targetOffset, int length) {
  Preconditions.checkState(!this.readOnly, "Cannot modify a read-only ByteArraySegment.");
  Exceptions.checkArrayRange(sourceOffset, length, source.length, "index", "values.length");
  Exceptions.checkArrayRange(targetOffset, length, this.length, "index", "values.length");
  Preconditions.checkElementIndex(length, source.getLength() + 1, "length");
  System.arraycopy(source.array, source.startOffset + sourceOffset, this.array, this.startOffset + targetOffset, length);
}

代码示例来源:origin: pravega/pravega

/**
 * Creates a new instance of the CacheReadResultEntry class.
 *
 * @param streamSegmentOffset The offset within the StreamSegment where this ReadResultEntry starts at. NOTE: this is
 *                            not where the first byte of 'data' starts, rather it's where dataOffset points to in the
 *                            StreamSegment.
 * @param data                The data buffer that contains the data.
 * @param dataOffset          The offset within data where this ReadResultEntry starts at.
 * @param dataLength          The length of the data that this ReadResultEntry has.
 */
CacheReadResultEntry(long streamSegmentOffset, byte[] data, int dataOffset, int dataLength) {
  super(ReadResultEntryType.Cache, streamSegmentOffset + dataOffset, dataLength);
  Exceptions.checkArrayRange(dataOffset, dataLength, data.length, "dataOffset", "dataLength");
  complete(new ReadResultEntryContents(new ByteArrayInputStream(data, dataOffset, dataLength), dataLength));
}

代码示例来源:origin: pravega/pravega

/**
 * Copies a specified number of bytes from the given ByteArraySegment into this ByteArraySegment.
 *
 * @param source       The ByteArraySegment to copy bytes from.
 * @param targetOffset The offset within this ByteArraySegment to start copying at.
 * @param length       The number of bytes to copy.
 * @throws IllegalStateException          If the ByteArraySegment is readonly.
 * @throws ArrayIndexOutOfBoundsException If targetOffset or length are invalid.
 */
public void copyFrom(ByteArraySegment source, int targetOffset, int length) {
  Preconditions.checkState(!this.readOnly, "Cannot modify a read-only ByteArraySegment.");
  Exceptions.checkArrayRange(targetOffset, length, this.length, "index", "values.length");
  Preconditions.checkElementIndex(length, source.getLength() + 1, "length");
  System.arraycopy(source.array, source.startOffset, this.array, targetOffset + this.startOffset, length);
}

代码示例来源:origin: pravega/pravega

boolean valid = i >= 0 && i + length <= maxBound;
  if (valid) {
    Exceptions.checkArrayRange(i, length, maxBound, "start", "length");
  } else {
    final int index = i;
    AssertExtensions.assertThrows(
        String.format("Unexpected behavior for checkArrayRange(index = %d, length = %d, maxbound = %d).", index, length, maxBound),
        () -> Exceptions.checkArrayRange(index, length, maxBound, "start", "length"),
        ex -> ex instanceof ArrayIndexOutOfBoundsException);
    () -> Exceptions.checkArrayRange(10, -1, 20, "start", "length"),
    ex -> ex instanceof IllegalArgumentException);
Exceptions.checkArrayRange(0, 0, 0, "start", "length");
    () -> Exceptions.checkArrayRange(0, 1, 0, "start", "length"),
    ex -> ex instanceof ArrayIndexOutOfBoundsException);

代码示例来源:origin: pravega/pravega

long traceId = LoggerHelpers.traceEnter(log, "read", handle, offset, length);
ensureNotDeleted(h);
Exceptions.checkArrayRange(bufferOffset, length, buffer.length, "bufferOffset", "length");

相关文章