2002-05-11 10:47:24 -04:00
|
|
|
/*
|
|
|
|
* ====================================================================
|
|
|
|
* The Apache Software License, Version 1.1
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* Copyright (c) 2000 The Apache Software Foundation. All rights
|
|
|
|
* reserved.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by the
|
|
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* 4. The names "Apache" and "Apache Software Foundation" must
|
|
|
|
* not be used to endorse or promote products derived from this
|
|
|
|
* software without prior written permission. For written
|
|
|
|
* permission, please contact apache@apache.org.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* 5. Products derived from this software may not be called "Apache",
|
|
|
|
* nor may "Apache" appear in their name, without prior written
|
|
|
|
* permission of the Apache Software Foundation.
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-05-11 10:47:24 -04:00
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
|
|
* information on the Apache Software Foundation, please see
|
|
|
|
* <http://www.apache.org/>.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
|
|
|
package org.apache.poi.hpsf;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>Provides various static utility methods.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @author Rainer Klute (klute@rainer-klute.de)
|
|
|
|
* @version $Id$
|
|
|
|
* @since 2002-02-09
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-02-01 08:28:28 -05:00
|
|
|
public class Util
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>Checks whether two byte arrays <var>a</var> and <var>b</var>
|
|
|
|
* are equal. They are equal</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* <ul>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* <li><p>if they have the same length and</p></li>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* <li><p>if for each <var>i</var> with
|
|
|
|
* <var>i</var> >= 0 and
|
|
|
|
* <var>i</var> < <var>a.length</var> holds
|
|
|
|
* <var>a</var>[<var>i</var>] == <var>b</var>[<var>i</var>].</p></li>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* </ul>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @param a The first byte array
|
|
|
|
* @param b The first byte array
|
|
|
|
* @return <code>true</code> if the byte arrays are equal, else
|
|
|
|
* <code>false</code>
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-02-01 08:28:28 -05:00
|
|
|
public static boolean equal(final byte[] a, final byte[] b)
|
|
|
|
{
|
|
|
|
if (a.length != b.length)
|
2002-02-13 23:00:59 -05:00
|
|
|
return false;
|
2003-02-01 08:28:28 -05:00
|
|
|
for (int i = 0; i < a.length; i++)
|
|
|
|
if (a[i] != b[i])
|
2002-02-13 23:00:59 -05:00
|
|
|
return false;
|
2003-02-01 08:28:28 -05:00
|
|
|
return true;
|
2002-02-13 23:00:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>Copies a part of a byte array into another byte array.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @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.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
|
|
|
public static void copy(final byte[] src, final int srcOffset,
|
2003-02-01 08:28:28 -05:00
|
|
|
final int length, final byte[] dst,
|
|
|
|
final int dstOffset)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < length; i++)
|
2002-02-13 23:00:59 -05:00
|
|
|
dst[dstOffset + i] = src[srcOffset + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>Concatenates the contents of several byte arrays into a
|
|
|
|
* single one.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @param byteArrays The byte arrays to be concatened.
|
|
|
|
* @return A new byte array containing the concatenated byte
|
|
|
|
* arrays.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-02-01 08:28:28 -05:00
|
|
|
public static byte[] cat(final byte[][] byteArrays)
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
int capacity = 0;
|
2003-02-01 08:28:28 -05:00
|
|
|
for (int i = 0; i < byteArrays.length; i++)
|
2002-02-13 23:00:59 -05:00
|
|
|
capacity += byteArrays[i].length;
|
2003-02-01 08:28:28 -05:00
|
|
|
final byte[] result = new byte[capacity];
|
2002-02-13 23:00:59 -05:00
|
|
|
int r = 0;
|
2003-02-01 08:28:28 -05:00
|
|
|
for (int i = 0; i < byteArrays.length; i++)
|
|
|
|
for (int j = 0; j < byteArrays[i].length; j++)
|
2002-02-13 23:00:59 -05:00
|
|
|
result[r++] = byteArrays[i][j];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>Copies bytes from a source byte array into a new byte
|
|
|
|
* array.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @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.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
|
|
|
public static byte[] copy(final byte[] src, final int offset,
|
2003-02-01 08:28:28 -05:00
|
|
|
final int length)
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
final byte[] result = new byte[length];
|
|
|
|
copy(src, offset, length, result, 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>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? :-))</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
|
|
|
public final static long EPOCH_DIFF = 11644473600000L;
|
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2003-02-01 08:28:28 -05:00
|
|
|
* <p>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
|
2003-02-13 11:57:39 -05:00
|
|
|
* words stored in the structure.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-02-01 08:28:28 -05:00
|
|
|
* @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}.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-02-01 08:28:28 -05:00
|
|
|
public static Date filetimeToDate(final int high, final int low)
|
|
|
|
{
|
2003-02-13 11:57:39 -05:00
|
|
|
final long filetime = ((long) high) << 32 | (low & 0xffffffffL);
|
2002-02-13 23:00:59 -05:00
|
|
|
final long ms_since_16010101 = filetime / (1000 * 10);
|
|
|
|
final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
|
|
|
|
return new Date(ms_since_19700101);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|