forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.java
More file actions
191 lines (171 loc) · 5.76 KB
/
Copy pathStream.java
File metadata and controls
191 lines (171 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package org.libvirt;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.StreamPointer;
import com.sun.jna.NativeLong;
public class Stream {
public static int VIR_STREAM_NONBLOCK = (1 << 0);
/**
* the native virStreamPtr.
*/
StreamPointer VSP;
/**
* The Connect Object that represents the Hypervisor of this Domain
*/
private Connect virConnect;
/**
* The libvirt connection from the hypervisor
*/
protected Libvirt libvirt;
Stream(Connect virConnect, StreamPointer VSP) {
this.virConnect = virConnect;
this.VSP = VSP;
libvirt = virConnect.libvirt;
}
/**
* Request that the in progress data transfer be cancelled abnormally before
* the end of the stream has been reached
*/
public int abort() throws LibvirtException {
int returnValue = libvirt.virStreamAbort(VSP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Register a callback to be notified when a stream becomes writable, or
* readable.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventAddCallback">Libvirt
* Docs</a>
* @param events
* the events to monitor
* @param cb
* the callback method
* @return 0 for success, -1 for failure
* @throws LibvirtException
*/
public int addCallback(int events, Libvirt.VirStreamEventCallback cb) throws LibvirtException {
int returnValue = libvirt.virStreamEventAddCallback(VSP, events, cb, null, null);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
@Override
public void finalize() throws LibvirtException {
free();
}
/**
* Indicate that there is no further data is to be transmitted on the
* stream.
*
* @return 0 if success, -1 if failure
* @throws LibvirtException
*/
public int finish() throws LibvirtException {
int returnValue = libvirt.virStreamFinish(VSP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Decrement the reference count on a stream, releasing the stream object if
* the reference count has hit zero.
*
* @throws LibvirtException
* @return 0 on success, or -1 on error.
*/
public int free() throws LibvirtException {
int success = 0;
if (VSP != null) {
success = libvirt.virStreamFree(VSP);
ErrorHandler.processError(libvirt, success);
VSP = null;
}
return success;
}
/**
* Receieves data from teh stream into the buffer provided.
*
* @param data
* the put the sata into
* @return the number of bytes read, -1 on error, -2 if the buffer is empty
* @throws LibvirtException
*/
public int receive(byte[] data) throws LibvirtException {
int returnValue = libvirt.virStreamRecv(VSP, data, new NativeLong(data.length));
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Batch receive method
*
* @see http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecvAll
* @param handler
* the callback handler
* @return 0 if successfule, -1 otherwise
* @throws LibvirtException
*/
public int receiveAll(Libvirt.VirStreamSinkFunc handler) throws LibvirtException {
int returnValue = libvirt.virStreamRecvAll(VSP, handler, null);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Remove an event callback from the stream
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventRemoveCallback">Libvirt
* Docs</a>
* @return 0 for success, -1 for failure
* @throws LibvirtException
*/
public int removeCallback() throws LibvirtException {
int returnValue = libvirt.virStreamEventRemoveCallback(VSP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Write a series of bytes to the stream.
*
* @param data
* the data to write
* @return the number of bytes written, -1 on error, -2 if the buffer is
* full
* @throws LibvirtException
*/
public int send(String data) throws LibvirtException {
int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length()));
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Batch send method
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamSendAll">Libvirt
* Documentation</a>
* @param handler
* the callback handler
* @return 0 if successfule, -1 otherwise
* @throws LibvirtException
*/
public int sendAll(Libvirt.VirStreamSourceFunc handler) throws LibvirtException {
int returnValue = libvirt.virStreamSendAll(VSP, handler, null);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Changes the set of events to monitor for a stream.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventUpdateCallback">Libvirt
* Docs</a>
* @param events
* the events to monitor
* @return 0 for success, -1 for failure
* @throws LibvirtException
*/
public int updateCallback(int events) throws LibvirtException {
int returnValue = libvirt.virStreamEventUpdateCallback(VSP, events);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
}