FileDocCategorySizeDatePackage
Util.javaAPI DocApache Poi 3.0.111102Tue Jun 26 21:26:00 BST 2007org.apache.poi.hpsf

Util

public class Util extends Object

Provides various static utility methods.

author
Rainer Klute (klute@rainer-klute.de)
version
$Id: Util.java 550886 2007-06-26 17:58:28Z klute $
since
2002-02-09

Fields Summary
public static final long
EPOCH_DIFF

The difference between the Windows epoch (1601-01-01 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in milliseconds: 11644473600000L. (Use your favorite spreadsheet program to verify the correctness of this value. By the way, did you notice that you can tell from the epochs which operating system is the modern one? :-))

Constructors Summary
Methods Summary
public static byte[]cat(byte[][] byteArrays)

Concatenates the contents of several byte arrays into a single one.

param
byteArrays The byte arrays to be concatened.
return
A new byte array containing the concatenated byte arrays.

        int capacity = 0;
        for (int i = 0; i < byteArrays.length; i++)
            capacity += byteArrays[i].length;
        final byte[] result = new byte[capacity];
        int r = 0;
        for (int i = 0; i < byteArrays.length; i++)
            for (int j = 0; j < byteArrays[i].length; j++)
                result[r++] = byteArrays[i][j];
        return result;
    
public static voidcopy(byte[] src, int srcOffset, int length, byte[] dst, int dstOffset)

Copies a part of a byte array into another byte array.

param
src The source byte array.
param
srcOffset Offset in the source byte array.
param
length The number of bytes to copy.
param
dst The destination byte array.
param
dstOffset Offset in the destination byte array.

        for (int i = 0; i < length; i++)
            dst[dstOffset + i] = src[srcOffset + i];
    
public static byte[]copy(byte[] src, int offset, int length)

Copies bytes from a source byte array into a new byte array.

param
src Copy from this byte array.
param
offset Start copying here.
param
length Copy this many bytes.
return
The new byte array. Its length is number of copied bytes.

        final byte[] result = new byte[length];
        copy(src, offset, length, result, 0);
        return result;
    
public static longdateToFileTime(java.util.Date date)

Converts a {@link Date} into a filetime.

param
date The date to be converted
return
The filetime
see
#filetimeToDate(long)
see
#filetimeToDate(int, int)

        long ms_since_19700101 = date.getTime();
        long ms_since_16010101 = ms_since_19700101 + EPOCH_DIFF;
        return ms_since_16010101 * (1000 * 10);
    
public static booleanequal(byte[] a, byte[] b)

Checks whether two byte arrays a and b are equal. They are equal

  • if they have the same length and

  • if for each i with i >= 0 and i < a.length holds a[i] == b[i].

param
a The first byte array
param
b The first byte array
return
true if the byte arrays are equal, else false

        if (a.length != b.length)
            return false;
        for (int i = 0; i < a.length; i++)
            if (a[i] != b[i])
                return false;
        return true;
    
public static booleanequals(java.util.Collection c1, java.util.Collection c2)

Checks whether two collections are equal. Two collections C1 and C2 are equal, if the following conditions are true:

  • For each c1i (element of C1) there is a c2j (element of C2), and c1i equals c2j.

  • For each c2i (element of C2) there is a c1j (element of C1) and c2i equals c1j.

param
c1 the first collection
param
c2 the second collection
return
true if the collections are equal, else false.

        final Object[] o1 = c1.toArray();
        final Object[] o2 = c2.toArray();
        return internalEquals(o1, o2);
    
public static booleanequals(java.lang.Object[] c1, java.lang.Object[] c2)

Compares to object arrays with regarding the objects' order. For example, [1, 2, 3] and [2, 1, 3] are equal.

param
c1 The first object array.
param
c2 The second object array.
return
true if the object arrays are equal, false if they are not.

        final Object[] o1 = (Object[]) c1.clone();
        final Object[] o2 = (Object[]) c2.clone();
        return internalEquals(o1, o2);
    
public static java.util.DatefiletimeToDate(int high, int low)

Converts a Windows FILETIME into a {@link Date}. The Windows FILETIME structure holds a date and time associated with a file. The structure identifies a 64-bit integer specifying the number of 100-nanosecond intervals which have passed since January 1, 1601. This 64-bit value is split into the two double words stored in the structure.

param
high The higher double word of the FILETIME structure.
param
low The lower double word of the FILETIME structure.
return
The Windows FILETIME as a {@link Date}.



                                                                                           
            
    
        final long filetime = ((long) high) << 32 | (low & 0xffffffffL);
        return filetimeToDate(filetime);
    
public static java.util.DatefiletimeToDate(long filetime)

Converts a Windows FILETIME into a {@link Date}. The Windows FILETIME structure holds a date and time associated with a file. The structure identifies a 64-bit integer specifying the number of 100-nanosecond intervals which have passed since January 1, 1601.

param
filetime The filetime to convert.
return
The Windows FILETIME as a {@link Date}.

        final long ms_since_16010101 = filetime / (1000 * 10);
        final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
        return new Date(ms_since_19700101);
    
private static booleaninternalEquals(java.lang.Object[] o1, java.lang.Object[] o2)

        for (int i1 = 0; i1 < o1.length; i1++)
        {
            final Object obj1 = o1[i1];
            boolean matchFound = false;
            for (int i2 = 0; !matchFound && i2 < o1.length; i2++)
            {
                final Object obj2 = o2[i2];
                if (obj1.equals(obj2))
                {
                    matchFound = true;
                    o2[i2] = null;
                }
            }
            if (!matchFound)
                return false;
        }
        return true;
    
public static byte[]pad4(byte[] ba)

Pads a byte array with 0x00 bytes so that its length is a multiple of 4.

param
ba The byte array to pad.
return
The padded byte array.

        final int PAD = 4;
        final byte[] result;
        int l = ba.length % PAD;
        if (l == 0)
            result = ba;
        else
        {
            l = PAD - l;
            result = new byte[ba.length + l];
            System.arraycopy(ba, 0, result, 0, ba.length);
        }
        return result;
    
public static char[]pad4(char[] ca)

Pads a character array with 0x0000 characters so that its length is a multiple of 4.

param
ca The character array to pad.
return
The padded character array.

        final int PAD = 4;
        final char[] result;
        int l = ca.length % PAD;
        if (l == 0)
            result = ca;
        else
        {
            l = PAD - l;
            result = new char[ca.length + l];
            System.arraycopy(ca, 0, result, 0, ca.length);
        }
        return result;
    
public static char[]pad4(java.lang.String s)

Pads a string with 0x0000 characters so that its length is a multiple of 4.

param
s The string to pad.
return
The padded string as a character array.

        return pad4(s.toCharArray());
    
public static java.lang.StringtoString(java.lang.Throwable t)

Returns a textual representation of a {@link Throwable}, including a stacktrace.

param
t The {@link Throwable}
return
a string containing the output of a call to t.printStacktrace().

        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        pw.close();
        try
        {
            sw.close();
            return sw.toString();
        }
        catch (IOException e)
        {
            final StringBuffer b = new StringBuffer(t.getMessage());
            b.append("\n");
            b.append("Could not create a stacktrace. Reason: ");
            b.append(e.getMessage());
            return b.toString();
        }