zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* ====================================================================
   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 HH.WMS.Utils.NPOI.HPSF
{
    using System;
    using System.IO;
    using System.Collections;
 
 
    /// <summary>
    /// Provides various static utility methods.
    /// @author Rainer Klute (klute@rainer-klute.de)
    /// @since 2002-02-09
    /// </summary>
    public class Util
    {
 
 
        /// <summary>
        /// Copies a part of a byte array into another byte array.
        /// </summary>
        /// <param name="src">The source byte array.</param>
        /// <param name="srcOffSet">OffSet in the source byte array.</param>
        /// <param name="Length">The number of bytes To Copy.</param>
        /// <param name="dst">The destination byte array.</param>
        /// <param name="dstOffSet">OffSet in the destination byte array.</param>
        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];
        }
 
 
 
        /// <summary>
        /// Concatenates the contents of several byte arrays into a
        /// single one.
        /// </summary>
        /// <param name="byteArrays">The byte arrays To be conCatened.</param>
        /// <returns>A new byte array containing the conCatenated byte arrays.</returns>
        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;
        }
 
 
 
        /// <summary>
        /// Copies bytes from a source byte array into a new byte
        /// array.
        /// </summary>
        /// <param name="src">Copy from this byte array.</param>
        /// <param name="offset">Start Copying here.</param>
        /// <param name="Length">Copy this many bytes.</param>
        /// <returns>The new byte array. Its Length is number of copied bytes.</returns>
        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 long EPOCH_DIFF = new DateTime(1970, 1, 1).Ticks;   //11644473600000L;
 
 
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="high">The higher double word of the FILETIME structure.</param>
        /// <param name="low">The lower double word of the FILETIME structure.</param>
        /// <returns>The Windows FILETIME as a {@link DateTime}.</returns>
        public static DateTime FiletimeToDate(int high, int low)
        {
            long filetime = ((long)high << 32) | ((long)low & 0xffffffffL);
            return FiletimeToDate(filetime);
        }
 
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="filetime">The filetime To Convert.</param>
        /// <returns>The Windows FILETIME as a {@link DateTime}.</returns>
        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);
        }
 
        /// <summary>
        /// Converts a {@link DateTime} into a filetime.
        /// </summary>
        /// <param name="dateTime">The DateTime To be Converted</param>
        /// <returns>The filetime</returns>
        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();
        }
 
        /// <summary>
        /// Compares To object arrays with regarding the objects' order. For
        /// example, [1, 2, 3] and [2, 1, 3] are equal.
        /// </summary>
        /// <param name="c1">The first object array.</param>
        /// <param name="c2">The second object array.</param>
        /// <returns><c>true</c>
        ///  if the object arrays are equal,
        /// <c>false</c>
        ///  if they are not.</returns>
        public static bool AreEqual(IList c1, IList c2)
        {
            return internalEquals(c1, c2);
        }
 
        /// <summary>
        /// Internals the equals.
        /// </summary>
        /// <param name="c1">The c1.</param>
        /// <param name="c2">The c2.</param>
        /// <returns></returns>
        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;
        }
 
 
 
        /// <summary>
        /// Pads a byte array with 0x00 bytes so that its Length is a multiple of
        /// 4.
        /// </summary>
        /// <param name="ba">The byte array To pad.</param>
        /// <returns>The padded byte array.</returns>
        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 ];
                Array.Copy(ba,result, ba.Length);
            }
            return result;
        }
 
 
 
        /// <summary>
        /// Pads a character array with 0x0000 characters so that its Length is a
        /// multiple of 4.
        /// </summary>
        /// <param name="ca">The character array To pad.</param>
        /// <returns>The padded character array.</returns>
        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];
                Array.Copy(ca, result, ca.Length);
            }
            return result;
        }
 
 
 
        /// <summary>
        /// Pads a string with 0x0000 characters so that its Length is a
        /// multiple of 4.
        /// </summary>
        /// <param name="s">The string To pad.</param>
        /// <returns> The padded string as a character array.</returns>
        public static char[] Pad4(String s)
        {
            return Pad4(s.ToCharArray());
        }
    }
}