diff --git a/CHANGES.md b/CHANGES.md index 1663634cdf..64b7e489f0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,7 +13,7 @@ Features Bug Fixes --------- - +* [#1411](https://github.com/java-native-access/jna/pull/1411): Do not throw `Win32Exception` on success for empty section in `Kernel32Util#getPrivateProfileSection` - [@mkarg](https://github.com/mkarg). Release 5.10.0 ============== diff --git a/README.md b/README.md index 30a038fe3b..79f9c6042a 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ JNA is a mature library with dozens of contributors and hundreds of commercial a - [FileBot Media Renamer](http://www.filebot.net) by Reinhard Pointner. - [USB for Java](https://launchpad.net/libusb4j) by Mario Boikov. - [Waffle](https://github.com/dblock/waffle): Enables SSO on Windows in Java applications, by Daniel Doubrovkine. -- [leveldb-jna](https://github.com/protonail/leveldb-jna): Cross-platform JNA based adapter for [LevelDB](https://github.com/google/leveldb) (used in [Keylord](http://protonail.com)). -- [bolt-jna](https://github.com/protonail/bolt-jna): Cross-platform JNA based adapter for [Bolt](https://github.com/boltdb/bolt) (used in [Keylord](http://protonail.com)). It is show how to use JNA for binding to Go library. +- [leveldb-jna](https://github.com/protonail/leveldb-jna): Cross-platform JNA based adapter for [LevelDB](https://github.com/google/leveldb). +- [bolt-jna](https://github.com/protonail/bolt-jna): Cross-platform JNA based adapter for [Bolt](https://github.com/boltdb/bolt). It is to show how to use JNA for binding to Go library. - [JVM OpenVR Bindings](https://github.com/kotlin-graphics/openvr). - [Apache Ignite](https://ignite.apache.org/): Direct IO plugin - [Domino JNA](https://github.com/klehmann/domino-jna): Cross-platform access to HCL Notes/Domino C API methods from Java diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java index 175690c256..9654fc52b1 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java @@ -719,6 +719,9 @@ public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcesso return procInfoList.toArray(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[0]); } + // Prevents useless heap pollution + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + /** * Retrieves all the keys and values for the specified section of an initialization file. * @@ -739,7 +742,12 @@ public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcesso public static final String[] getPrivateProfileSection(final String appName, final String fileName) { final char buffer[] = new char[32768]; // Maximum section size according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx) if (Kernel32.INSTANCE.GetPrivateProfileSection(appName, buffer, new DWORD(buffer.length), fileName).intValue() == 0) { - throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); + final int lastError = Kernel32.INSTANCE.GetLastError(); + if (lastError == Kernel32.ERROR_SUCCESS) { + return EMPTY_STRING_ARRAY; + } else { + throw new Win32Exception(lastError); + } } return new String(buffer).split("\0"); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32UtilTest.java b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32UtilTest.java index 5d326eace7..cd96bfbee1 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32UtilTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32UtilTest.java @@ -244,9 +244,18 @@ public final void testGetPrivateProfileSection() throws IOException { final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini"); tmp.deleteOnExit(); - final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp))); + final PrintWriter writer0 = new PrintWriter(new BufferedWriter(new FileWriter(tmp))); + try { + writer0.println("[X]"); + } finally { + writer0.close(); + } + + final String[] lines0 = Kernel32Util.getPrivateProfileSection("X", tmp.getCanonicalPath()); + assertEquals(lines0.length, 0); + + final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp, true))); try { - writer.println("[X]"); writer.println("A=1"); writer.println("foo=bar"); } finally {