Code cleanups

This commit is contained in:
Klaus Reimer 2013-04-12 21:40:56 +02:00
parent 2d50f26150
commit d0f7070fd7
6 changed files with 71 additions and 63 deletions

View File

@ -7,6 +7,9 @@ package de.ailis.usb4java;
import java.io.Serializable;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import de.ailis.usb4java.descriptors.SimpleUsbDeviceDescriptor;
/**
@ -59,14 +62,12 @@ public final class DeviceId implements Serializable
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + this.busNumber;
result = prime * result + this.deviceAddress;
result = prime * result + this.portNumber;
result = prime * result + ((this.deviceDescriptor == null) ? 0 :
this.deviceDescriptor.hashCode());
return result;
return new HashCodeBuilder()
.append(this.busNumber)
.append(this.deviceAddress)
.append(this.portNumber)
.append(this.deviceDescriptor)
.toHashCode();
}
@Override
@ -75,17 +76,13 @@ public final class DeviceId implements Serializable
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
DeviceId other = (DeviceId) obj;
if (this.busNumber != other.busNumber) return false;
if (this.deviceAddress != other.deviceAddress) return false;
if (this.portNumber != other.portNumber) return false;
if (this.deviceDescriptor == null)
{
if (other.deviceDescriptor != null) return false;
}
else if (!this.deviceDescriptor.equals(other.deviceDescriptor))
return false;
return true;
final DeviceId other = (DeviceId) obj;
return new EqualsBuilder()
.append(this.busNumber, other.busNumber)
.append(this.deviceAddress, other.deviceAddress)
.append(this.portNumber, other.portNumber)
.append(this.deviceDescriptor, other.deviceDescriptor)
.isEquals();
}
/**
@ -127,12 +124,12 @@ public final class DeviceId implements Serializable
{
return this.deviceDescriptor;
}
@Override
public String toString()
{
return String.format("Bus %03d Device %03d: ID %04x:%04x",
this.busNumber, this.deviceAddress,
this.busNumber, this.deviceAddress,
this.deviceDescriptor.idVendor(),
this.deviceDescriptor.idProduct());
}

View File

@ -152,25 +152,29 @@ public final class IrpQueue extends AbstractIrpQueue<UsbIrp>
final int size =
Math.min(len - read, descriptor.wMaxPacketSize() & 0xffff);
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
IntBuffer transferred = IntBuffer.allocate(1);
final IntBuffer transferred = IntBuffer.allocate(1);
int result;
if (type == UsbConst.ENDPOINT_TYPE_BULK)
{
result =
LibUSB.bulkTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0) throw new LibUsbException(
"Unable to read from bulk endpoint", result);
result = LibUSB.bulkTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0)
{
throw new LibUsbException(
"Unable to read from bulk endpoint", result);
}
}
else if (type == UsbConst.ENDPOINT_TYPE_INTERRUPT)
{
result =
LibUSB.interruptTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0) throw new LibUsbException(
"Unable to read from interrupt endpoint", result);
result = LibUSB.interruptTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0)
{
throw new LibUsbException(
"Unable to read from interrupt endpoint", result);
}
}
else
{
@ -214,23 +218,29 @@ public final class IrpQueue extends AbstractIrpQueue<UsbIrp>
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
buffer.put(data, offset + written, size);
buffer.rewind();
IntBuffer transferred = IntBuffer.allocate(1);
final IntBuffer transferred = IntBuffer.allocate(1);
int result;
if (type == UsbConst.ENDPOINT_TYPE_BULK)
{
result = LibUSB.bulkTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0) throw new LibUsbException(
"Unable to write to bulk endpoint", result);
if (result < 0)
{
throw new LibUsbException(
"Unable to write to bulk endpoint", result);
}
}
else if (type == UsbConst.ENDPOINT_TYPE_INTERRUPT)
{
result = LibUSB.interruptTransfer(handle,
descriptor.bEndpointAddress(), buffer, transferred,
getConfig().getTimeout());
if (result < 0) throw new LibUsbException(
"Unable to write to interrupt endpoint", result);
if (result < 0)
{
throw new LibUsbException(
"Unable to write to interrupt endpoint", result);
}
}
else
{

View File

@ -122,17 +122,18 @@ public class Usb4JavaDevice implements UsbDevice
new ArrayList<Usb4JavaConfiguration>(numConfigurations);
for (int i = 0; i < numConfigurations; i += 1)
{
ConfigDescriptor configDescriptor = new ConfigDescriptor();
int result =
LibUSB.getConfigDescriptor(device, i, configDescriptor);
final ConfigDescriptor configDescriptor = new ConfigDescriptor();
final int result = LibUSB.getConfigDescriptor(device, i,
configDescriptor);
if (result < 0)
{
throw new LibUsbException("Unable to get configuation " + i
+ " for device " + id, result);
}
try
{
Usb4JavaConfiguration config =
new Usb4JavaConfiguration(this,
configDescriptor);
final Usb4JavaConfiguration config = new Usb4JavaConfiguration(
this, configDescriptor);
configurations.add(config);
this.configMapping.put(configDescriptor.bConfigurationValue(),
config);
@ -168,7 +169,7 @@ public class Usb4JavaDevice implements UsbDevice
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Usb4JavaDevice other = (Usb4JavaDevice) obj;
final Usb4JavaDevice other = (Usb4JavaDevice) obj;
return this.id.equals(other.id);
}
@ -218,8 +219,8 @@ public class Usb4JavaDevice implements UsbDevice
final Device device = this.manager.getLibUsbDevice(this.id);
try
{
DeviceHandle handle = new DeviceHandle();
int result = LibUSB.open(device, handle);
final DeviceHandle handle = new DeviceHandle();
final int result = LibUSB.open(device, handle);
if (result < 0)
{
throw new LibUsbException("Can't open device "

View File

@ -64,7 +64,7 @@ public final class UsbDeviceManager
throw new IllegalArgumentException("rootHub must be set");
this.rootHub = rootHub;
this.context = new Context();
int result = LibUSB.init(this.context);
final int result = LibUSB.init(this.context);
if (result != 0)
throw new LibUsbException("Unable to initialize libusb", result);
}
@ -94,7 +94,7 @@ public final class UsbDeviceManager
final int addressNumber = LibUSB.getDeviceAddress(device);
final int portNumber = LibUSB.getPortNumber(device);
final DeviceDescriptor deviceDescriptor = new DeviceDescriptor();
int result = LibUSB.getDeviceDescriptor(device, deviceDescriptor);
final int result = LibUSB.getDeviceDescriptor(device, deviceDescriptor);
if (result < 0)
{
LOG.warning("Unable to get device descriptor for device " +
@ -169,13 +169,13 @@ public final class UsbDeviceManager
*/
private void processRemovedDevices(final Set<Usb4JavaDevice> current)
{
Iterator<Usb4JavaDevice> it = this.devices.values().iterator();
final Iterator<Usb4JavaDevice> it = this.devices.values().iterator();
while (it.hasNext())
{
Usb4JavaDevice device = it.next();
final Usb4JavaDevice device = it.next();
if (!current.contains(device))
{
Usb4JavaDevice parent = this.devices.get(device.getId());
final Usb4JavaDevice parent = this.devices.get(device.getId());
if (parent == null)
this.rootHub.disconnectUsbDevice(device);
else if (parent.isUsbHub())
@ -199,12 +199,12 @@ public final class UsbDeviceManager
{
if (!this.devices.containsValue(device))
{
DeviceId parentId = device.getParentId();
final DeviceId parentId = device.getParentId();
if (parentId == null)
this.rootHub.connectUsbDevice(device);
else
{
Usb4JavaDevice parent = this.devices.get(parentId);
final Usb4JavaDevice parent = this.devices.get(parentId);
if (parent != null && parent.isUsbHub())
{
((Usb4JavaHub) parent).connectUsbDevice(device);
@ -242,7 +242,7 @@ public final class UsbDeviceManager
if (id == null) throw new IllegalArgumentException("id must be set");
final DeviceList devices = new DeviceList();
int result = LibUSB.getDeviceList(this.context, devices);
final int result = LibUSB.getDeviceList(this.context, devices);
if (result < 0)
throw new Usb4JavaRuntimeException("Unable to get USB device list",
result);

View File

@ -453,7 +453,7 @@ public final class LibUSB
/** Device sent more data than requested. */
public static final int TRANSFER_OVERFLOW = 6;
/** The maximum size of a string (Unicode). */
private static final int MAX_STRING_SIZE = 126;
@ -670,7 +670,7 @@ public final class LibUSB
* @return the wMaxPacketSize value {@link #ERROR_NOT_FOUND} if the endpoint
* does not exist {@link #ERROR_OTHER} on other failure
*/
public static native int getMaxPacketSize(final Device device,
public static native int getMaxPacketSize(final Device device,
int endpoint);
/**
@ -745,7 +745,7 @@ public final class LibUSB
* {@link #ERROR_NO_DEVICE} if the device has been disconnected
* another error on other failure
*/
public static native int open(final Device device,
public static native int open(final Device device,
final DeviceHandle handle);
/**
@ -1157,9 +1157,9 @@ public final class LibUSB
final int index)
{
if (handle == null || index == 0) return null;
StringBuffer buffer = new StringBuffer();
if (getStringDescriptorAscii(handle, index, buffer, MAX_STRING_SIZE)
>= 0)
final StringBuffer buffer = new StringBuffer();
if (getStringDescriptorAscii(handle, index, buffer,
MAX_STRING_SIZE) >= 0)
{
return buffer.toString();
}

View File

@ -86,7 +86,7 @@ public final class DumpUtils
*/
public static String toHexDump(final ByteBuffer bytes)
{
int columns = 16;
final int columns = 16;
bytes.rewind();
if (!bytes.hasRemaining()) return "";
final StringBuilder builder = new StringBuilder();