KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > Util


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hpsf;
19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Date JavaDoc;
25
26 /**
27  * <p>Provides various static utility methods.</p>
28  *
29  * @author Rainer Klute (klute@rainer-klute.de)
30  * @version $Id: Util.java,v 1.14 2004/04/09 13:05:16 glens Exp $
31  * @since 2002-02-09
32  */

33 public class Util
34 {
35
36     /**
37      * <p>Checks whether two byte arrays <var>a</var> and <var>b</var>
38      * are equal. They are equal</p>
39      *
40      * <ul>
41      *
42      * <li><p>if they have the same length and</p></li>
43      *
44      * <li><p>if for each <var>i</var> with
45      * <var>i</var>&nbsp;&gt;=&nbsp;0 and
46      * <var>i</var>&nbsp;&lt;&nbsp;<var>a.length</var> holds
47      * <var>a</var>[<var>i</var>]&nbsp;== <var>b</var>[<var>i</var>].</p></li>
48      *
49      * </ul>
50      *
51      * @param a The first byte array
52      * @param b The first byte array
53      * @return <code>true</code> if the byte arrays are equal, else
54      * <code>false</code>
55      */

56     public static boolean equal(final byte[] a, final byte[] b)
57     {
58         if (a.length != b.length)
59             return false;
60         for (int i = 0; i < a.length; i++)
61             if (a[i] != b[i])
62                 return false;
63         return true;
64     }
65
66
67
68     /**
69      * <p>Copies a part of a byte array into another byte array.</p>
70      *
71      * @param src The source byte array.
72      * @param srcOffset Offset in the source byte array.
73      * @param length The number of bytes to copy.
74      * @param dst The destination byte array.
75      * @param dstOffset Offset in the destination byte array.
76      */

77     public static void copy(final byte[] src, final int srcOffset,
78                             final int length, final byte[] dst,
79                             final int dstOffset)
80     {
81         for (int i = 0; i < length; i++)
82             dst[dstOffset + i] = src[srcOffset + i];
83     }
84
85
86
87     /**
88      * <p>Concatenates the contents of several byte arrays into a
89      * single one.</p>
90      *
91      * @param byteArrays The byte arrays to be concatened.
92      * @return A new byte array containing the concatenated byte
93      * arrays.
94      */

95     public static byte[] cat(final byte[][] byteArrays)
96     {
97         int capacity = 0;
98         for (int i = 0; i < byteArrays.length; i++)
99             capacity += byteArrays[i].length;
100         final byte[] result = new byte[capacity];
101         int r = 0;
102         for (int i = 0; i < byteArrays.length; i++)
103             for (int j = 0; j < byteArrays[i].length; j++)
104                 result[r++] = byteArrays[i][j];
105         return result;
106     }
107
108
109
110     /**
111      * <p>Copies bytes from a source byte array into a new byte
112      * array.</p>
113      *
114      * @param src Copy from this byte array.
115      * @param offset Start copying here.
116      * @param length Copy this many bytes.
117      * @return The new byte array. Its length is number of copied bytes.
118      */

119     public static byte[] copy(final byte[] src, final int offset,
120                               final int length)
121     {
122         final byte[] result = new byte[length];
123         copy(src, offset, length, result, 0);
124         return result;
125     }
126
127
128
129     /**
130      * <p>The difference between the Windows epoch (1601-01-01
131      * 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in
132      * milliseconds: 11644473600000L. (Use your favorite spreadsheet
133      * program to verify the correctness of this value. By the way,
134      * did you notice that you can tell from the epochs which
135      * operating system is the modern one? :-))</p>
136      */

137     public static final long EPOCH_DIFF = 11644473600000L;
138
139
140     /**
141      * <p>Converts a Windows FILETIME into a {@link Date}. The Windows
142      * FILETIME structure holds a date and time associated with a
143      * file. The structure identifies a 64-bit integer specifying the
144      * number of 100-nanosecond intervals which have passed since
145      * January 1, 1601. This 64-bit value is split into the two double
146      * words stored in the structure.</p>
147      *
148      * @param high The higher double word of the FILETIME structure.
149      * @param low The lower double word of the FILETIME structure.
150      * @return The Windows FILETIME as a {@link Date}.
151      */

152     public static Date JavaDoc filetimeToDate(final int high, final int low)
153     {
154         final long filetime = ((long) high) << 32 | (low & 0xffffffffL);
155         final long ms_since_16010101 = filetime / (1000 * 10);
156         final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
157         return new Date JavaDoc(ms_since_19700101);
158     }
159
160
161
162     /**
163      * <p>Converts a {@link Date} into a filetime.</p>
164      *
165      * @param date The date to be converted
166      * @return The filetime
167      *
168      * @see #filetimeToDate
169      */

170     public static long dateToFileTime(final Date JavaDoc date)
171     {
172         long ms_since_19700101 = date.getTime();
173         long ms_since_16010101 = ms_since_19700101 + EPOCH_DIFF;
174         return ms_since_16010101 * (1000 * 10);
175     }
176
177
178     /**
179      * <p>Checks whether two collections are equal. Two collections
180      * C<sub>1</sub> and C<sub>2</sub> are equal, if the following conditions
181      * are true:</p>
182      *
183      * <ul>
184      *
185      * <li><p>For each c<sub>1<em>i</em></sub> (element of C<sub>1</sub>) there
186      * is a c<sub>2<em>j</em></sub> (element of C<sub>2</sub>), and
187      * c<sub>1<em>i</em></sub> equals c<sub>2<em>j</em></sub>.</p></li>
188      *
189      * <li><p>For each c<sub>2<em>i</em></sub> (element of C<sub>2</sub>) there
190      * is a c<sub>1<em>j</em></sub> (element of C<sub>1</sub>) and
191      * c<sub>2<em>i</em></sub> equals c<sub>1<em>j</em></sub>.</p></li>
192      *
193      * </ul>
194      *
195      * @param c1 the first collection
196      * @param c2 the second collection
197      * @return <code>true</code> if the collections are equal, else
198      * <code>false</code>.
199      */

200     public static boolean equals(final Collection JavaDoc c1, final Collection JavaDoc c2)
201     {
202         final Object JavaDoc[] o1 = c1.toArray();
203         final Object JavaDoc[] o2 = c2.toArray();
204         return internalEquals(o1, o2);
205     }
206
207
208
209     /**
210      * <p>Compares to object arrays with regarding the objects' order. For
211      * example, [1, 2, 3] and [2, 1, 3] are equal.</p>
212      *
213      * @param c1 The first object array.
214      * @param c2 The second object array.
215      * @return <code>true</code> if the object arrays are equal,
216      * <code>false</code> if they are not.
217      */

218     public static boolean equals(final Object JavaDoc[] c1, final Object JavaDoc[] c2)
219     {
220         final Object JavaDoc[] o1 = (Object JavaDoc[]) c1.clone();
221         final Object JavaDoc[] o2 = (Object JavaDoc[]) c2.clone();
222         return internalEquals(o1, o2);
223     }
224
225     private static boolean internalEquals(final Object JavaDoc[] o1, final Object JavaDoc[] o2)
226     {
227         for (int i1 = 0; i1 < o1.length; i1++)
228         {
229             final Object JavaDoc obj1 = o1[i1];
230             boolean matchFound = false;
231             for (int i2 = 0; !matchFound && i2 < o1.length; i2++)
232             {
233                 final Object JavaDoc obj2 = o2[i2];
234                 if (obj1.equals(obj2))
235                 {
236                     matchFound = true;
237                     o2[i2] = null;
238                 }
239             }
240             if (!matchFound)
241                 return false;
242         }
243         return true;
244     }
245
246
247
248     /**
249      * <p>Pads a byte array with 0x00 bytes so that its length is a multiple of
250      * 4.</p>
251      *
252      * @param ba The byte array to pad.
253      * @return The padded byte array.
254      */

255     public static byte[] pad4(final byte[] ba)
256     {
257         final int PAD = 4;
258         final byte[] result;
259         int l = ba.length % PAD;
260         if (l == 0)
261             result = ba;
262         else
263         {
264             l = PAD - l;
265             result = new byte[ba.length + l];
266             System.arraycopy(ba, 0, result, 0, ba.length);
267         }
268         return result;
269     }
270
271
272
273     /**
274      * <p>Pads a character array with 0x0000 characters so that its length is a
275      * multiple of 4.</p>
276      *
277      * @param ca The character array to pad.
278      * @return The padded character array.
279      */

280     public static char[] pad4(final char[] ca)
281     {
282         final int PAD = 4;
283         final char[] result;
284         int l = ca.length % PAD;
285         if (l == 0)
286             result = ca;
287         else
288         {
289             l = PAD - l;
290             result = new char[ca.length + l];
291             System.arraycopy(ca, 0, result, 0, ca.length);
292         }
293         return result;
294     }
295
296
297
298     /**
299      * <p>Pads a string with 0x0000 characters so that its length is a
300      * multiple of 4.</p>
301      *
302      * @param s The string to pad.
303      * @return The padded string as a character array.
304      */

305     public static char[] pad4(final String JavaDoc s)
306     {
307         return pad4(s.toCharArray());
308     }
309
310
311
312     /**
313      * <p>Returns a textual representation of a {@link Throwable}, including a
314      * stacktrace.</p>
315      *
316      * @param t The {@link Throwable}
317      *
318      * @return a string containing the output of a call to
319      * <code>t.printStacktrace()</code>.
320      */

321     public static String JavaDoc toString(final Throwable JavaDoc t)
322     {
323         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
324         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
325         t.printStackTrace(pw);
326         pw.close();
327         try
328         {
329             sw.close();
330             return sw.toString();
331         }
332         catch (IOException JavaDoc e)
333         {
334             final StringBuffer JavaDoc b = new StringBuffer JavaDoc(t.getMessage());
335             b.append("\n");
336             b.append("Could not create a stacktrace. Reason: ");
337             b.append(e.getMessage());
338             return b.toString();
339         }
340     }
341
342 }
343
Popular Tags