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