Skip to content

Commit e23e40e

Browse files
committed
Split transfer method in three methods
1 parent f2b2a75 commit e23e40e

File tree

1 file changed

+73
-28
lines changed

1 file changed

+73
-28
lines changed

src/main/java/org/usb4java/javax/IrpQueue.java

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,48 +184,93 @@ private int write(final byte[] data, final int offset, final int len)
184184
* @throws UsbException
185185
* When data transfer fails.
186186
*/
187-
private int transfer(final DeviceHandle handle,
188-
final UsbEndpointDescriptor descriptor, final int type,
187+
private int transfer(final DeviceHandle handle,
188+
final UsbEndpointDescriptor descriptor, final int type,
189189
final ByteBuffer buffer) throws UsbException
190190
{
191191
final byte address = descriptor.bEndpointAddress();
192-
final int timeout = getConfig().getTimeout();
193-
final boolean in = this.pipe.getUsbEndpoint().getDirection() ==
192+
final boolean in = this.pipe.getUsbEndpoint().getDirection() ==
194193
UsbConst.ENDPOINT_DIRECTION_IN;
195-
final IntBuffer transferred = IntBuffer.allocate(1);
196-
int result;
197194
if (type == UsbConst.ENDPOINT_TYPE_BULK)
198195
{
199-
do
200-
{
201-
result = LibUsb.bulkTransfer(handle, address, buffer,
202-
transferred, timeout);
203-
}
204-
while (in && result == LibUsb.ERROR_TIMEOUT && !isAborting());
205-
if (result < 0)
206-
{
207-
throw new LibUsbException(
208-
"Transfer error on bulk endpoint", result);
209-
}
196+
return transferBulk(handle, address, in, buffer);
210197
}
211198
else if (type == UsbConst.ENDPOINT_TYPE_INTERRUPT)
212199
{
213-
do
214-
{
215-
result = LibUsb.interruptTransfer(handle, address, buffer,
216-
transferred, timeout);
217-
}
218-
while (in && result == LibUsb.ERROR_TIMEOUT && !isAborting());
219-
if (result < 0)
220-
{
221-
throw new LibUsbException(
222-
"Transfer error on interrupt endpoint", result);
223-
}
200+
return transferInterrupt(handle, address, in, buffer);
224201
}
225202
else
226203
{
227204
throw new UsbException("Unsupported endpoint type: " + type);
228205
}
206+
}
207+
208+
/**
209+
* Transfers bulk data from or to the device.
210+
*
211+
* @param handle
212+
* The device handle.
213+
* @param address
214+
* The endpoint address.
215+
* @param in
216+
* If bulk-in transfer.
217+
* @param buffer
218+
* The data buffer.
219+
* @return The number of transferred bytes.
220+
* @throws LibUsbException
221+
* When data transfer fails.
222+
*/
223+
private int transferBulk(final DeviceHandle handle, final byte address,
224+
final boolean in, final ByteBuffer buffer) throws LibUsbException
225+
{
226+
final IntBuffer transferred = IntBuffer.allocate(1);
227+
int result;
228+
do
229+
{
230+
result = LibUsb.bulkTransfer(handle, address, buffer,
231+
transferred, getConfig().getTimeout());
232+
}
233+
while (in && result == LibUsb.ERROR_TIMEOUT && !isAborting());
234+
if (result < 0)
235+
{
236+
throw new LibUsbException(
237+
"Transfer error on bulk endpoint", result);
238+
}
239+
return transferred.get(0);
240+
}
241+
242+
/**
243+
* Transfers interrupt data from or to the device.
244+
*
245+
* @param handle
246+
* The device handle.
247+
* @param address
248+
* The endpoint address.
249+
* @param in
250+
* If interrupt-in transfer.
251+
* @param buffer
252+
* The data buffer.
253+
* @return The number of transferred bytes.
254+
* @throws LibUsbException
255+
* When data transfer fails.
256+
*/
257+
private int transferInterrupt(final DeviceHandle handle,
258+
final byte address, final boolean in, final ByteBuffer buffer)
259+
throws LibUsbException
260+
{
261+
final IntBuffer transferred = IntBuffer.allocate(1);
262+
int result;
263+
do
264+
{
265+
result = LibUsb.interruptTransfer(handle, address, buffer,
266+
transferred, getConfig().getTimeout());
267+
}
268+
while (in && result == LibUsb.ERROR_TIMEOUT && !isAborting());
269+
if (result < 0)
270+
{
271+
throw new LibUsbException(
272+
"Transfer error on interrupt endpoint", result);
273+
}
229274
return transferred.get(0);
230275
}
231276
}

0 commit comments

Comments
 (0)