1 2 17 18 package org.apache.poi.hpsf; 19 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.util.Collection ; 24 import java.util.Date ; 25 26 33 public class Util 34 { 35 36 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 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 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 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 137 public static final long EPOCH_DIFF = 11644473600000L; 138 139 140 152 public static Date 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 (ms_since_19700101); 158 } 159 160 161 162 170 public static long dateToFileTime(final Date 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 200 public static boolean equals(final Collection c1, final Collection c2) 201 { 202 final Object [] o1 = c1.toArray(); 203 final Object [] o2 = c2.toArray(); 204 return internalEquals(o1, o2); 205 } 206 207 208 209 218 public static boolean equals(final Object [] c1, final Object [] c2) 219 { 220 final Object [] o1 = (Object []) c1.clone(); 221 final Object [] o2 = (Object []) c2.clone(); 222 return internalEquals(o1, o2); 223 } 224 225 private static boolean internalEquals(final Object [] o1, final Object [] o2) 226 { 227 for (int i1 = 0; i1 < o1.length; i1++) 228 { 229 final Object obj1 = o1[i1]; 230 boolean matchFound = false; 231 for (int i2 = 0; !matchFound && i2 < o1.length; i2++) 232 { 233 final Object 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 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 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 305 public static char[] pad4(final String s) 306 { 307 return pad4(s.toCharArray()); 308 } 309 310 311 312 321 public static String toString(final Throwable t) 322 { 323 final StringWriter sw = new StringWriter (); 324 final PrintWriter pw = new PrintWriter (sw); 325 t.printStackTrace(pw); 326 pw.close(); 327 try 328 { 329 sw.close(); 330 return sw.toString(); 331 } 332 catch (IOException e) 333 { 334 final StringBuffer b = new StringBuffer (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 |