net.freeutils.tnef

Class TNEFUtils

public class TNEFUtils extends Object

The TNEFUtils class provides utility methods used by the TNEF processing classes.

Since: 2003-04-25

Method Summary
static intattID(int att)
Returns the ID part of a 32-bit combined attribute type and ID value.
static intattribute(int atp, int id)
Returns a 32-bit value containing a combined attribute type and ID.
static intattType(int att)
Returns the type part of a 32-bit combined attribute type and ID value.
static intcalculateChecksum(byte[] data)
Returns the checksum of a given byte array.
static intcalculateChecksum(byte[] data, int offset, int length)
Returns the checksum of a range of bytes within a given byte array.
static intcalculateChecksum(RawInputStream ris)
Returns the checksum of all the data in a given RawInputStream.
static intcalculateCRC32(byte[] buf, int off, int len)
Calculates the CRC32 of the given bytes.
static StringcreateString(byte[] bytes, int offset, int length)
Creates a String from a C-style null terminated byte sequence.
static StringcreateStringUnicode(byte[] bytes, int offset, int length)
Creates a String from a C-style null terminated Unicode byte sequence.
static byte[]decompressRTF(byte[] src)
Decompresses compressed-RTF data.
static StringgetConstName(Class cls, String constPrefix, long value)
Returns the name of a constant which is defined in given Class, has a name beginning with given prefix, and has given value.
static intgetU16(int b1, int b2)
Returns an unsigned 16-bit value from little-endian ordered bytes.
static intgetU16(byte[] buf, int offset)
Returns an unsigned 16-bit value from little-endian ordered bytes.
static longgetU32(int b1, int b2, int b3, int b4)
Returns an unsigned 32-bit value from little-endian ordered bytes.
static longgetU32(byte[] buf, int offset)
Returns an unsigned 32-bit value from little-endian ordered bytes.
static longgetU64(byte[] buf, int offset)
Returns a 64-bit value from little-endian ordered bytes.
static intgetU8(byte[] buf, int offset)
Returns an unsigned 8-bit value from a byte array.
static booleanisTNEFMimeType(String mimeType)
Checks whether the given string contains a TNEF mime type
static intread(InputStream in, byte[] b, int off, int min, int max)
Reads bytes from a stream.
static StringremoveTerminatingNulls(String s)
Removes all null characters ('\0') from the end of a given String.
static Stringreplace(String s, String search, String replace)
Replaces all occurrences of given substring within string with a replacement string.
static byte[]toGUID(String guid)
Converts a GUID string to a byte array.
static StringtoHexString(byte[] bytes)
Creates a String containing the hexadecimal representation of the given bytes.
static StringtoHexString(byte[] bytes, int max)
Creates a String containing the hexadecimal representation of the given bytes.
static StringtoHexString(byte[] bytes, int offset, int len, int max)
Creates a String containing the hexadecimal representation of the given bytes.

Method Detail

attID

public static int attID(int att)
Returns the ID part of a 32-bit combined attribute type and ID value.

Parameters: att the combined attribute type and ID value

Returns: the ID part of a 32-bit combined attribute type and ID value

attribute

public static int attribute(int atp, int id)
Returns a 32-bit value containing a combined attribute type and ID.

Parameters: atp the attribute type id the attribute ID

Returns: a 32-bit value containing a combined attribute type and ID

attType

public static int attType(int att)
Returns the type part of a 32-bit combined attribute type and ID value.

Parameters: att the combined attribute type and ID value

Returns: the type part of a 32-bit combined attribute type and ID value

calculateChecksum

public static int calculateChecksum(byte[] data)
Returns the checksum of a given byte array.

Parameters: data the byte array on which to calculate the checksum

Returns: the checksum of a given byte array

calculateChecksum

public static int calculateChecksum(byte[] data, int offset, int length)
Returns the checksum of a range of bytes within a given byte array.

Parameters: data the byte array on which to calculate the checksum offset the offset within the array from which to begin length the number of bytes to calculate checksum on

Returns: the checksum of a range of bytes within a given byte array

calculateChecksum

public static int calculateChecksum(RawInputStream ris)
Returns the checksum of all the data in a given RawInputStream. The stream's current position is not modified.

Parameters: ris the stream from which the data is read

Returns: the checksum of all the data in a given RawInputStream

Throws: IOException if an I/O error occurs

calculateCRC32

public static int calculateCRC32(byte[] buf, int off, int len)

Deprecated: use CompressedRTFInputStream instead

Calculates the CRC32 of the given bytes. The CRC32 calculation is similar to the standard one as demonstrated in RFC 1952, but with the inversion (before and after the calculation) omitted.

Parameters: buf the byte array to calculate CRC32 on off the offset within buf at which the CRC32 calculation will start len the number of bytes on which to calculate the CRC32

Returns: the CRC32 value

createString

public static String createString(byte[] bytes, int offset, int length)
Creates a String from a C-style null terminated byte sequence. The null terminated byte sequence is interpreted as 8-bit ISO-8859-1 characters (a.k.a. ISO-Latin-1), which is a superset of US-ASCII. This way we don't lose any 8-bit values and remain fully compatible: If the source charset is unknown, a str.getByte("ISO8859_1") will reconstruct the exact original byte sequence which the application can then process in any charset it sees fit.

Parameters: bytes a byte array containing a C-style null terminated string offset the offset within bytes where the string begins length the length of the C-style string in bytes, which may include any number of terminating null ('\0') characters

Returns: a String containing the C-style string's characters, interpreted as ISO-8859-1 characters

createStringUnicode

public static String createStringUnicode(byte[] bytes, int offset, int length)
Creates a String from a C-style null terminated Unicode byte sequence. The null terminated byte sequence is interpreted as 16-bit Unicode characters (UTF-16), stored in Little Endian order.

