diff options
| author | Xin Li <delphij@google.com> | 2019-09-04 13:33:59 -0700 |
|---|---|---|
| committer | Xin Li <delphij@google.com> | 2019-09-04 13:33:59 -0700 |
| commit | e199ca954dff7fdfb06e6280695d34a957ed5efc (patch) | |
| tree | 78be7fa3d9855c2a536e80ed4e7360845a968791 /core/java/android/util/SparseIntArray.java | |
| parent | 1bc876bb5b2b8506fe5e47421456c36ba4eefdea (diff) | |
| parent | 9e89d13906739e856ed809ba81c32415770717c6 (diff) | |
DO NOT MERGE - Merge Android 10 into master
Bug: 139893257
Change-Id: I9e3c4fe5406c9913d50fe3b07d7f7cef7a246b96
Diffstat (limited to 'core/java/android/util/SparseIntArray.java')
| -rw-r--r-- | core/java/android/util/SparseIntArray.java | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/core/java/android/util/SparseIntArray.java b/core/java/android/util/SparseIntArray.java index 19547534aef5..1ca1717828fa 100644 --- a/core/java/android/util/SparseIntArray.java +++ b/core/java/android/util/SparseIntArray.java @@ -16,14 +16,15 @@ package android.util; +import android.annotation.UnsupportedAppUsage; + import com.android.internal.util.ArrayUtils; import com.android.internal.util.GrowingArrayUtils; -import java.util.Arrays; - -import android.annotation.UnsupportedAppUsage; import libcore.util.EmptyArray; +import java.util.Arrays; + /** * SparseIntArrays map integers to integers. Unlike a normal array of integers, * there can be gaps in the indices. It is intended to be more memory efficient @@ -46,11 +47,11 @@ import libcore.util.EmptyArray; * order in the case of <code>valueAt(int)</code>.</p> */ public class SparseIntArray implements Cloneable { - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use keyAt(int) private int[] mKeys; - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use valueAt(int), setValueAt(int, int) private int[] mValues; - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use size() private int mSize; /** @@ -169,8 +170,18 @@ public class SparseIntArray implements Cloneable { * be in ascending order, e.g., <code>keyAt(0)</code> will return the * smallest key and <code>keyAt(size()-1)</code> will return the largest * key.</p> + * + * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for + * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an + * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public int keyAt(int index) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } return mKeys[index]; } @@ -184,16 +195,35 @@ public class SparseIntArray implements Cloneable { * <code>valueAt(0)</code> will return the value associated with the * smallest key and <code>valueAt(size()-1)</code> will return the value * associated with the largest key.</p> + * + * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for + * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an + * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public int valueAt(int index) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } return mValues[index]; } /** * Directly set the value at a particular index. - * @hide + * + * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for + * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an + * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public void setValueAt(int index, int value) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } mValues[index] = value; } |
