From c4278144bee667e2565a40f12a2a2c251d6cf3aa Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Thu, 26 Jan 2023 20:09:01 +0000 Subject: [PATCH] 8299982: (bf) Buffer.checkIndex(int, int) should use Preconditions.checkIndex(int, int, BiFunction) Reviewed-by: uschindler, vtewari, rriggs, alanb --- .../share/classes/java/nio/Buffer.java | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/java.base/share/classes/java/nio/Buffer.java b/src/java.base/share/classes/java/nio/Buffer.java index 116200531e4..843c9b3becd 100644 --- a/src/java.base/share/classes/java/nio/Buffer.java +++ b/src/java.base/share/classes/java/nio/Buffer.java @@ -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,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