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