/* ==================================================================== Licensed To the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for Additional information regarding Copyright ownership. The ASF licenses this file To You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a Copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed To in writing, software distributed under the License is distributed on an "AS Is" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ /* ================================================================ * About NPOI * Author: Tony Qu * Author's email: tonyqus (at) gmail.com * Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn) * HomePage: http://www.codeplex.com/npoi * Contributors: * * ==============================================================*/ namespace Npoi.Core.HPSF { using System; using System.Collections; /// /// Provides various static utility methods. /// @author Rainer Klute (klute@rainer-klute.de) /// @since 2002-02-09 /// public class Util { /// /// Copies a part of a byte array into another byte array. /// /// The source byte array. /// OffSet in the source byte array. /// The number of bytes To Copy. /// The destination byte array. /// OffSet in the destination byte array. public static void Copy(byte[] src, int srcOffSet, int Length, byte[] dst, int dstOffSet) { for (int i = 0; i < Length; i++) dst[dstOffSet + i] = src[srcOffSet + i]; } /// /// Concatenates the contents of several byte arrays into a /// single one. /// /// The byte arrays To be conCatened. /// A new byte array containing the conCatenated byte arrays. public static byte[] Cat(byte[][] byteArrays) { int capacity = 0; for (int i = 0; i < byteArrays.Length; i++) capacity += byteArrays[i].Length; byte[] result = new byte[capacity]; int r = 0; for (int i = 0; i < byteArrays.Length; i++) for (int j = 0; j < byteArrays[i].Length; j++) result[r++] = byteArrays[i][j]; return result; } /// /// Copies bytes from a source byte array into a new byte /// array. /// /// Copy from this byte array. /// Start Copying here. /// Copy this many bytes. /// The new byte array. Its Length is number of copied bytes. public static byte[] Copy(byte[] src, int offset, int Length) { byte[] result = new byte[Length]; Copy(src, offset, Length, result, 0); return result; } /** * 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? :-)) */ public static readonly long EPOCH_DIFF = new DateTime(1970, 1, 1).Ticks; //11644473600000L; /// /// Converts a Windows FILETIME into a {@link DateTime}. The Windows /// FILETIME structure holds a DateTime 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 /// words stored in the structure. /// /// The higher double word of the FILETIME structure. /// The lower double word of the FILETIME structure. /// The Windows FILETIME as a {@link DateTime}. public static DateTime FiletimeToDate(int high, int low) { long filetime = ((long)high << 32) | ((long)low & 0xffffffffL); return FiletimeToDate(filetime); } /// /// Converts a Windows FILETIME into a {@link DateTime}. The Windows /// FILETIME structure holds a DateTime 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. /// /// The filetime To Convert. /// The Windows FILETIME as a {@link DateTime}. public static DateTime FiletimeToDate(long filetime) { return DateTime.FromFileTime(filetime); //long ms_since_16010101 = filetime / (1000 * 10); //long ms_since_19700101 = ms_since_16010101 -EPOCH_DIFF; //return new DateTime(ms_since_19700101); } /// /// Converts a {@link DateTime} into a filetime. /// /// The DateTime To be Converted /// The filetime public static long DateToFileTime(DateTime dateTime) { //long ms_since_19700101 = DateTime.Ticks; //long ms_since_16010101 = ms_since_19700101 + EPOCH_DIFF; //return ms_since_16010101 * (1000 * 10); return dateTime.ToFileTime(); } /// /// Compares To object arrays with regarding the objects' order. For /// example, [1, 2, 3] and [2, 1, 3] are equal. /// /// The first object array. /// The second object array. /// true /// if the object arrays are equal, /// false /// if they are not. public static bool AreEqual(IList c1, IList c2) { return internalEquals(c1, c2); } /// /// Internals the equals. /// /// The c1. /// The c2. /// private static bool internalEquals(IList c1, IList c2) { IEnumerator o1 = c1.GetEnumerator(); while (o1.MoveNext()) { Object obj1 = o1.Current; bool matchFound = false; IEnumerator o2 = c2.GetEnumerator(); while (!matchFound && o2.MoveNext()) { Object obj2 = o2.Current; if (obj1.Equals(obj2)) { matchFound = true; //o2[i2] = null; } } if (!matchFound) return false; } return true; } /// /// Pads a byte array with 0x00 bytes so that its Length is a multiple of /// 4. /// /// The byte array To pad. /// The padded byte array. public static byte[] Pad4(byte[] ba) { int PAD = 4; byte[] result; int l = ba.Length % PAD; if (l == 0) result = ba; else { l = PAD - l; result = new byte[ba.Length + l]; System.Array.Copy(ba, result, ba.Length); } return result; } /// /// Pads a character array with 0x0000 characters so that its Length is a /// multiple of 4. /// /// The character array To pad. /// The padded character array. public static char[] Pad4(char[] ca) { int PAD = 4; char[] result; int l = ca.Length % PAD; if (l == 0) result = ca; else { l = PAD - l; result = new char[ca.Length + l]; System.Array.Copy(ca, result, ca.Length); } return result; } /// /// Pads a string with 0x0000 characters so that its Length is a /// multiple of 4. /// /// The string To pad. /// The padded string as a character array. public static char[] Pad4(string s) { return Pad4(s.ToCharArray()); } } }