diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDRange.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDRange.java index b0eaf355e2c..e4976d136f8 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDRange.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDRange.java @@ -16,6 +16,7 @@ */ package org.apache.pdfbox.pdmodel.common; +import java.util.Objects; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSFloat; @@ -46,10 +47,18 @@ public PDRange() * Constructor assumes a starting index of 0. * * @param range The array that describes the range. + * If range is null, then sets to the default range of 0..1. */ public PDRange( COSArray range ) { - rangeArray = range; + if (range == null) { + rangeArray = new COSArray(); + rangeArray.add( new COSFloat( 0.0f ) ); + rangeArray.add( new COSFloat( 1.0f ) ); + } else { + rangeArray = range; + } + startingIndex = 0; } /** @@ -60,11 +69,22 @@ public PDRange( COSArray range ) * * @param range The array that describes the index * @param index The range index into the array for the start of the range. + * If range is null, then regardless of index, sets to the default range of 0..1. + * @throws IllegalArgumentException if index does not correspond to a valid range. */ public PDRange( COSArray range, int index ) { - rangeArray = range; - startingIndex = index; + if (range == null) { + rangeArray = new COSArray(); + rangeArray.add( new COSFloat( 0.0f ) ); + rangeArray.add( new COSFloat( 1.0f ) ); + startingIndex = 0; + } else if (index < 0 || 2 * index + 2 > range.size()) { + throw new IllegalArgumentException("index does not correspond to a valid range in the array."); + } else { + rangeArray = Objects.requireNonNull(range, "range cannot be null"); + startingIndex = index; + } } /** diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestPDRange.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestPDRange.java new file mode 100644 index 00000000000..a7cdcd0d206 --- /dev/null +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestPDRange.java @@ -0,0 +1,57 @@ +/* + * Copyright 2015 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pdfbox.pdmodel.common; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.apache.pdfbox.cos.COSArray; +import org.apache.pdfbox.cos.COSFloat; +import org.junit.jupiter.api.Test; + +class TestPDRange { + + @Test + void testNullRange() + { + PDRange pdRange1 = new PDRange( null ); + assertEquals( pdRange1.getMin(), 0.0f ); + assertEquals( pdRange1.getMax(), 1.0f ); + + PDRange pdRange2 = new PDRange( null, 0 ); + assertEquals( pdRange2.getMin(), 0.0f ); + assertEquals( pdRange2.getMax(), 1.0f ); + } + + @Test + void testInvalidIndex() + { + COSArray cosArray = new COSArray(); + cosArray.add( new COSFloat( 0.0f ) ); + cosArray.add( new COSFloat( 1.0f ) ); + cosArray.add( new COSFloat( 2.0f ) ); + + assertThrows(IllegalArgumentException.class, () -> { + new PDRange( cosArray, -1 ); + }); + assertThrows(IllegalArgumentException.class, () -> { + new PDRange( cosArray, 1 ); + }); + assertThrows(IllegalArgumentException.class, () -> { + new PDRange( cosArray, 2 ); + }); + } +}