Let the Device.listCapabilities method return an array filled with
non-null string objects. Furthermore, the decoding of the character
data does no longer depend upon the default JVM charset.
This also fixes a memleak because the allocated memory was not freed
in the old code.
Patch #2 just adds a unit test which simply uses this function.
Claudio Bley (2):
Make Device.listCapabilities return only valid array elements
test: ensure the Device.listCapabilities method works
src/main/java/org/libvirt/Device.java | 10 +++++++---
src/main/java/org/libvirt/jna/Libvirt.java | 2 +-
src/test/java/org/libvirt/TestJavaBindings.java | 14 ++++++++++++++
3 files changed, 22 insertions(+), 4 deletions(-)
--
1.8.5.2.msysgit.0
Show replies by date
The libvirt function virNodeDeviceListCaps might return fewer elements
than requested. Take this into account and properly decode the UTF-8
strings returned.
Additionally, the given strings are freed before returning the resulting
string array.
---
src/main/java/org/libvirt/Device.java | 10 +++++++---
src/main/java/org/libvirt/jna/Libvirt.java | 2 +-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/libvirt/Device.java
b/src/main/java/org/libvirt/Device.java
index fe49ce9..d55321e 100644
--- a/src/main/java/org/libvirt/Device.java
+++ b/src/main/java/org/libvirt/Device.java
@@ -3,6 +3,8 @@ package org.libvirt;
import org.libvirt.jna.DevicePointer;
import static org.libvirt.Library.libvirt;
+import com.sun.jna.Pointer;
+
/**
* A device which is attached to a node
*/
@@ -135,13 +137,15 @@ public class Device {
*/
public String[] listCapabilities() throws LibvirtException {
int maxCaps = getNumberOfCapabilities();
- String[] names = new String[maxCaps];
if (maxCaps > 0) {
- libvirt.virNodeDeviceListCaps(VDP, names, maxCaps);
+ Pointer[] ptrs = new Pointer[maxCaps];
+ int got = libvirt.virNodeDeviceListCaps(VDP, ptrs, maxCaps);
processError();
+ return Library.toStringArray(ptrs, got);
+ } else {
+ return Library.NO_STRINGS;
}
- return names;
}
/**
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java
b/src/main/java/org/libvirt/jna/Libvirt.java
index b026b04..a2bf42e 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -281,7 +281,7 @@ public interface Libvirt extends Library {
String virNodeDeviceGetName(DevicePointer virDevicePointer);
String virNodeDeviceGetParent(DevicePointer virDevicePointer);
int virNodeDeviceNumOfCaps(DevicePointer virDevicePointer);
- int virNodeDeviceListCaps(DevicePointer virDevicePointer, String[] names, int
maxNames);
+ int virNodeDeviceListCaps(DevicePointer virDevicePointer, Pointer[] names, int
maxNames);
String virNodeDeviceGetXMLDesc(DevicePointer virDevicePointer);
int virNodeDeviceFree(DevicePointer virDevicePointer);
int virNodeDeviceDettach(DevicePointer virDevicePointer);
--
1.8.5.2.msysgit.0
---
src/test/java/org/libvirt/TestJavaBindings.java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/src/test/java/org/libvirt/TestJavaBindings.java
b/src/test/java/org/libvirt/TestJavaBindings.java
index bba4cdb..3600844 100644
--- a/src/test/java/org/libvirt/TestJavaBindings.java
+++ b/src/test/java/org/libvirt/TestJavaBindings.java
@@ -45,6 +45,20 @@ public final class TestJavaBindings extends TestCase {
assertTrue("conn.isSecure", conn.isSecure() == 1);
}
+ /*
+ * Excercise the listCapabilities method of the Device class.
+ */
+ public void testDeviceListCapabilities() throws Exception {
+ Device dev = this.conn.deviceLookupByName("computer");
+ String[] caps = dev.listCapabilities();
+
+ // check that all caps are non-empty strings
+ for (String c: caps) {
+ assertNotNull("capability is null", c);
+ assertFalse("capability is empty", c.isEmpty());
+ }
+ }
+
public void testNodeInfo() throws Exception {
NodeInfo nodeInfo = conn.nodeInfo();
assertEquals("nodeInfo.model", "i686", nodeInfo.model);
--
1.8.5.2.msysgit.0