[libvirt-java] [PATCH] Fix wrapping of virStreamSend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The native virStreamSend function is meant to be used to transfer
byte data.

Wrapping the function to use a Java String as the data depends on
the default charset of the JVM and might impose further problems
transferring \0 characters.

Use a byte Array as the parameter type instead, as for virStreamRecv.

Add the Stream.receive(byte[]) method which should be used instead
of the legacy Stream.receive(String) method.  The old method is kept
for backward compatibility, but marked as deprecated.
---
 src/main/java/org/libvirt/Stream.java      | 27 +++++++++++++++++++++++++--
 src/main/java/org/libvirt/jna/Libvirt.java |  2 +-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/libvirt/Stream.java b/src/main/java/org/libvirt/Stream.java
index bd8e87f..404c9a0 100644
--- a/src/main/java/org/libvirt/Stream.java
+++ b/src/main/java/org/libvirt/Stream.java
@@ -142,6 +142,27 @@ public class Stream {
     }
 
     /**
+     * Write the data of the given string to a stream.
+     *
+     * @param data
+     *            the string data to write
+     * @return the number of bytes written, -1 on error, -2 if the buffer is
+     *         full
+     * @throws LibvirtException
+     *
+     * @deprecated This libvirt API was previously wrapped incorrectly, since
+     *             the native function is meant to transfer bytes, not string
+     *             data.<p>
+     *             Its effect depends on the default Charset on your platform
+     *             which is determined on JVM startup.<p>
+     *             Use the {@link #send(byte[])} method instead.
+     */
+    @Deprecated
+    public int send(String data) throws LibvirtException {
+        return send(data.getBytes(java.nio.charset.Charset.defaultCharset()));
+    }
+
+    /**
      * Write a series of bytes to the stream.
      *
      * @param data
@@ -149,9 +170,11 @@ public class Stream {
      * @return the number of bytes written, -1 on error, -2 if the buffer is
      *         full
      * @throws LibvirtException
+     *
+     * @since  1.5.2
      */
-    public int send(String data) throws LibvirtException {
-        int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length()));
+    public int send(byte[] data) throws LibvirtException {
+        int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length));
         processError();
         return returnValue;
     }
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 813f09b..98f2125 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -368,7 +368,7 @@ public interface Libvirt extends Library {
     int virStreamFinish(StreamPointer virStreamPtr) ;
     int virStreamFree(StreamPointer virStreamPtr) ;
     StreamPointer virStreamNew(ConnectionPointer virConnectPtr, int flags) ;
-    int virStreamSend(StreamPointer virStreamPtr, String data, NativeLong size);
+    int virStreamSend(StreamPointer virStreamPtr, byte[] data, NativeLong size);
     int virStreamSendAll(StreamPointer virStreamPtr, Libvirt.VirStreamSourceFunc handler, Pointer opaque);
     int virStreamRecv(StreamPointer virStreamPtr, byte[] data, NativeLong length);
     int virStreamRecvAll(StreamPointer virStreamPtr, Libvirt.VirStreamSinkFunc handler, Pointer opaque);
-- 
1.8.5.2.msysgit.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]