javax.usb.util
Class UsbUtil

java.lang.Object
  extended byjavax.usb.util.UsbUtil

public class UsbUtil
extends java.lang.Object

General utility methods.

Author:
Dan Streetman

Nested Class Summary
static class UsbUtil.SynchronizedUsbDevice
          A synchronized UsbDevice wrapper implementation.
static class UsbUtil.SynchronizedUsbPipe
          A synchronized UsbPipe wrapper implementation.
 
Constructor Summary
UsbUtil()
           
 
Method Summary
static java.lang.String getSpeedString(java.lang.Object object)
          Get a String description of the specified device-speed Object.
static UsbDevice synchronizedUsbDevice(UsbDevice usbDevice)
          Create a synchronized UsbDevice.
static UsbPipe synchronizedUsbPipe(UsbPipe usbPipe)
          Create a synchronized UsbPipe.
static java.lang.String toHexString(byte b)
          Format a byte into a proper length hex String.
static java.lang.String toHexString(int i)
          Format a int into a proper length hex String.
static java.lang.String toHexString(long l)
          Format a long into the specified length hex String.
static java.lang.String toHexString(long l, char c, int min, int max)
          Format a long into the specified length hex String.
static java.lang.String toHexString(short s)
          Format a short into a proper length hex String.
static java.lang.String toHexString(java.lang.String delimiter, byte[] array)
          Format a byte[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, byte[] array, int length)
          Format a byte[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, int[] array)
          Format a int[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, int[] array, int length)
          Format a int[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, long[] array)
          Format a long[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, long[] array, int length)
          Format a long[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, short[] array)
          Format a short[] into a hex String.
static java.lang.String toHexString(java.lang.String delimiter, short[] array, int length)
          Format a short[] into a hex String.
static int toInt(byte byte3, byte byte2, byte byte1, byte byte0)
          Convert 4 bytes into an int.
static int toInt(short mss, short lss)
          Convert 2 shorts into an int.
static long toLong(byte byte7, byte byte6, byte byte5, byte byte4, byte byte3, byte byte2, byte byte1, byte byte0)
          Convert 8 bytes into a long.
static long toLong(int msi, int lsi)
          Convert 2 ints into a long.
static long toLong(short short3, short short2, short short1, short short0)
          Convert 4 shorts into a long.
static short toShort(byte msb, byte lsb)
          Convert 2 bytes into a short.
static int unsignedInt(byte b)
          Get the specified byte's value as an unsigned integer.
static int unsignedInt(short s)
          Get the specified short's value as an unsigned integer.
static long unsignedLong(byte b)
          Get the specified byte's value as an unsigned long.
static long unsignedLong(int i)
          Get the specified int's value as an unsigned long.
static long unsignedLong(short s)
          Get the specified short's value as an unsigned long.
static short unsignedShort(byte b)
          Get the specified byte's value as an unsigned short.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UsbUtil

public UsbUtil()
Method Detail

unsignedShort

public static short unsignedShort(byte b)
Get the specified byte's value as an unsigned short.

This converts the specified byte into a short. The least significant byte (8 bits) of the short will be identical to the byte (8 bits) provided, and the most significant byte (8 bits) of the short will be zero.

For many of the values in this USB API, unsigned bytes are used. However, since Java does not include unsigned bytes in the language, those unsigned bytes must be converted to a larger storage type before being used in unsigned calculations.

Parameters:
b - the byte to convert.
Returns:
An unsigned short representing the specified byte.

unsignedInt

public static int unsignedInt(byte b)
Get the specified byte's value as an unsigned integer.

This converts the specified byte into an integer. The least significant byte (8 bits) of the integer will be identical to the byte (8 bits) provided, and the most significant 3 bytes (24 bits) of the integer will be zero.

For many of the values in this USB API, unsigned bytes are used. However, since Java does not include unsigned bytes in the language, those unsigned bytes must be converted to a larger storage type before being used in unsigned calculations.

Parameters:
b - the byte to convert.
Returns:
An unsigned int representing the specified byte.

unsignedInt

public static int unsignedInt(short s)
Get the specified short's value as an unsigned integer.

This converts the specified byte into an integer. The least significant short (16 bits) of the integer will be identical to the short (16 bits) provided, and the most significant 2 bytes (16 bits) of the integer will be zero.

For many of the values in this USB API, unsigned shorts are used. However, since Java does not include unsigned short in the language, those unsigned shorts must be converted to a larger storage type before being used in unsigned calculations.

Parameters:
s - the short to convert.
Returns:
An unsigned int representing the specified short.

unsignedLong

public static long unsignedLong(byte b)
Get the specified byte's value as an unsigned long.

