diff --git a/cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java b/cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java index 8409e3ef7a..e1cf0531ba 100644 --- a/cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java +++ b/cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998-2019 John Caron and University Corporation for Atmospheric Research/Unidata + * Copyright (c) 1998-2025 John Caron and University Corporation for Atmospheric Research/Unidata * See LICENSE for license information. */ @@ -27,6 +27,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.annotation.Nullable; +import ucar.nc2.dataset.DatasetUrl; import ucar.nc2.internal.iosp.netcdf3.N3headerNew; import ucar.nc2.internal.iosp.netcdf3.N3iospNew; import ucar.nc2.iosp.AbstractIOServiceProvider; @@ -476,7 +477,9 @@ public static ucar.unidata.io.RandomAccessFile getRaf(String location, int buffe } private static String removeFragment(String uriString) { - return uriString.split("#")[0]; + List protocols = DatasetUrl.getProtocols(uriString); + // only remove fragment from non-local files + return (protocols.isEmpty() || protocols.contains("file")) ? uriString : uriString.split("#")[0]; } private static boolean looksCompressed(String filename) { diff --git a/cdm/core/src/test/java/ucar/nc2/TestOpenFunkyFilePath.java b/cdm/core/src/test/java/ucar/nc2/TestOpenFunkyFilePath.java new file mode 100644 index 0000000000..17fc879a88 --- /dev/null +++ b/cdm/core/src/test/java/ucar/nc2/TestOpenFunkyFilePath.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025 University Corporation for Atmospheric Research/Unidata + * See LICENSE for license information. + */ + +package ucar.nc2; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.File; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import ucar.nc2.util.IO; +import ucar.unidata.util.test.TestDir; + +public class TestOpenFunkyFilePath { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testFileWithHash() throws IOException { + File org = new File(TestDir.cdmLocalTestDataDir + "jan.nc"); + NetcdfFile ncf = NetcdfFiles.open(org.getAbsolutePath()); + assertThat(ncf).isNotNull(); + ncf.close(); + + // create file to trigger error from https://github.com/Unidata/netcdf-java/issues/1492 + File copyWithHashName = File.createTempFile("jan#", ".nc", tempFolder.getRoot()); + IO.copyFile(org, copyWithHashName); + + NetcdfFile ncf2 = NetcdfFiles.open(copyWithHashName.getAbsolutePath()); + assertThat(ncf2).isNotNull(); + ncf2.close(); + + NetcdfFile ncf3 = NetcdfFiles.open("file:" + copyWithHashName.getAbsolutePath()); + assertThat(ncf3).isNotNull(); + ncf3.close(); + + NetcdfFile ncf4 = NetcdfFiles.open("file://" + copyWithHashName.getAbsolutePath()); + assertThat(ncf4).isNotNull(); + ncf4.close(); + } + +} diff --git a/cdm/s3/src/test/java/ucar/unidata/io/s3/TestS3ExternalCompressionRead.java b/cdm/s3/src/test/java/ucar/unidata/io/s3/TestS3ExternalCompressionRead.java index e79d3726c9..d047b4da09 100644 --- a/cdm/s3/src/test/java/ucar/unidata/io/s3/TestS3ExternalCompressionRead.java +++ b/cdm/s3/src/test/java/ucar/unidata/io/s3/TestS3ExternalCompressionRead.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020 University Corporation for Atmospheric Research/Unidata + * Copyright (c) 2019-2025 University Corporation for Atmospheric Research/Unidata * See LICENSE for license information. */ @@ -19,7 +19,7 @@ @Category(NeedsExternalResource.class) public class TestS3ExternalCompressionRead { - private static final String compressedObject = "cdms3:noaa-nexrad-level2?1991/07/20/KTLX/KTLX19910720_160529.gz"; + private static final String compressedObject = "cdms3:unidata-nexrad-level2?1991/07/20/KTLX/KTLX19910720_160529.gz"; private static final String fragment = "#delimiter=/"; @Test