SunSPOT API V3.0


com.sun.spot.util
Class Utils

java.lang.Object
  extended by com.sun.spot.util.Utils

public class Utils
extends Object

Collection of utility functions


Field Summary
static int SIZE_OF_INT
           
static int SIZE_OF_LONG
           
static int SIZE_OF_SHORT
           
 
Constructor Summary
Utils()
           
 
Method Summary
static int as6BitNumber(int number)
          Truncates an int to 6 bits
static byte[] copy(byte[] aByteArray)
          Make a copy of a byte array
static Vector enumToVector(Enumeration items)
          Convert an Enumeration to a Vector
static byte[] getDataWithCRC(DataInputStream dataInputStream)
          Read data from a stream, checking the CRC.
static int getManifestProperty(String manifestPropertyName, int valueIfNotInManifest)
          Get the value of a property from this suite's manifest (or the manifest of a parent suite).
static String getManifestProperty(String manifestPropertyName, String valueIfNotInManifest)
          Get the value of a property from this suite's manifest (or the manifest of a parent suite).
static int getSystemProperty(String propertyName, int valueIfNotDefined)
          Get the value of a system property.
static boolean isOptionSelected(String optionName, boolean defaultIfAbsent)
          Test whether a boolean option is set in the system properties
static void log(String message)
          Display a log message if the "spot.diagnostics" property is set
static void putDataWithCRC(DataOutputStream dataOutputStream, byte[] data, int offset, int length)
          Put data onto a stream, adding a CRC.
static int readBigEndInt(byte[] byteArray, int offset)
          Read a big-endian int from an array
static long readBigEndLong(byte[] byteArray, int offset)
          Read a big-endian long from an array
static int readBigEndShort(byte[] byteArray, int offset)
          Read a big-endian short from an array
static int readLittleEndInt(byte[] byteArray, int offset)
          Read a little-endian int from an array
static long readLittleEndLong(byte[] byteArray, int offset)
          Read a little-endian long from an array
static int readLittleEndShort(byte[] byteArray, int offset)
          Read a little-endian short from an array
static long readLong(String number)
          Parse a decimal or hexadecimal string to get a long.
static int readNumber(String number)
          Parse a decimal or hexadecimal string to get an int.
static void sleep(long milliseconds)
          Do a Thread.sleep(...), catching and ignoring any InterruptedException
static String[] split(String s, char marker)
          Split a string into parts.
static String stringify(byte[] b)
          Generate a string representation of a byte array.
static String withSpacesReplacedByZeros(String string)
          Generate a String that is a copy of the supplied String but with all spaces replaced by nulls.
static void writeBigEndInt(byte[] byteArray, int offset, int value)
          Write a big-endian int into an array
static void writeBigEndLong(byte[] byteArray, int offset, long value)
          Write a big-endian long into an array
static void writeBigEndShort(byte[] byteArray, int offset, int value)
          Write a big-endian short into an array
static void writeLittleEndInt(byte[] byteArray, int offset, int value)
          Write a little-endian int into an array
static void writeLittleEndLong(byte[] byteArray, int offset, long value)
          Write a little-endian long into an array
static void writeLittleEndShort(byte[] byteArray, int offset, int value)
          Write a little-endian short into an array
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_OF_SHORT

public static int SIZE_OF_SHORT

SIZE_OF_INT

public static int SIZE_OF_INT

SIZE_OF_LONG

public static int SIZE_OF_LONG
Constructor Detail

Utils

public Utils()
Method Detail

log

public static void log(String message)
Display a log message if the "spot.diagnostics" property is set

Parameters:
message - the message to display

copy

public static byte[] copy(byte[] aByteArray)
Make a copy of a byte array

Parameters:
aByteArray - the array to be copied
Returns:
the copy

readNumber

public static int readNumber(String number)
Parse a decimal or hexadecimal string to get an int. The number may start with 0x or x as the hex indicator.

Parameters:
number - the string to be parsed
Returns:
the parsed value

readLong

public static long readLong(String number)
Parse a decimal or hexadecimal string to get a long. The number may start with 0x or x as the hex indicator.

Parameters:
number - the string to be parsed
Returns:
the parsed value

readLittleEndLong

public static long readLittleEndLong(byte[] byteArray,
                                     int offset)
Read a little-endian long from an array

Parameters:
byteArray - the array containing the number as 8 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

readLittleEndInt

public static int readLittleEndInt(byte[] byteArray,
                                   int offset)
Read a little-endian int from an array

Parameters:
byteArray - the array containing the number as 4 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

readBigEndInt

public static int readBigEndInt(byte[] byteArray,
                                int offset)
Read a big-endian int from an array

Parameters:
byteArray - the array containing the number as 4 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

readLittleEndShort

public static int readLittleEndShort(byte[] byteArray,
                                     int offset)
Read a little-endian short from an array

Parameters:
byteArray - the array containing the number as 2 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

readBigEndShort

public static int readBigEndShort(byte[] byteArray,
                                  int offset)
Read a big-endian short from an array

Parameters:
byteArray - the array containing the number as 2 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

readBigEndLong

public static long readBigEndLong(byte[] byteArray,
                                  int offset)
Read a big-endian long from an array

Parameters:
byteArray - the array containing the number as 8 consecutive bytes
offset - the offset into the array of the number
Returns:
the number

writeBigEndLong

public static void writeBigEndLong(byte[] byteArray,
                                   int offset,
                                   long value)
Write a big-endian long into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

writeLittleEndLong

public static void writeLittleEndLong(byte[] byteArray,
                                      int offset,
                                      long value)
Write a little-endian long into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

writeBigEndInt

public static void writeBigEndInt(byte[] byteArray,
                                  int offset,
                                  int value)
Write a big-endian int into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

writeLittleEndInt

public static void writeLittleEndInt(byte[] byteArray,
                                     int offset,
                                     int value)
Write a little-endian int into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

writeLittleEndShort

public static void writeLittleEndShort(byte[] byteArray,
                                       int offset,
                                       int value)
Write a little-endian short into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

writeBigEndShort

public static void writeBigEndShort(byte[] byteArray,
                                    int offset,
                                    int value)
Write a big-endian short into an array

Parameters:
byteArray - the array to contain the number
offset - the offset into the array where the number is to be placed
value - the number

as6BitNumber

public static int as6BitNumber(int number)
Truncates an int to 6 bits

Parameters:
number - the number to truncate
Returns:
the truncated value

sleep

public static void sleep(long milliseconds)
Do a Thread.sleep(...), catching and ignoring any InterruptedException

Parameters:
milliseconds - the period to sleep

getManifestProperty

public static String getManifestProperty(String manifestPropertyName,
                                         String valueIfNotInManifest)
Get the value of a property from this suite's manifest (or the manifest of a parent suite).

Parameters:
manifestPropertyName - the name of the property
valueIfNotInManifest - the value to return if not found
Returns:
the value

getManifestProperty

public static int getManifestProperty(String manifestPropertyName,
                                      int valueIfNotInManifest)
Get the value of a property from this suite's manifest (or the manifest of a parent suite).

Parameters:
manifestPropertyName - the name of the property
valueIfNotInManifest - the value to return if not found
Returns:
the value

getSystemProperty

public static int getSystemProperty(String propertyName,
                                    int valueIfNotDefined)
Get the value of a system property.

Parameters:
propertyName - the name of the property
valueIfNotDefined - the value to return if not found
Returns:
the value

enumToVector

public static Vector enumToVector(Enumeration items)
Convert an Enumeration to a Vector

Parameters:
items - the Enumeration to convert
Returns:
the Vector

stringify

public static String stringify(byte[] b)
Generate a string representation of a byte array.

Parameters:
b - the array
Returns:
the string representation

withSpacesReplacedByZeros

public static String withSpacesReplacedByZeros(String string)
Generate a String that is a copy of the supplied String but with all spaces replaced by nulls.

Parameters:
string - the input String
Returns:
the output String

isOptionSelected

public static boolean isOptionSelected(String optionName,
                                       boolean defaultIfAbsent)
Test whether a boolean option is set in the system properties

Parameters:
optionName -
defaultIfAbsent - the value to return if optionName is absent from system properties
Returns:
true if optionName exists and has the value true, or optionName absent and defaultIfAbsent

split

public static String[] split(String s,
                             char marker)
Split a string into parts. Utils.split("foo:bar", ':') returns String[] {"foo", "bar"}

Parameters:
s - the string to split
marker - the character that marks the separation between parts
Returns:
the parts

getDataWithCRC

public static byte[] getDataWithCRC(DataInputStream dataInputStream)
                             throws IOException
Read data from a stream, checking the CRC. The stream must contain, in order: a big-endian int that is the number of data bytes to read, the data, a 16-bit CRC.

Parameters:
dataInputStream - the stream holding the data
Returns:
the checked data
Throws:
IOException
SpotFatalException - if the length is improbable or the CRC is incorrect

putDataWithCRC

public static void putDataWithCRC(DataOutputStream dataOutputStream,
                                  byte[] data,
                                  int offset,
                                  int length)
                           throws IOException
Put data onto a stream, adding a CRC. The length is written first, as a big-endian int, then the data, and finally the 16-bit CRC.

Parameters:
dataOutputStream - the stream to receive the data
data - the array containing the data
offset - the offset into the array at which the data starts
length - the number of bytes of data
Throws:
IOException

SunSPOT API V3.0


Copyright © 2006, 2007 Sun Microsystems, Inc. All Rights Reserved.