This converts the specified byte into a long. The least significant byte (8 bits) of the long will be identical to the byte (8 bits) provided, and the most significant 7 bytes (56 bits) of the long will be zero.

For many of the values in this USB API, unsigned bytes are used. However, since Java does not include unsigned bytes in the language, those unsigned bytes must be converted to a larger storage type before being used in unsigned calculations.

Parameters:
b - the byte to convert.
Returns:
An unsigned long representing the specified byte.

unsignedLong

public static long unsignedLong(short s)
Get the specified short's value as an unsigned long.

This converts the specified byte into a long. The least significant short (16 bits) of the long will be identical to the short (16 bits) provided, and the most significant 6 bytes (48 bits) of the long will be zero.

For many of the values in this USB API, unsigned shorts are used. However, since Java does not include unsigned short in the language, those unsigned shorts must be converted to a larger storage type before being used in unsigned calculations.

Parameters:
s - the short to convert.
Returns:
An unsigned long representing the specified short.

unsignedLong

public static long unsignedLong(int i)
Get the specified int's value as an unsigned long.

This converts the specified int into a long. The least significant int (32 bits) of the long will be identical to the int (32 bits) provided, and the most significant int (32 bits) of the long will be zero.

Parameters:
i - the int to convert.
Returns:
An unsigned long representing the specified int.

toShort

public static short toShort(byte msb,
                            byte lsb)
Convert 2 bytes into a short.

This converts the 2 bytes into a short. The msb will be the high byte (8 bits) of the short, and the lsb will be the low byte (8 bits) of the short.

Parameters:
msb - The Most Significant Byte.
lsb - The Least Significant Byte.
Returns:
A short representing the bytes.

toInt

public static int toInt(byte byte3,
                        byte byte2,
                        byte byte1,
                        byte byte0)
Convert 4 bytes into an int.

This converts the 4 bytes into an int.

Parameters:
byte3 - The byte to be left-shifted 24 bits.
byte2 - The byte to be left-shifted 16 bits.
byte1 - The byte to be left-shifted 8 bits.
byte0 - The byte that will not be left-shifted.
Returns:
An int representing the bytes.

toLong

public static long toLong(byte byte7,
                          byte byte6,
                          byte byte5,
                          byte byte4,
                          byte byte3,
                          byte byte2,
                          byte byte1,
                          byte byte0)
Convert 8 bytes into a long.

This converts the 8 bytes into a long.

Parameters:
byte7 - The byte to be left-shifted 56 bits.
byte6 - The byte to be left-shifted 48 bits.
byte5 - The byte to be left-shifted 40 bits.
byte4 - The byte to be left-shifted 32 bits.
byte3 - The byte to be left-shifted 24 bits.
byte2 - The byte to be left-shifted 16 bits.
byte1 - The byte to be left-shifted 8 bits.
byte0 - The byte that will not be left-shifted.
Returns:
A long representing the bytes.

toInt

public static int toInt(short mss,
                        short lss)
Convert 2 shorts into an int.

This converts the 2 shorts into an int.

Parameters:
mss - The Most Significant Short.
lss - The Least Significant Short.
Returns:
An int representing the shorts.

toLong

public static long toLong(short short3,
                          short short2,
                          short short1,
                          short short0)
Convert 4 shorts into a long.

This converts the 4 shorts into a long.

Parameters:
short3 - The short to be left-shifted 48 bits.
short2 - The short to be left-shifted 32 bits.
short1 - The short to be left-shifted 16 bits.
short0 - The short that will not be left-shifted.
Returns:
A long representing the shorts.

toLong

public static long toLong(int msi,
                          int lsi)
Convert 2 ints into a long.

This converts the 2 ints into a long.

Parameters:
msi - The Most Significant Int.
lsi - The Least Significant Int.
Returns:
A long representing the ints.

toHexString

public static java.lang.String toHexString(byte b)
Format a byte into a proper length hex String.