Parameters: bytes a byte array containing a C-style null terminated Unicode string offset the offset within bytes where the string begins length the length of the C-style string in bytes, which may include any number of terminating null ('\0') characters

Returns: a String containing the C-style string's characters, interpreted as Unicode (UTF-16 Little Endian) characters

decompressRTF

public static byte[] decompressRTF(byte[] src)

Deprecated: use CompressedRTFInputStream instead

Decompresses compressed-RTF data.

Parameters: src the compressed-RTF data bytes

Returns: an array containing the decompressed bytes

Throws: IllegalArgumentException if src does not contain valid compressed-RTF bytes

getConstName

public static String getConstName(Class cls, String constPrefix, long value)
Returns the name of a constant which is defined in given Class, has a name beginning with given prefix, and has given value. If the constant's name cannot be found, the value is returned as a hex String.

Parameters: cls the Class containing the constant constPrefix the prefix of the constant name (used in grouping constants) value the constant's value

Returns: the name of the constant

getU16

public static int getU16(int b1, int b2)
Returns an unsigned 16-bit value from little-endian ordered bytes.

Parameters: b1 first byte value b2 second byte value

Returns: an unsigned 16-bit value as an int

getU16

public static int getU16(byte[] buf, int offset)
Returns an unsigned 16-bit value from little-endian ordered bytes.

Parameters: buf a byte array from which byte values are taken offset the offset within buf from which byte values are taken

Returns: an unsigned 16-bit value as an int

getU32

public static long getU32(int b1, int b2, int b3, int b4)
Returns an unsigned 32-bit value from little-endian ordered bytes.

Parameters: b1 first byte value b2 second byte value b3 third byte value b4 fourth byte value

Returns: an unsigned 32-bit value as a long

getU32

public static long getU32(byte[] buf, int offset)
Returns an unsigned 32-bit value from little-endian ordered bytes.

Parameters: buf a byte array from which byte values are taken offset the offset within buf from which byte values are taken

Returns: an unsigned 32-bit value as a long

getU64

public static long getU64(byte[] buf, int offset)
Returns a 64-bit value from little-endian ordered bytes.

Parameters: buf a byte array from which byte values are taken offset the offset within buf from which byte values are taken

Returns: a 64-bit value as a long

getU8

public static int getU8(byte[] buf, int offset)
Returns an unsigned 8-bit value from a byte array.

Parameters: buf a byte array from which byte value is taken offset the offset within buf from which byte value is taken

Returns: an unsigned 8-bit value as an int

isTNEFMimeType

public static boolean isTNEFMimeType(String mimeType)
Checks whether the given string contains a TNEF mime type

Parameters: mimeType the mimeType to check

Returns: true if the given string contains a TNEF mime type, false otherwise

read

public static int read(InputStream in, byte[] b, int off, int min, int max)
Reads bytes from a stream. The number of bytes read is no less than the given minimum and no more than the given maximum.

Parameters: in the input stream to read from b the byte array to read into off the offset within the byte array to start reading into min the minimum number of bytes to read max the maximum number of bytes to read

Returns: the number of bytes read

Throws: IOException if an error occurs or there are less than the minimum number of bytes in the stream

removeTerminatingNulls

public static String removeTerminatingNulls(String s)
Removes all null characters ('\0') from the end of a given String. Useful for converting a C-style null terminated string to a Java String.

Parameters: s a String

Returns: a String identical to the given string, with trailing null characters removed

replace

public static String replace(String s, String search, String replace)
Replaces all occurrences of given substring within string with a replacement string.

Parameters: s the string to be modified search the substring to search for replace the string with which to replace occurrences of the search substring

Returns: a new string consisting of the given string, with all occurrences of search string replaced by replace string. If given string or search string are empty or null, the string itself is returned.

toGUID

public static byte[] toGUID(String guid)

Deprecated: use the GUID class for GUID manipulation

Converts a GUID string to a byte array. A GUID string can contain only hex digits, and optional dashes. All dashes are ignored.

Parameters: guid the GUID string

Returns: the equivalent GUID byte array

Throws: IllegalArgumentException if the given string does not represent a valid GUID

toHexString

public static String toHexString(byte[] bytes)
Creates a String containing the hexadecimal representation of the given bytes.

Parameters: bytes a byte array whose content is to be displayed

Returns: a String containing the hexadecimal representation of the given bytes

toHexString

public static String toHexString(byte[] bytes, int max)
Creates a String containing the hexadecimal representation of the given bytes.

If {@code max} is non-negative and {@code bytes.length > max}, then the first {@code max} bytes are returned, followed by a human-readable indication that there are {@code bytes.length} total bytes of data including those that are not returned.

Parameters: bytes a byte array whose content is to be displayed max the maximum number of bytes to be displayed (-1 means no limit)

Returns: a String containing the hexadecimal representation of the given bytes

toHexString

public static String toHexString(byte[] bytes, int offset, int len, int max)
Creates a String containing the hexadecimal representation of the given bytes.

If {@code max} is non-negative and {@code len > max}, then the first {@code max} bytes are returned, followed by a human-readable indication that there are {@code len} total bytes of data including those that are not returned.

In particular, {@code offset + len} can extend beyond the array boundaries, as long as {@code offset + max} is still within them, resulting in {@code max} bytes returned followed by an indication that there are {@code len} total data bytes (including those that are not returned).

Parameters: bytes a byte array whose content is to be displayed offset the offset within the byte array to start at len the number of bytes max the maximum number of bytes to be displayed (-1 means no limit)

Returns: a String containing the hexadecimal representation of the given bytes