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

org.openrdf.util.ByteArrayUtil Maven / Gradle / Ivy

The newest version!
/*  Sesame - Storage and Querying architecture for RDF and RDF Schema
 *  Copyright (C) 2001-2006 Aduna
 *
 *  Contact: 
 *  	Aduna
 *  	Prinses Julianaplein 14 b
 *  	3817 CS Amersfoort
 *  	The Netherlands
 *  	tel. +33 (0)33 465 99 87
 *  	fax. +33 (0)33 465 99 87
 *
 *  	http://aduna-software.com/
 *  	http://www.openrdf.org/
 *  
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.openrdf.util;

/**
 * Class providing utility methods for handling byte arrays.
 **/
public class ByteArrayUtil {

	/**
	 * Puts the entire source array in the target
	 * array at offset offset.
	 **/
	public static void put(byte[] source, byte[] target, int offset) {
		System.arraycopy(source, 0, target, offset, source.length);
	}

	/**
	 * Gets the subarray from array that starts at offset.
	 **/
	public static byte[] get(byte[] array, int offset) {
		return get(array, offset, array.length - offset);
	}

	/**
	 * Gets the subarray of length length from array
	 * that starts at offset.
	 **/
	public static byte[] get(byte[] array, int offset, int length) {
		byte[] result = new byte[length];
		System.arraycopy(array, offset, result, 0, length);
		return result;
	}

	public static void putInt(int value, byte[] array, int offset) {
		array[offset]   = (byte)(0xff & (value >> 24));
		array[offset+1] = (byte)(0xff & (value >> 16));
		array[offset+2] = (byte)(0xff & (value >> 8));
		array[offset+3] = (byte)(0xff & value);
	}

	public static int getInt(byte[] array, int offset) {
		return
			((array[offset]   & 0xff) << 24) |
			((array[offset+1] & 0xff) << 16) |
			((array[offset+2] & 0xff) << 8) |
			 (array[offset+3] & 0xff);
	}

	public static void putLong(long value, byte[] array, int offset) {
		array[offset]   = (byte)(0xff & (value >> 56));
		array[offset+1] = (byte)(0xff & (value >> 48));
		array[offset+2] = (byte)(0xff & (value >> 40));
		array[offset+3] = (byte)(0xff & (value >> 32));
		array[offset+4] = (byte)(0xff & (value >> 24));
		array[offset+5] = (byte)(0xff & (value >> 16));
		array[offset+6] = (byte)(0xff & (value >> 8));
		array[offset+7] = (byte)(0xff & value);
	}

	public static long getLong(byte[] array, int offset) {
		return
			((long)(array[offset]   & 0xff) << 56) |
			((long)(array[offset+1] & 0xff) << 48) |
			((long)(array[offset+2] & 0xff) << 40) |
			((long)(array[offset+3] & 0xff) << 32) |
			((long)(array[offset+4] & 0xff) << 24) |
			((long)(array[offset+5] & 0xff) << 16) |
			((long)(array[offset+6] & 0xff) << 8) |
			((long)(array[offset+7] & 0xff));
	}

	/**
	 * Checks whether value matches pattern after
	 * mask has bee applied to value. In other words: this
	 * method returns true if value[i]&mask[i] == pattern[i] for
	 * all i.
	 **/
	public static boolean matchesPattern(byte[] value, byte[] mask, byte[] pattern) {
		for (int i = 0; i < value.length; i++) {
			if ( (value[i] & mask[i]) != pattern[i]) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Checks whether subValue matches the region in
	 * superValue starting at offset offset.
	 **/
	public static boolean regionMatches(byte[] subValue, byte[] superValue, int offset) {
		for (int i = 0; i < subValue.length; i++) {
			if (subValue[i] != superValue[i+offset]) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Compares two regions of bytes, indicating whether one is larger than the
	 * other.
	 *
	 * @param array1 The first byte array.
	 * @param startIdx1 The start of the region in the first array.
	 * @param array2 The second byte array.
	 * @param startIdx2 The start of the region in the second array.
	 * @param length The length of the region that should be compared.
	 * @return A negative number when the first region is smaller than the
	 * second, a positive number when the first region is larger than the
	 * second, or 0 if the regions are equal.
	 */
	public static int compareRegion(byte[] array1, int startIdx1, byte[] array2, int startIdx2, int length) {
		int result = 0;
		for (int i = 0; result == 0 && i < length; i++) {
			result = (array1[startIdx1 + i] & 0xff) - (array2[startIdx2 + i] & 0xff);
		}
		return result;
	}

	/**
	 * Returns the hexadecimal value of the supplied byte array. The resulting
	 * string always uses two hexadecimals per byte. As a result, the length
	 * of the resulting string is guaranteed to be twice the length of the
	 * supplied byte array.
	 **/
	public static String toHexString(byte[] array) {
		StringBuffer result = new StringBuffer(2*array.length);

		for (int i = 0; i < array.length; i++) {
			String hex = Integer.toHexString(array[i] & 0xff);

			if (hex.length() == 1) {
				result.append('0');
			}

			result.append(hex);
		}

		return result.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy