8299982: (bf) Buffer.checkIndex(int, int) should use Preconditions.checkIndex(int, int, BiFunction)

Reviewed-by: uschindler, vtewari, rriggs, alanb
This commit is contained in:
Brian Burkhalter 2023-01-26 20:09:01 +00:00
parent 7ddafd75b0
commit c4278144be

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,13 +33,17 @@ import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.misc.VM.BufferPool;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;
import java.io.FileDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Reference;
import java.util.List;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* A container for data of a specific primitive type.
@ -738,18 +742,36 @@ public abstract sealed class Buffer
}
/**
* Checks the given index against the limit, throwing an {@link
* IndexOutOfBoundsException} if it is not smaller than the limit
* or is smaller than zero.
* Exception formatter that returns an {@link IndexOutOfBoundsException}
* with no detail message.
*/
private static final BiFunction<String,List<Number>,IndexOutOfBoundsException>
IOOBE_FORMATTER = Preconditions.outOfBoundsExceptionFormatter(new Function<>() {
@Override
public IndexOutOfBoundsException apply(String s) {
return new IndexOutOfBoundsException();
}
});
/**
* Checks the given index against the limit, throwing an {@link
* IndexOutOfBoundsException} if it is greater than the limit
* or is negative.
*/
@ForceInline
final int checkIndex(int i) { // package-private
return Objects.checkIndex(i, limit);
return Preconditions.checkIndex(i, limit, IOOBE_FORMATTER);
}
/**
* Checks the given index and number of bytes against the range
* {@code [0, limit]}, throwing an {@link
* IndexOutOfBoundsException} if the index is negative or the index
* plus the number of bytes is greater than the limit.
*/
@ForceInline
final int checkIndex(int i, int nb) { // package-private
if ((i < 0) || (nb > limit - i))
throw new IndexOutOfBoundsException();
return i;
return Preconditions.checkIndex(i, limit - nb + 1, IOOBE_FORMATTER);
}
final int markValue() { // package-private