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   
55   package org.apache.poi.hpsf;
56   
57   import java.util.*;
58   
59   /**
60    * <p>Provides various static utility methods.</p>
61    *
62    * @author Rainer Klute (klute@rainer-klute.de)
63    * @version $Id: Util.java,v 1.3 2002/05/01 09:31:52 klute Exp $
64    * @since 2002-02-09
65    */
66   public class Util
67   {
68   
69       /**
70        * <p>Checks whether two byte arrays <var>a</var> and <var>b</var>
71        * are equal. They are equal</p>
72        *
73        * <ul>
74        * <li><p>if they have the same length and</p></li>
75        *
76        * <li><p>if for each <var>i</var> with
77        * <var>i</var> >= 0 and
78        * <var>i</var> < <var>a.length</var> holds
79        * <var>a</var>[<var>i</var>] == <var>b</var>[<var>i</var>].</p></li>
80        * </ul>
81        */
82       public static boolean equal(final byte[] a, final byte[] b)
83       {
84           if (a.length != b.length)
85               return false;
86           for (int i = 0; i < a.length; i++)
87               if (a[i] != b[i])
88                   return false;
89           return true;
90       }
91   
92   
93   
94       /**
95        * <p>Copies a part of a byte array into another byte array.</p>
96        */
97       public static void copy(final byte[] src, final int srcOffset,
98                               final int length,
99                               final byte[] dst, final int dstOffset)
100      {
101          for (int i = 0; i < length; i++)
102              dst[dstOffset + i] = src[srcOffset + i];
103      }
104  
105  
106  
107      /**
108       * <p>Concatenates the contents of several byte arrays into a
109       * single one.</p>
110       *
111       * @param byteArrays The byte arrays to be concatened.
112       *
113       * @return A new byte array containing the concatenated byte
114       * arrays.
115       */
116      public static byte[] cat(final byte[][] byteArrays)
117      {
118          int capacity = 0;
119          for (int i = 0; i < byteArrays.length; i++)
120              capacity += byteArrays[i].length;
121          final byte[] result = new byte[capacity];
122          int r = 0;
123          for (int i = 0; i < byteArrays.length; i++)
124              for (int j = 0; j < byteArrays[i].length; j++)
125                  result[r++] = byteArrays[i][j];
126          return result;
127      }
128  
129  
130  
131      /**
132       * <p>Copies bytes from a source byte array into a new byte
133       * array.</p>
134       *
135       * @param src Copy from this byte array.
136       *
137       * @param offset Start copying here.
138       *
139       * @param length Copy this many bytes.
140       *
141       * @return The new byte array. Its length is number of copied bytes.
142       */
143      public static byte[] copy(final byte[] src, final int offset,
144                                final int length)
145      {
146          final byte[] result = new byte[length];
147          copy(src, offset, length, result, 0);
148          return result;
149      }
150  
151  
152  
153      /**
154       * <p>The difference between the Windows epoch (1601-01-01
155       * 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in
156       * milliseconds: 11644473600000L. (Use your favorite spreadsheet
157       * program to verify the correctness of this value. By the way,
158       * did you notice that you can tell from the epochs which
159       * operating system is the modern one? :-))</p>
160       */
161      public final static long EPOCH_DIFF = 11644473600000L;
162  
163      /**
164       * <p>Converts a Windows FILETIME into a {@link Date}. The Windows
165       * FILETIME structure holds a date and time associated with a
166       * file. The structure identifies a 64-bit integer specifying the
167       * number of 100-nanosecond intervals which have passed since
168       * January 1, 1601. This 64-bit value is split into the two double
169       * word stored in the structure.</p>
170       *
171       * @param high The higher double word of the FILETIME structure.
172       *
173       * @param low The lower double word of the FILETIME structure.
174       */
175      public static Date filetimeToDate(final int high, final int low)
176      {
177          final long filetime = ((long) high) << 32 | ((long) low);
178          final long ms_since_16010101 = filetime / (1000 * 10);
179          final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
180          return new Date(ms_since_19700101);
181      }
182  
183  }
184  
185