Skip to content

Commit 87d24dd

Browse files
committed
Allow hash characters in file names passed to NetcdfFiles
When opening a file through NetcdfFiles, any potential URI fragments are removed for certain checks. This only needs to be done for non-local files, so we skip this for local files. Fixes #1492
1 parent 5dd4ca6 commit 87d24dd

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998-2019 John Caron and University Corporation for Atmospheric Research/Unidata
2+
* Copyright (c) 1998-2025 John Caron and University Corporation for Atmospheric Research/Unidata
33
* See LICENSE for license information.
44
*/
55

@@ -27,6 +27,7 @@
2727
import java.util.zip.ZipEntry;
2828
import java.util.zip.ZipInputStream;
2929
import javax.annotation.Nullable;
30+
import ucar.nc2.dataset.DatasetUrl;
3031
import ucar.nc2.internal.iosp.netcdf3.N3headerNew;
3132
import ucar.nc2.internal.iosp.netcdf3.N3iospNew;
3233
import ucar.nc2.iosp.AbstractIOServiceProvider;
@@ -476,7 +477,9 @@ public static ucar.unidata.io.RandomAccessFile getRaf(String location, int buffe
476477
}
477478

478479
private static String removeFragment(String uriString) {
479-
return uriString.split("#")[0];
480+
List<String> protocols = DatasetUrl.getProtocols(uriString);
481+
// only remove fragment from non-local files
482+
return (protocols.isEmpty() || protocols.contains("file")) ? uriString : uriString.split("#")[0];
480483
}
481484

482485
private static boolean looksCompressed(String filename) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2025 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
4+
*/
5+
6+
package ucar.nc2;
7+
8+
import static com.google.common.truth.Truth.assertThat;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.TemporaryFolder;
15+
import ucar.nc2.util.IO;
16+
import ucar.unidata.util.test.TestDir;
17+
18+
public class TestOpenFunkyFilePath {
19+
20+
@Rule
21+
public TemporaryFolder tempFolder = new TemporaryFolder();
22+
23+
@Test
24+
public void testFileWithHash() throws IOException {
25+
File org = new File(TestDir.cdmLocalTestDataDir + "jan.nc");
26+
NetcdfFile ncf = NetcdfFiles.open(org.getAbsolutePath());
27+
assertThat(ncf).isNotNull();
28+
ncf.close();
29+
30+
// create file to trigger error from https://github.com/Unidata/netcdf-java/issues/1492
31+
File copyWithHashName = File.createTempFile("jan#", ".nc", tempFolder.getRoot());
32+
IO.copyFile(org, copyWithHashName);
33+
34+
NetcdfFile ncf2 = NetcdfFiles.open(copyWithHashName.getAbsolutePath());
35+
assertThat(ncf2).isNotNull();
36+
ncf2.close();
37+
38+
NetcdfFile ncf3 = NetcdfFiles.open("file:" + copyWithHashName.getAbsolutePath());
39+
assertThat(ncf3).isNotNull();
40+
ncf3.close();
41+
42+
NetcdfFile ncf4 = NetcdfFiles.open("file://" + copyWithHashName.getAbsolutePath());
43+
assertThat(ncf4).isNotNull();
44+
ncf4.close();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)