Skip to content

Commit 74faf03

Browse files
author
Alexey Semenyuk
committed
8374819: jpackage and jpackage tests leave some I/O streams unclosed
Reviewed-by: almatvee
1 parent 805866b commit 74faf03

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/AppImageInfoPListFile.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,8 +24,6 @@
2424
*/
2525
package jdk.jpackage.internal;
2626

27-
import static jdk.jpackage.internal.util.XmlUtils.initDocumentBuilder;
28-
2927
import java.io.IOException;
3028
import java.nio.file.Files;
3129
import java.nio.file.Path;
@@ -50,7 +48,7 @@ static final class InvalidPlistFileException extends Exception {
5048
static AppImageInfoPListFile loadFromInfoPList(Path infoPListFile)
5149
throws IOException, InvalidPlistFileException, SAXException {
5250

53-
final var plistReader = new PListReader(initDocumentBuilder().parse(Files.newInputStream(infoPListFile)));
51+
final var plistReader = new PListReader(Files.readAllBytes(infoPListFile));
5452

5553
try {
5654
return new AppImageInfoPListFile(

src/jdk.jpackage/share/classes/jdk/jpackage/internal/AppImageFile.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,11 +28,12 @@
2828
import static java.util.stream.Collectors.toUnmodifiableMap;
2929
import static java.util.stream.Collectors.toUnmodifiableSet;
3030
import static jdk.jpackage.internal.cli.StandardAppImageFileOption.APP_VERSION;
31-
import static jdk.jpackage.internal.cli.StandardAppImageFileOption.LAUNCHER_AS_SERVICE;
3231
import static jdk.jpackage.internal.cli.StandardAppImageFileOption.DESCRIPTION;
32+
import static jdk.jpackage.internal.cli.StandardAppImageFileOption.LAUNCHER_AS_SERVICE;
3333
import static jdk.jpackage.internal.cli.StandardAppImageFileOption.LAUNCHER_NAME;
3434
import static jdk.jpackage.internal.util.function.ThrowingFunction.toFunction;
3535

36+
import java.io.ByteArrayInputStream;
3637
import java.io.IOException;
3738
import java.nio.file.Files;
3839
import java.nio.file.NoSuchFileException;
@@ -162,7 +163,23 @@ static ExternalApplication load(ApplicationLayout appLayout, OperatingSystem os)
162163
final var relativeAppImageFilePath = appImageDir.relativize(appImageFilePath);
163164

164165
try {
165-
final Document doc = XmlUtils.initDocumentBuilder().parse(Files.newInputStream(appImageFilePath));
166+
//
167+
// Use javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream).
168+
// Don't use javax.xml.parsers.DocumentBuilder#parse(java.io.File) as this will introduce
169+
// dependency on how the XML parser reports filesystem I/O errors.
170+
// E.g.: the default JDK XML parser throws java.io.FileNotFoundException if the supplied
171+
// directory is not found and throws org.xml.sax.SAXParseException if the supplied file is a directory.
172+
// Another DOM XML parser (a different version of Xerces?) may behave differently.
173+
//
174+
// The use of javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream) eliminates
175+
// differences in how XML parsers handle file system I/O errors.
176+
// Filesystem I/O is delegated to java.nio.file.Files#readAllBytes(java.nio.file.Path),
177+
// XML parser deals with the byte stream in memory and the error handling code
178+
// doesn't depend on how XML parser reports filesystem I/O errors because
179+
// it reads data from memory, not from the filesystem.
180+
//
181+
final Document doc = XmlUtils.initDocumentBuilder().parse(
182+
new ByteArrayInputStream(Files.readAllBytes(appImageFilePath)));
166183

167184
final XPath xPath = XPathFactory.newInstance().newXPath();
168185

src/jdk.jpackage/share/classes/jdk/jpackage/internal/util/PListReader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,7 +35,6 @@
3535
import java.util.Objects;
3636
import java.util.Optional;
3737
import java.util.stream.Stream;
38-
import javax.xml.parsers.ParserConfigurationException;
3938
import javax.xml.transform.dom.DOMSource;
4039
import javax.xml.xpath.XPath;
4140
import javax.xml.xpath.XPathConstants;
@@ -311,7 +310,7 @@ public PListReader(Node node) {
311310
}
312311
}
313312

314-
public PListReader(byte[] xmlData) throws ParserConfigurationException, SAXException, IOException {
313+
public PListReader(byte[] xmlData) throws SAXException, IOException {
315314
this(XmlUtils.initDocumentBuilder().parse(new ByteArrayInputStream(xmlData)));
316315
}
317316

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/AppImageFile.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
2828
import static jdk.jpackage.internal.util.function.ThrowingSupplier.toSupplier;
2929

3030
import java.io.IOException;
31-
import java.nio.file.Files;
3231
import java.nio.file.Path;
3332
import java.util.Collections;
3433
import java.util.HashMap;
@@ -132,7 +131,7 @@ public void save(Path appImageDir) throws IOException {
132131
public static AppImageFile load(Path appImageDir) {
133132
return toSupplier(() -> {
134133
Document doc = XmlUtils.initDocumentBuilder().parse(
135-
Files.newInputStream(getPathInAppImage(appImageDir)));
134+
getPathInAppImage(appImageDir).toFile());
136135

137136
XPath xPath = XPathFactory.newInstance().newXPath();
138137

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LauncherVerifier.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,7 +39,6 @@
3939
import java.util.regex.Matcher;
4040
import java.util.regex.Pattern;
4141
import java.util.stream.Stream;
42-
import javax.xml.parsers.ParserConfigurationException;
4342
import jdk.jpackage.internal.resources.ResourceLocator;
4443
import jdk.jpackage.internal.util.PListReader;
4544
import jdk.jpackage.internal.util.function.ThrowingBiConsumer;
@@ -367,7 +366,7 @@ private void verifyInAppImageFile(JPackageCommand cmd) {
367366
}
368367
}
369368

370-
private void verifyMacEntitlements(JPackageCommand cmd) throws ParserConfigurationException, SAXException, IOException {
369+
private void verifyMacEntitlements(JPackageCommand cmd) throws SAXException, IOException {
371370
Path launcherPath = cmd.appLauncherPath(name);
372371
var entitlements = MacSignVerify.findEntitlements(launcherPath);
373372

@@ -457,8 +456,10 @@ private static String launcherDescription(
457456
private static final class DefaultEntitlements {
458457
private static Map<String, Object> loadFromResources(String resourceName) {
459458
return ThrowingSupplier.toSupplier(() -> {
460-
var bytes = ResourceLocator.class.getResourceAsStream(resourceName).readAllBytes();
461-
return new PListReader(bytes).toMap(true);
459+
try (var in = ResourceLocator.class.getResourceAsStream(resourceName)) {
460+
var bytes = in.readAllBytes();
461+
return new PListReader(bytes).toMap(true);
462+
}
462463
}).get();
463464
}
464465

test/jdk/tools/jpackage/macosx/HostArchPkgTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -64,8 +64,7 @@ private static void verifyHostArch(JPackageCommand cmd) throws Exception {
6464
dbf.setFeature("http://apache.org/xml/features/" +
6565
"nonvalidating/load-external-dtd", false);
6666
DocumentBuilder b = dbf.newDocumentBuilder();
67-
org.w3c.dom.Document doc
68-
= b.parse(Files.newInputStream(distributionFile));
67+
org.w3c.dom.Document doc = b.parse(distributionFile.toFile());
6968

7069
XPath xPath = XPathFactory.newInstance().newXPath();
7170

test/jdk/tools/jpackage/windows/WinLongVersionTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -180,9 +180,8 @@ public static void testNoUpgradeTable() throws IOException {
180180
cmd.setFakeRuntime();
181181

182182
// Create package without Upgrade table
183-
Document doc = XmlUtils.initDocumentBuilder().parse(
184-
Files.newInputStream(TKit.SRC_ROOT.resolve(
185-
"windows/classes/jdk/jpackage/internal/resources/main.wxs")));
183+
Document doc = XmlUtils.initDocumentBuilder().parse(TKit.SRC_ROOT.resolve(
184+
"windows/classes/jdk/jpackage/internal/resources/main.wxs").toFile());
186185
XPath xPath = XPathFactory.newInstance().newXPath();
187186
NodeList nodes = (NodeList) xPath.evaluate("/Wix/Product/Upgrade",
188187
doc, XPathConstants.NODESET);

0 commit comments

Comments
 (0)