This is identical to Long.toHexString() except this pads (with 0's) to the proper size.

Parameters:
b - the byte to convert

toHexString

public static java.lang.String toHexString(short s)
Format a short into a proper length hex String.

This is identical to Long.toHexString() except this pads (with 0's) to the proper size.

Parameters:
s - the short to convert

toHexString

public static java.lang.String toHexString(int i)
Format a int into a proper length hex String.

This is identical to Long.toHexString() except this pads (with 0's) to the proper size.

Parameters:
i - the integer to convert

toHexString

public static java.lang.String toHexString(long l)
Format a long into the specified length hex String.

This is identical to Long.toHexString() except this pads (with 0's) to the proper size.

Parameters:
l - the long to convert

toHexString

public static java.lang.String toHexString(long l,
                                           char c,
                                           int min,
                                           int max)
Format a long into the specified length hex String.

This is identical to Long.toHexString() except this pads (with 0's), or truncates, to the specified size. If max < min the functionaliy is exactly as Long.toHexString().

Parameters:
l - the long to convert
c - the character to use for padding
min - the min length of the resulting String
max - the max length of the resulting String

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           byte[] array,
                                           int length)
Format a byte[] into a hex String.

This creates a String by concatenating the result of delimiter + toHexString(byte) for each byte in the array. If the specified length is greater than the actual array length, the array length is used. If the specified length (or array length) is 0 or less, the resulting String will be an empty String.

Parameters:
delimiter - The delimiter to prefix every byte with.
array - The byte[] to convert.
length - The number of bytes to use.
Returns:
A String representing the byte[].
Throws:
java.lang.NullPointerException - If the byte[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           short[] array,
                                           int length)
Format a short[] into a hex String.

This creates a String by concatenating the result of delimiter + toHexString(short) for each short in the array. If the specified length is greater than the actual array length, the array length is used. If the specified length (or array length) is 0 or less, the resulting String will be an empty String.

Parameters:
delimiter - The delimiter to prefix every short with.
array - The short[] to convert.
length - The number of shorts to use.
Returns:
A String representing the short[].
Throws:
java.lang.NullPointerException - If the short[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           int[] array,
                                           int length)
Format a int[] into a hex String.

This creates a String by concatenating the result of delimiter + toHexString(int) for each int in the array. If the specified length is greater than the actual array length, the array length is used. If the specified length (or array length) is 0 or less, the resulting String will be an empty String.

Parameters:
delimiter - The delimiter to prefix every int with.
array - The int[] to convert.
length - The number of ints to use.
Returns:
A String representing the int[].
Throws:
java.lang.NullPointerException - If the int[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           long[] array,
                                           int length)
Format a long[] into a hex String.

This creates a String by concatenating the result of delimiter + toHexString(long) for each long in the array. If the specified length is greater than the actual array length, the array length is used. If the specified length (or array length) is 0 or less, the resulting String will be an empty String.

Parameters:
delimiter - The delimiter to prefix every long with.
array - The long[] to convert.
length - The number of longs to use.
Returns:
A String representing the long[].
Throws:
java.lang.NullPointerException - If the long[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           byte[] array)
Format a byte[] into a hex String.

This calls toHexString(delimiter, array, array.length).

Parameters:
delimiter - The delimiter to prefix every byte with.
array - The byte[] to convert.
Returns:
A String representing the byte[].
Throws:
java.lang.NullPointerException - If the byte[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           short[] array)
Format a short[] into a hex String.

This calls toHexString(delimiter, array, array.length).

Parameters:
delimiter - The delimiter to prefix every short with.
array - The short[] to convert.
Returns:
A String representing the short[].
Throws:
java.lang.NullPointerException - If the short[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           int[] array)
Format a int[] into a hex String.

This calls toHexString(delimiter, array, array.length).

Parameters:
delimiter - The delimiter to prefix every int with.
array - The int[] to convert.
Returns:
A String representing the int[].
Throws:
java.lang.NullPointerException - If the int[] is null.

toHexString

public static java.lang.String toHexString(java.lang.String delimiter,
                                           long[] array)
Format a long[] into a hex String.

This calls toHexString(delimiter, array, array.length).

Parameters:
delimiter - The delimiter to prefix every long with.
array - The long[] to convert.
Returns:
A String representing the long[].
Throws:
java.lang.NullPointerException - If the long[] is null.

getSpeedString

public static java.lang.String getSpeedString(java.lang.Object object)
Get a String description of the specified device-speed Object.

The String will be one of:

The string "null" is used for a null Object. The string "Invalid" is used for an Object that does not correspond to any of those defined in UsbConst.

Parameters:
object - The device-speed Object.
Returns:
A String representing the speed Object.
See Also:
Low Speed., Full Speed., Unknown Speed.

synchronizedUsbDevice

public static UsbDevice synchronizedUsbDevice(UsbDevice usbDevice)
Create a synchronized UsbDevice.

Parameters:
usbDevice - The unsynchronized UsbDevice.
Returns:
A synchronized UsbDevice.

synchronizedUsbPipe

public static UsbPipe synchronizedUsbPipe(UsbPipe usbPipe)
Create a synchronized UsbPipe.

Parameters:
usbPipe - The unsynchronized UsbPipe.
Returns:
A synchronized UsbPipe.


Copyright © 2009 Eurotech Inc. All rights reserved.