static void checkNonnegative(int position) { if (position < 0) { throw new IndexOutOfBoundsException("position (" + position + ") must not be negative"); } }
@Override public void skipBytes(int length) { int newReaderIndex = readerIndex + length; if (newReaderIndex > writerIndex) { throw new IndexOutOfBoundsException(); } readerIndex = newReaderIndex; }
private void assertIndex(boolean expression, String format, Object... args) { if (!expression) { String message = String.format(format, args); throw new IndexOutOfBoundsException(message); } }
/** * get. * * @param index index. * @return element. */ public E get(int index) { if (index >= mSize) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize); } return index < 0 ? mElements.get(index + mSize) : mElements.get(index); }
@Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { ByteBuffer data = buffer.duplicate(); try { data.limit(index + length).position(index); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } data.get(dst, dstIndex, length); }
@Override public void onNext(T t) { if (value != null) { upstream.get().dispose(); onError(new IndexOutOfBoundsException("More than one element received")); return; } value = t; }
/** Write {@code byteCount} bytes from {@code source} to the file at {@code pos}. */ public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) throw new IndexOutOfBoundsException(); while (byteCount > 0L) { long bytesWritten = fileChannel.transferFrom(source, pos, byteCount); pos += bytesWritten; byteCount -= bytesWritten; } }
@Override public String apply(String input) { throw new IndexOutOfBoundsException(); } })
@Override public void getBytes(int index, ByteBuffer dst) { ByteBuffer data = buffer.duplicate(); int bytesToCopy = Math.min(capacity() - index, dst.remaining()); try { data.limit(index + bytesToCopy).position(index); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } dst.put(data); }
@Override public ChannelBuffer copy(int index, int length) { if (index < 0 || length < 0 || index + length > array.length) { throw new IndexOutOfBoundsException(); } byte[] copiedArray = new byte[length]; System.arraycopy(array, index, copiedArray, 0, length); return new HeapChannelBuffer(copiedArray); }
@Override public void onNext(T t) { if (value != null) { upstream.get().cancel(); onError(new IndexOutOfBoundsException("More than one element received")); return; } value = t; }
@Override public void onNext(T t) { if (done) { return; } if (value != null) { upstream.cancel(); done = true; this.value = null; downstream.onError(new IndexOutOfBoundsException("Too many elements in the Publisher")); } else { value = t; } }
@Override public void readBytes(ChannelBuffer dst, int dstIndex, int length) { // careful if (readableBytes() < length) { throw new IndexOutOfBoundsException(); } byte[] data = new byte[length]; buffer.readBytes(data, 0, length); dst.setBytes(dstIndex, data, 0, length); }
@Override public void writeBytes(ChannelBuffer src, int length) { // careful if (length > src.readableBytes()) { throw new IndexOutOfBoundsException(); } writeBytes(src, src.readerIndex(), length); src.readerIndex(src.readerIndex() + length); }
@Override public void getBytes(int index, ChannelBuffer dst, int length) { if (length > dst.writableBytes()) { throw new IndexOutOfBoundsException(); } getBytes(index, dst, dst.writerIndex(), length); dst.writerIndex(dst.writerIndex() + length); }
@Override public void writeBytes(ChannelBuffer src, int length) { if (length > src.readableBytes()) { throw new IndexOutOfBoundsException(); } writeBytes(src, src.readerIndex(), length); src.readerIndex(src.readerIndex() + length); }
@Override public void getBytes(int index, ChannelBuffer dst, int length) { // careful if (length > dst.writableBytes()) { throw new IndexOutOfBoundsException(); } getBytes(index, dst, dst.writerIndex(), length); dst.writerIndex(dst.writerIndex() + length); }
@Override public void writeBytes(ChannelBuffer src, int length) { // careful if (length > src.readableBytes()) { throw new IndexOutOfBoundsException(); } writeBytes(src, src.readerIndex(), length); src.readerIndex(src.readerIndex() + length); }
@Override public void setBytes(int index, ChannelBuffer src, int length) { // careful if (length > src.readableBytes()) { throw new IndexOutOfBoundsException(); } setBytes(index, src, src.readerIndex(), length); src.readerIndex(src.readerIndex() + length); }
private static void assertCannotSet(AbstractFuture<Integer> future) { assertThat(future.set(99)).isFalse(); assertThat(future.setException(new IndexOutOfBoundsException())).isFalse(); assertThat(future.setFuture(new AbstractFuture<Integer>() {})).isFalse(); assertThat(future.setFuture(immediateFuture(99))).isFalse(); }