org.jaudiotagger.audio.asf.util
Class Utils

java.lang.Object
  extended by org.jaudiotagger.audio.asf.util.Utils

public class Utils
extends java.lang.Object

Some static Methods which are used in several Classes.

Author:
Christian Laireiter

Field Summary
static java.lang.String LINE_SEPARATOR
          Stores the default line separator of the current underlying system.
static int MAXIMUM_STRING_LENGTH_ALLOWED
           
 
Constructor Summary
Utils()
           
 
Method Summary
static void checkStringLengthNullSafe(java.lang.String value)
          This method checks given string will not exceed limit in bytes[] when converted UTF-16LE encoding (2 bytes per character) and checks whether the length doesn't exceed 65535 bytes.
static void copy(java.io.InputStream source, java.io.OutputStream dest, long amount)
          effectively copies a specified amount of bytes from one stream to another.
static void flush(java.io.InputStream source, java.io.OutputStream dest)
          Copies all of the source to the destination.
static byte[] getBytes(long value, int byteCount)
          This method will create a byte[] at the size of byteCount and insert the bytes of value (starting from lowset byte) into it.
static byte[] getBytes(java.lang.String source, java.nio.charset.Charset charset)
          Convenience method to convert the given string into a byte sequence which has the format of the charset given.
static java.util.GregorianCalendar getDateOf(java.math.BigInteger fileTime)
          Since date values in ASF files are given in 100 ns steps since first january of 1601 a little conversion must be done.
static boolean isBlank(java.lang.String toTest)
          Tests if the given string is null or just contains whitespace characters.
static boolean isStringLengthValidNullSafe(java.lang.String value)
           
static java.math.BigInteger readBig64(java.io.InputStream stream)
          Reads 8 bytes from stream and interprets them as a UINT64 which is returned as BigInteger.
static byte[] readBinary(java.io.InputStream stream, long size)
          Reads size bytes from the stream.
static java.lang.String readCharacterSizedString(java.io.InputStream stream)
          This method reads a UTF-16 String, which length is given on the number of characters it consists of.
static java.lang.String readFixedSizeUTF16Str(java.io.InputStream stream, int strLen)
          This method reads a UTF-16 encoded String.
static GUID readGUID(java.io.InputStream stream)
          This Method reads a GUID (which is a 16 byte long sequence) from the given raf and creates a wrapper.
static int readUINT16(java.io.InputStream stream)
          Reads 2 bytes from stream and interprets them as UINT16.
static long readUINT32(java.io.InputStream stream)
          Reads 4 bytes from stream and interprets them as UINT32.
static long readUINT64(java.io.InputStream stream)
          Reads long as little endian.
static java.lang.String readUTF16LEStr(java.io.InputStream stream)
          This method reads a UTF-16 encoded String, beginning with a 16-bit value representing the number of bytes needed.
static void writeUINT16(int number, java.io.OutputStream out)
          Writes the given value as UINT16 into the stream.
static void writeUINT32(long number, java.io.OutputStream out)
          Writes the given value as UINT32 into the stream.
static void writeUINT64(long number, java.io.OutputStream out)
          Writes the given value as UINT64 into the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
Stores the default line separator of the current underlying system.


MAXIMUM_STRING_LENGTH_ALLOWED

public static final int MAXIMUM_STRING_LENGTH_ALLOWED
See Also:
Constant Field Values
Constructor Detail

Utils

public Utils()
Method Detail

checkStringLengthNullSafe

public static void checkStringLengthNullSafe(java.lang.String value)
                                      throws java.lang.IllegalArgumentException
This method checks given string will not exceed limit in bytes[] when converted UTF-16LE encoding (2 bytes per character) and checks whether the length doesn't exceed 65535 bytes.

Parameters:
value - The string to check.
Throws:
java.lang.IllegalArgumentException - If byte representation takes more than 65535 bytes.

isStringLengthValidNullSafe

public static boolean isStringLengthValidNullSafe(java.lang.String value)
Parameters:
value -
Returns:
true unless string is too long

copy

public static void copy(java.io.InputStream source,
                        java.io.OutputStream dest,
                        long amount)
                 throws java.io.IOException
effectively copies a specified amount of bytes from one stream to another.

Parameters:
source - stream to read from
dest - stream to write to
amount - amount of bytes to copy
Throws:
java.io.IOException - on I/O errors, and if the source stream depletes before all bytes have been copied.

flush

public static void flush(java.io.InputStream source,
                         java.io.OutputStream dest)
                  throws java.io.IOException
Copies all of the source to the destination.

Parameters:
source - source to read from
dest - stream to write to
Throws:
java.io.IOException - on I/O errors.

getBytes

public static byte[] getBytes(long value,
                              int byteCount)
This method will create a byte[] at the size of byteCount and insert the bytes of value (starting from lowset byte) into it.
You can easily create a Word (16-bit), DWORD (32-bit), QWORD (64 bit) out of the value, ignoring the original type of value, since java automatically performs transformations.
Warning: This method works with unsigned numbers only.

