1 /* 2 * ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2000 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" must 28 * not be used to endorse or promote products derived from this 29 * software without prior written permission. For written 30 * permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * nor may "Apache" appear in their name, without prior written 34 * permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 package org.apache.poi.hpsf; 56 57 import java.util.*; 58 59 /** 60 * <p> 61 * 62 * Provides various static utility methods.</p> 63 * 64 *@author Rainer Klute (klute@rainer-klute.de) 65 *@created May 10, 2002 66 *@version $Id: Util.java,v 1.4 2002/05/11 14:47:23 acoliver Exp $ 67 *@since 2002-02-09 68 */ 69 public class Util { 70 71 /** 72 * <p> 73 * 74 * Checks whether two byte arrays <var>a</var> and <var>b</var> are equal. 75 * They are equal</p> 76 * <ul> 77 * <li> <p> 78 * 79 * if they have the same length and</p> </li> 80 * <li> <p> 81 * 82 * if for each <var>i</var> with <var>i</var> >= 0 and 83 * <var>i</var> < <var>a.length</var> holds <var>a</var> [ 84 * <var>i</var> ] == <var>b</var> [<var>i</var> ].</p> </li> 85 * 86 * </ul> 87 * 88 * 89 *@param a Description of the Parameter 90 *@param b Description of the Parameter 91 *@return Description of the Return Value 92 */ 93 public static boolean equal(final byte[] a, final byte[] b) { 94 if (a.length != b.length) { 95 return false; 96 } 97 for (int i = 0; i < a.length; i++) { 98 if (a[i] != b[i]) { 99 return false; 100 } 101 } 102 return true; 103 } 104 105 106 107 /** 108 * <p> 109 * 110 * Copies a part of a byte array into another byte array.</p> 111 * 112 *@param src Description of the Parameter 113 *@param srcOffset Description of the Parameter 114 *@param length Description of the Parameter 115 *@param dst Description of the Parameter 116 *@param dstOffset Description of the Parameter 117 */ 118 public static void copy(final byte[] src, final int srcOffset, 119 final int length, 120 final byte[] dst, final int dstOffset) { 121 for (int i = 0; i < length; i++) { 122 dst[dstOffset + i] = src[srcOffset + i]; 123 } 124 } 125 126 127 128 /** 129 * <p> 130 * 131 * Concatenates the contents of several byte arrays into a single one.</p> 132 * 133 *@param byteArrays The byte arrays to be concatened. 134 *@return A new byte array containing the concatenated byte 135 * arrays. 136 */ 137 public static byte[] cat(final byte[][] byteArrays) { 138 int capacity = 0; 139 for (int i = 0; i < byteArrays.length; i++) { 140 capacity += byteArrays[i].length; 141 } 142 final byte[] result = new byte[capacity]; 143 int r = 0; 144 for (int i = 0; i < byteArrays.length; i++) { 145 for (int j = 0; j < byteArrays[i].length; j++) { 146 result[r++] = byteArrays[i][j]; 147 } 148 } 149 return result; 150 } 151 152 153 154 /** 155 * <p> 156 * 157 * Copies bytes from a source byte array into a new byte array.</p> 158 * 159 *@param src Copy from this byte array. 160 *@param offset Start copying here. 161 *@param length Copy this many bytes. 162 *@return The new byte array. Its length is number of copied bytes. 163 */ 164 public static byte[] copy(final byte[] src, final int offset, 165 final int length) { 166 final byte[] result = new byte[length]; 167 copy(src, offset, length, result, 0); 168 return result; 169 } 170 171 172 173 /** 174 * <p> 175 * 176 * The difference between the Windows epoch (1601-01-01 00:00:00) and the 177 * Unix epoch (1970-01-01 00:00:00) in milliseconds: 11644473600000L. (Use 178 * your favorite spreadsheet program to verify the correctness of this 179 * value. By the way, did you notice that you can tell from the epochs 180 * which operating system is the modern one? :-))</p> 181 */ 182 public final static long EPOCH_DIFF = 11644473600000L; 183 184 185 /** 186 * <p> 187 * 188 * Converts a Windows FILETIME into a {@link Date}. The Windows FILETIME 189 * structure holds a date and time associated with a file. The structure 190 * identifies a 64-bit integer specifying the number of 100-nanosecond 191 * intervals which have passed since January 1, 1601. This 64-bit value is 192 * split into the two double word stored in the structure.</p> 193 * 194 *@param high The higher double word of the FILETIME structure. 195 *@param low The lower double word of the FILETIME structure. 196 *@return Description of the Return Value 197 */ 198 public static Date filetimeToDate(final int high, final int low) { 199 final long filetime = ((long) high) << 32 | ((long) low); 200 final long ms_since_16010101 = filetime / (1000 * 10); 201 final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF; 202 return new Date(ms_since_19700101); 203 } 204 205 } 206 207