All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hpsf.Util Maven / Gradle / Ivy

/* ====================================================================
   Copyright 2002-2004   Apache Software Foundation

   Licensed 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.
==================================================================== */

package org.apache.poi.hpsf;

import java.util.*;

/**
 * 

Provides various static utility methods.

* * @author Rainer Klute ([email protected]) * @version $Id: Util.java,v 1.7.4.1 2004/02/22 11:54:45 glens Exp $ * @since 2002-02-09 */ public class Util { /** *

Checks whether two byte arrays a and b * are equal. They are equal

* *
    * *
  • if they have the same length and

  • * *
  • if for each i with * i >= 0 and * i < a.length holds * a[i] == b[i].

  • * *
* * @param a The first byte array * @param b The first byte array * @return true if the byte arrays are equal, else * false */ public static boolean equal(final byte[] a, final byte[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) if (a[i] != b[i]) return false; return true; } /** *

Copies a part of a byte array into another byte array.

* * @param src The source byte array. * @param srcOffset Offset in the source byte array. * @param length The number of bytes to copy. * @param dst The destination byte array. * @param dstOffset Offset in the destination byte array. */ public static void copy(final byte[] src, final int srcOffset, final int length, final byte[] dst, final 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.

* * @param byteArrays The byte arrays to be concatened. * @return A new byte array containing the concatenated byte * arrays. */ public static byte[] cat(final byte[][] byteArrays) { int capacity = 0; for (int i = 0; i < byteArrays.length; i++) capacity += byteArrays[i].length; final 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.

* * @param src Copy from this byte array. * @param offset Start copying here. * @param length Copy this many bytes. * @return The new byte array. Its length is number of copied bytes. */ public static byte[] copy(final byte[] src, final int offset, final int length) { final 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 final static long EPOCH_DIFF = 11644473600000L; /** *

Converts a Windows FILETIME into a {@link Date}. The Windows * FILETIME structure holds a date 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.

* * @param high The higher double word of the FILETIME structure. * @param low The lower double word of the FILETIME structure. * @return The Windows FILETIME as a {@link Date}. */ public static Date filetimeToDate(final int high, final int low) { final long filetime = ((long) high) << 32 | (low & 0xffffffffL); final long ms_since_16010101 = filetime / (1000 * 10); final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF; return new Date(ms_since_19700101); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy