a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.5 KiB

  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. using System.Collections;
  16. namespace NPOI.HPSF
  17. {
  18. using System;
  19. using System.IO;
  20. using System.Text;
  21. /// <summary>
  22. /// Provides various static utility methods.
  23. /// </summary>
  24. public class Util
  25. {
  26. /// <summary>
  27. /// The difference between the Windows epoch (1601-01-01
  28. /// 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in
  29. /// milliseconds: 11644473600000L. (Use your favorite spreadsheet
  30. /// program to verify the correctness of this value. By the way,
  31. /// did you notice that you can tell from the epochs which
  32. /// operating system is the modern one? :-))
  33. /// </summary>
  34. public static long EPOCH_DIFF = 11644473600000L;
  35. /// <summary>
  36. /// Converts a Windows FILETIME into a {@link Date}. The Windows
  37. /// FILETIME structure holds a date and time associated with a
  38. /// file. The structure identifies a 64-bit integer specifying the
  39. /// number of 100-nanosecond intervals which have passed since
  40. /// January 1, 1601. This 64-bit value is split into the two double
  41. /// words stored in the structure.
  42. /// </summary>
  43. /// <param name="high">The higher double word of the FILETIME structure.</param>
  44. /// <param name="low">The lower double word of the FILETIME structure.</param>
  45. /// <return>Windows FILETIME as a {@link Date}.</return>
  46. public static DateTime FiletimeToDate(int high, int low)
  47. {
  48. long filetime = ((long)high) << 32 | (low & 0xffffffffL);
  49. return FiletimeToDate(filetime);
  50. }
  51. /// <summary>
  52. /// Converts a Windows FILETIME into a {@link Date}. The Windows
  53. /// FILETIME structure holds a date and time associated with a
  54. /// file. The structure identifies a 64-bit integer specifying the
  55. /// number of 100-nanosecond intervals which have passed since
  56. /// January 1, 1601.
  57. /// </summary>
  58. /// <param name="filetime">The filetime to convert.</param>
  59. /// <return>Windows FILETIME as a {@link Date}.</return>
  60. public static DateTime FiletimeToDate(long filetime)
  61. {
  62. return DateTime.FromFileTime(filetime);
  63. //long ms_since_16010101 = filetime / (1000 * 10);
  64. //long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
  65. //return new DateTime(ms_since_19700101);
  66. }
  67. /// <summary>
  68. /// Converts a {@link Date} into a filetime.
  69. /// </summary>
  70. /// <param name="date">The date to be converted</param>
  71. /// <return>filetime</return>
  72. public static long DateToFileTime(DateTime dateTime)
  73. {
  74. //long ms_since_19700101 = DateTime.Ticks;
  75. //long ms_since_16010101 = ms_since_19700101 + EPOCH_DIFF;
  76. //return ms_since_16010101 * (1000 * 10);
  77. return dateTime.ToFileTime();
  78. }
  79. /// <summary>
  80. /// Compares to object arrays with regarding the objects' order. For
  81. /// example, [1, 2, 3] and [2, 1, 3] are equal.
  82. /// </summary>
  83. /// <param name="c1">The first object array.</param>
  84. /// <param name="c2">The second object array.</param>
  85. /// <return>if the object arrays are equal,
  86. /// <code>false</code> if they are not.
  87. /// </return>
  88. public static bool AreEqual(IList c1, IList c2)
  89. {
  90. for(int i1 = 0; i1 < c1.Count; i1++)
  91. {
  92. Object obj1 = c1[i1];
  93. bool matchFound = false;
  94. for(int i2 = 0; !matchFound && i2 < c1.Count; i2++)
  95. {
  96. Object obj2 = c2[i2];
  97. if(obj1.Equals(obj2))
  98. {
  99. matchFound = true;
  100. //c2[i2] = null;
  101. }
  102. }
  103. if(!matchFound)
  104. return false;
  105. }
  106. return true;
  107. }
  108. /// <summary>
  109. /// Pads a byte array with 0x00 bytes so that its length is a multiple of
  110. /// 4.
  111. /// </summary>
  112. /// <param name="ba">The byte array to pad.</param>
  113. /// <return>padded byte array.</return>
  114. public static byte[] Pad4(byte[] ba)
  115. {
  116. int PAD = 4;
  117. byte[] result;
  118. int l = ba.Length % PAD;
  119. if(l == 0)
  120. result = ba;
  121. else
  122. {
  123. l = PAD - l;
  124. result = new byte[ba.Length + l];
  125. System.Array.Copy(ba, 0, result, 0, ba.Length);
  126. }
  127. return result;
  128. }
  129. /// <summary>
  130. /// Returns a textual representation of a {@link Throwable}, including a
  131. /// stacktrace.
  132. /// </summary>
  133. /// <param name="t">The {@link Throwable}</param>
  134. ///
  135. /// <return>string containing the output of a call to
  136. /// <code>t.printStacktrace()</code>.
  137. /// </return>
  138. public static String ToString(Exception t)
  139. {
  140. StringWriter sw = new StringWriter();
  141. //PrintWriter pw = new PrintWriter(sw);
  142. //t.printStackTrace(pw);
  143. //pw.Close();
  144. try
  145. {
  146. sw.Close();
  147. return sw.ToString();
  148. }
  149. catch(IOException e)
  150. {
  151. StringBuilder b = new StringBuilder(t.Message);
  152. b.Append("\n");
  153. b.Append("Could not create a stacktrace. Reason: ");
  154. b.Append(e.Message);
  155. return b.ToString();
  156. }
  157. }
  158. }
  159. }