This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit e6382ae9538c2f0c1530261f6747f3b6b7675ff5 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Fri Dec 9 16:29:35 2022 +0100 Fix NullPointerException when auxiliary PRJ, TFW or HDR file can not be found. Improve test stability. --- .../org/apache/sis/internal/storage/PRJDataStore.java | 11 ++++++++--- .../sis/internal/storage/esri/RawRasterStore.java | 16 ++++++++++------ .../sis/internal/storage/image/WorldFileStore.java | 18 +++++++++++------- .../sis/internal/storage/csv/StoreProviderTest.java | 5 ++++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java index 52029b56ed..38b510889f 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java @@ -66,7 +66,7 @@ import org.apache.sis.util.Classes; * be null and the CRS defined by the {@code DataOptionKey} will be used.</p> * * @author Martin Desruisseaux (Geomatys) - * @version 1.2 + * @version 1.3 * @since 1.2 * @module */ @@ -146,7 +146,12 @@ public abstract class PRJDataStore extends URIDataStore { protected final void readPRJ() throws DataStoreException { Exception cause = null; try { - final String wkt = readAuxiliaryFile(PRJ).toString(); + final AuxiliaryContent content = readAuxiliaryFile(PRJ); + if (content == null) { + listeners.warning(Resources.format(Resources.Keys.CanNotReadAuxiliaryFile_1, PRJ)); + return; + } + final String wkt = content.toString(); final StoreFormat format = new StoreFormat(locale, timezone, null, listeners); format.setConvention(Convention.WKT1_COMMON_UNITS); // Ignored if the format is WKT 2. final ParsePosition pos = new ParsePosition(0); @@ -177,7 +182,7 @@ public abstract class PRJDataStore extends URIDataStore { * An arbitrary size limit is applied for safety. * * @param extension the filename extension of the auxiliary file to open. - * @return the file content together with the source. Should be short-lived. + * @return the file content together with the source, or {@code null} if none. Should be short-lived. * @throws NoSuchFileException if the auxiliary file has not been found (when opened from path). * @throws FileNotFoundException if the auxiliary file has not been found (when opened from URL). * @throws IOException if another error occurred while opening the stream. diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/esri/RawRasterStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/esri/RawRasterStore.java index 7af2e2f21b..22b186f4bf 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/esri/RawRasterStore.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/esri/RawRasterStore.java @@ -358,6 +358,10 @@ final class RawRasterStore extends RasterStore { RawRasterLayout layout = RawRasterLayout.BIL; ByteOrder byteOrder = ByteOrder.nativeOrder(); final AuxiliaryContent header = readAuxiliaryFile(RawRasterStoreProvider.HDR); + if (header == null) { + throw new DataStoreException(Resources.forLocale(getLocale()) + .getString(Resources.Keys.CanNotReadAuxiliaryFile_1, RawRasterStoreProvider.HDR)); + } for (CharSequence line : CharSequences.splitOnEOL(header)) { final int length = line.length(); final int keyStart = CharSequences.skipLeadingWhitespaces(line, 0, length); @@ -381,12 +385,12 @@ final class RawRasterStore extends RasterStore { case BANDROWBYTES: bandRowBytes = parseStrictlyPositive(keyword, value); break; case TOTALROWBYTES: totalRowBytes = parseStrictlyPositive(keyword, value); break; case BANDGAPBYTES: bandGapBytes = Integer.parseInt(value); break; - case SKIPBYTES: skipBytes = Long.valueOf(value); break; - case ULXMAP: ulxmap = Double.valueOf(value); geomask |= 1; break; - case ULYMAP: ulymap = Double.valueOf(value); geomask |= 2; break; - case XDIM: xdim = Double.valueOf(value); geomask |= 4; break; - case YDIM: ydim = Double.valueOf(value); geomask |= 8; break; - case NODATA: nodataValue = Double.valueOf(value); break; + case SKIPBYTES: skipBytes = Long.parseLong(value); break; + case ULXMAP: ulxmap = Double.parseDouble(value); geomask |= 1; break; + case ULYMAP: ulymap = Double.parseDouble(value); geomask |= 2; break; + case XDIM: xdim = Double.parseDouble(value); geomask |= 4; break; + case YDIM: ydim = Double.parseDouble(value); geomask |= 8; break; + case NODATA: nodataValue = Double.parseDouble(value); break; case PIXELTYPE: signed = indexOf(keyword, value, "SIGNED", "SIGNEDINT") >= 0; break; case LAYOUT: layout = RawRasterLayout.valueOf(value.toUpperCase(Locale.US)); break; case BYTEORDER: { diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java index cdfc09d1bd..251dfae3fe 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java @@ -370,17 +370,21 @@ loop: for (int convention=0;; convention++) { * Reads the "World file" by parsing an auxiliary file with the given suffix. * * @param wld suffix of the auxiliary file. - * @return the "World file" content as an affine transform. + * @return the "World file" content as an affine transform, or {@code null} if none was found. * @throws IOException if an I/O error occurred. * @throws DataStoreException if the file content cannot be parsed. */ private AffineTransform2D readWorldFile(final String wld) throws IOException, DataStoreException { - final AuxiliaryContent content = readAuxiliaryFile(wld); - final String filename = content.getFilename(); - final CharSequence[] lines = CharSequences.splitOnEOL(readAuxiliaryFile(wld)); - final int expected = 6; // Expected number of elements. - int count = 0; // Actual number of elements. - final double[] elements = new double[expected]; + final AuxiliaryContent content = readAuxiliaryFile(wld); + if (content == null) { + listeners.warning(Resources.format(Resources.Keys.CanNotReadAuxiliaryFile_1, wld)); + return null; + } + final String filename = content.getFilename(); + final CharSequence[] lines = CharSequences.splitOnEOL(content); + final int expected = 6; // Expected number of elements. + int count = 0; // Actual number of elements. + final double[] elements = new double[expected]; for (int i=0; i<expected; i++) { final String line = lines[i].toString().trim(); if (!line.isEmpty() && line.charAt(0) != '#') { diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/csv/StoreProviderTest.java b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/csv/StoreProviderTest.java index 9a3e17c9e6..41a69d1e48 100644 --- a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/csv/StoreProviderTest.java +++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/csv/StoreProviderTest.java @@ -16,6 +16,8 @@ */ package org.apache.sis.internal.storage.csv; +import org.apache.sis.setup.OptionKey; +import org.apache.sis.setup.GeometryLibrary; import org.apache.sis.storage.DataStoreException; import org.apache.sis.storage.StorageConnector; import org.apache.sis.storage.ProbeResult; @@ -29,7 +31,7 @@ import static org.junit.Assert.*; * Tests {@link StoreProvider}. * * @author Martin Desruisseaux (Geomatys) - * @version 0.8 + * @version 1.3 * @since 0.8 * @module */ @@ -43,6 +45,7 @@ public final strictfp class StoreProviderTest extends TestCase { public void testProbeContent() throws DataStoreException { final StoreProvider p = new StoreProvider(); final StorageConnector c = new StorageConnector(StoreTest.testData()); + c.setOption(OptionKey.GEOMETRY_LIBRARY, GeometryLibrary.ESRI); assertEquals(ProbeResult.SUPPORTED, p.probeContent(c)); } }