Parameters:
value - The value to be written into the result.
byteCount - The number of bytes the array has got.
Returns:
A byte[] with the size of byteCount containing the lower byte values of value.

getBytes

public static byte[] getBytes(java.lang.String source,
                              java.nio.charset.Charset charset)
Convenience method to convert the given string into a byte sequence which has the format of the charset given.

Parameters:
source - string to convert.
charset - charset to apply
Returns:
the source's binary representation according to the charset.

getDateOf

public static java.util.GregorianCalendar getDateOf(java.math.BigInteger fileTime)
Since date values in ASF files are given in 100 ns steps since first january of 1601 a little conversion must be done.
This method converts a date given in described manner to a calendar.

Parameters:
fileTime - Time in 100ns since 1 jan 1601
Returns:
Calendar holding the date representation.

isBlank

public static boolean isBlank(java.lang.String toTest)
Tests if the given string is null or just contains whitespace characters.

Parameters:
toTest - String to test.
Returns:
see description.

readBig64

public static java.math.BigInteger readBig64(java.io.InputStream stream)
                                      throws java.io.IOException
Reads 8 bytes from stream and interprets them as a UINT64 which is returned as BigInteger.

Parameters:
stream - stream to readm from.
Returns:
a BigInteger which represents the read 8 bytes value.
Throws:
java.io.IOException

readBinary

public static byte[] readBinary(java.io.InputStream stream,
                                long size)
                         throws java.io.IOException
Reads size bytes from the stream.

Parameters:
stream - stream to read from.
size - amount of bytes to read.
Returns:
the read bytes.
Throws:
java.io.IOException - on I/O errors.

readCharacterSizedString

public static java.lang.String readCharacterSizedString(java.io.InputStream stream)
                                                 throws java.io.IOException
This method reads a UTF-16 String, which length is given on the number of characters it consists of.
The stream must be at the number of characters. This number contains the terminating zero character (UINT16).

Parameters:
stream - Input source
Returns:
String
Throws:
java.io.IOException - read errors

readFixedSizeUTF16Str

public static java.lang.String readFixedSizeUTF16Str(java.io.InputStream stream,
                                                     int strLen)
                                              throws java.io.IOException
This method reads a UTF-16 encoded String.
For the use this method the number of bytes used by current string must be known.
The ASF specification recommends that those strings end with a terminating zero. However it also says that it is not always the case.

Parameters:
stream - Input source
strLen - Number of bytes the String may take.
Returns:
read String.
Throws:
java.io.IOException - read errors.

readGUID

public static GUID readGUID(java.io.InputStream stream)
                     throws java.io.IOException
This Method reads a GUID (which is a 16 byte long sequence) from the given raf and creates a wrapper.
Warning :
There is no way of telling if a byte sequence is a guid or not. The next 16 bytes will be interpreted as a guid, whether it is or not.

Parameters:
stream - Input source.
Returns:
A class wrapping the guid.
Throws:
java.io.IOException - happens when the file ends before guid could be extracted.

readUINT16

public static int readUINT16(java.io.InputStream stream)
                      throws java.io.IOException
Reads 2 bytes from stream and interprets them as UINT16.

Parameters:
stream - stream to read from.
Returns:
UINT16 value
Throws:
java.io.IOException - on I/O Errors.

readUINT32

public static long readUINT32(java.io.InputStream stream)
                       throws java.io.IOException
Reads 4 bytes from stream and interprets them as UINT32.

Parameters:
stream - stream to read from.
Returns:
UINT32 value
Throws:
java.io.IOException - on I/O Errors.

readUINT64

public static long readUINT64(java.io.InputStream stream)
                       throws java.io.IOException
Reads long as little endian.

Parameters:
stream - Data source
Returns:
long value
Throws:
java.io.IOException - read error, or eof is reached before long is completed

readUTF16LEStr

public static java.lang.String readUTF16LEStr(java.io.InputStream stream)
                                       throws java.io.IOException
This method reads a UTF-16 encoded String, beginning with a 16-bit value representing the number of bytes needed. The String is terminated with as 16-bit ZERO.

Parameters:
stream - Input source
Returns:
read String.
Throws:
java.io.IOException - read errors.

writeUINT16

public static void writeUINT16(int number,
                               java.io.OutputStream out)
                        throws java.io.IOException
Writes the given value as UINT16 into the stream.

Parameters:
number - value to write.
out - stream to write into.
Throws:
java.io.IOException - On I/O errors

writeUINT32

public static void writeUINT32(long number,
                               java.io.OutputStream out)
                        throws java.io.IOException
Writes the given value as UINT32 into the stream.

Parameters:
number - value to write.
out - stream to write into.
Throws:
java.io.IOException - On I/O errors

writeUINT64

public static void writeUINT64(long number,
                               java.io.OutputStream out)
                        throws java.io.IOException
Writes the given value as UINT64 into the stream.

Parameters:
number - value to write.
out - stream to write into.
Throws:
java.io.IOException - On I/O errors