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

com.abubusoft.kripton.common.DynamicByteBufferHelper Maven / Gradle / Ivy

/*
 * Copyright 2013-2014 Richard M. Hightower
 * 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.
 *
 * __________                              _____          __   .__
 * \______   \ ____   ____   ____   /\    /     \ _____  |  | _|__| ____    ____
 *  |    |  _//  _ \ /  _ \ /    \  \/   /  \ /  \\__  \ |  |/ /  |/    \  / ___\
 *  |    |   (  <_> |  <_> )   |  \ /\  /    Y    \/ __ \|    <|  |   |  \/ /_/  >
 *  |______  /\____/ \____/|___|  / \/  \____|__  (____  /__|_ \__|___|  /\___  /
 *         \/                   \/              \/     \/     \/       \//_____/
 *      ____.                     ___________   _____    ______________.___.
 *     |    |____ ___  _______    \_   _____/  /  _  \  /   _____/\__  |   |
 *     |    \__  \\  \/ /\__  \    |    __)_  /  /_\  \ \_____  \  /   |   |
 * /\__|    |/ __ \\   /  / __ \_  |        \/    |    \/        \ \____   |
 * \________(____  /\_/  (____  / /_______  /\____|__  /_______  / / ______|
 *               \/           \/          \/         \/        \/  \/
 */

package com.abubusoft.kripton.common;

import java.nio.charset.StandardCharsets;

import com.abubusoft.kripton.exception.KriptonRuntimeException;

// TODO: Auto-generated Javadoc
/**
 * The Class DynamicByteBufferHelper.
 */
public class DynamicByteBufferHelper {

	/**
	 * Grow.
	 *
	 * @param array the array
	 * @param size the size
	 * @return the byte[]
	 */
	public static byte[] grow(byte[] array, final int size) {
		byte[] newArray = new byte[array.length + size];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	/**
	 * Grow.
	 *
	 * @param array the array
	 * @return the byte[]
	 */
	public static byte[] grow(byte[] array) {

		byte[] newArray = new byte[array.length * 2];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	/**
	 * Shrink.
	 *
	 * @param array the array
	 * @param size the size
	 * @return the byte[]
	 */
	public static byte[] shrink(byte[] array, int size) {

		byte[] newArray = new byte[array.length - size];

		System.arraycopy(array, 0, newArray, 0, array.length - size);
		return newArray;
	}

	/**
	 * Compact.
	 *
	 * @param array the array
	 * @return the byte[]
	 */
	public static byte[] compact(byte[] array) {

		int nullCount = 0;
		for (byte ch : array) {

			if (ch == '\0') {
				nullCount++;
			}
		}
		byte[] newArray = new byte[array.length - nullCount];

		int j = 0;
		for (byte ch : array) {

			if (ch == '\0') {
				continue;
			}

			newArray[j] = ch;
			j++;
		}
		return newArray;
	}

	/**
	 * Creates an array of bytes.
	 *
	 * @param size            size of the array you want to make
	 * @return array of bytes
	 */
	public static byte[] arrayOfByte(final int size) {
		return new byte[size];
	}

	/**
	 * Array.
	 *
	 * @param array            array
	 * @return array
	 */

	public static byte[] array(final byte... array) {
		return array;
	}

	/**
	 * Bytes.
	 *
	 * @param array            array
	 * @return array
	 */

	public static byte[] bytes(final byte... array) {
		return array;
	}

	/**
	 * Bytes.
	 *
	 * @param str            string
	 * @return array
	 */

	public static byte[] bytes(String str) {
		return str.getBytes(StandardCharsets.UTF_8);
	}

	/**
	 * Len.
	 *
	 * @param array the array
	 * @return the int
	 */
	public static int len(byte[] array) {
		return array.length;
	}

	/**
	 * Length of.
	 *
	 * @param array the array
	 * @return the int
	 */
	public static int lengthOf(byte[] array) {
		return array.length;
	}

	/**
	 * At index.
	 *
	 * @param array the array
	 * @param index the index
	 * @return the byte
	 */
	public static byte atIndex(final byte[] array, final int index) {
		return idx(array, index);
	}

	/**
	 * Idx.
	 *
	 * @param array the array
	 * @param index the index
	 * @return the byte
	 */
	public static byte idx(final byte[] array, final int index) {
		final int i = calculateIndex(array, index);

		return array[i];
	}

	/**
	 * At index.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 */
	public static void atIndex(final byte[] array, int index, byte value) {
		idx(array, index, value);
	}

	/**
	 * Idx.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 */
	public static void idx(final byte[] array, int index, byte value) {
		final int i = calculateIndex(array, index);

		array[i] = value;
	}

	/**
	 * Slice of.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @param endIndex the end index
	 * @return the byte[]
	 */
	public static byte[] sliceOf(byte[] array, int startIndex, int endIndex) {
		return slc(array, startIndex, endIndex);
	}

	/**
	 * Slc.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @param endIndex the end index
	 * @return the byte[]
	 */
	public static byte[] slc(byte[] array, int startIndex, int endIndex) {

		final int start = calculateIndex(array, startIndex);
		final int end = calculateEndIndex(array, endIndex);
		final int newLength = end - start;

		if (newLength < 0) {
			throw new ArrayIndexOutOfBoundsException(String.format("start index %d, end index %d, length %d", startIndex, endIndex, array.length));
		}

		byte[] newArray = new byte[newLength];
		System.arraycopy(array, start, newArray, 0, newLength);
		return newArray;
	}

	/**
	 * Slice of.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @return the byte[]
	 */
	public static byte[] sliceOf(byte[] array, int startIndex) {
		return slc(array, startIndex);
	}

	/**
	 * Slc.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @return the byte[]
	 */
	public static byte[] slc(byte[] array, int startIndex) {

		final int start = calculateIndex(array, startIndex);
		final int newLength = array.length - start;

		if (newLength < 0) {
			throw new ArrayIndexOutOfBoundsException(String.format("start index %d, length %d", startIndex, array.length));
		}

		byte[] newArray = new byte[newLength];
		System.arraycopy(array, start, newArray, 0, newLength);
		return newArray;
	}

	/**
	 * End slice of.
	 *
	 * @param array the array
	 * @param endIndex the end index
	 * @return the byte[]
	 */
	public static byte[] endSliceOf(byte[] array, int endIndex) {
		return slcEnd(array, endIndex);
	}

	/**
	 * Slc end.
	 *
	 * @param array the array
	 * @param endIndex the end index
	 * @return the byte[]
	 */
	public static byte[] slcEnd(byte[] array, int endIndex) {

		final int end = calculateEndIndex(array, endIndex);
		final int newLength = end; // + (endIndex < 0 ? 1 : 0);

		if (newLength < 0) {
			throw new ArrayIndexOutOfBoundsException(String.format("start index %d, length %d", endIndex, array.length));
		}

		byte[] newArray = new byte[newLength];
		System.arraycopy(array, 0, newArray, 0, newLength);
		return newArray;
	}

	/**
	 * In.
	 *
	 * @param value the value
	 * @param array the array
	 * @return true, if successful
	 */
	public static boolean in(int value, byte... array) {
		for (int currentValue : array) {
			if (currentValue == value) {
				return true;
			}
		}
		return false;
	}

	/**
	 * In int array.
	 *
	 * @param value the value
	 * @param array the array
	 * @return true, if successful
	 */
	public static boolean inIntArray(byte value, int[] array) {
		for (int currentValue : array) {
			if (currentValue == value) {
				return true;
			}
		}
		return false;
	}

	/**
	 * In.
	 *
	 * @param value the value
	 * @param offset the offset
	 * @param array the array
	 * @return true, if successful
	 */
	public static boolean in(int value, int offset, byte[] array) {
		for (int index = offset; index < array.length; index++) {
			int currentValue = array[index];
			if (currentValue == value) {
				return true;
			}
		}
		return false;
	}

	/**
	 * In.
	 *
	 * @param value the value
	 * @param offset the offset
	 * @param end the end
	 * @param array the array
	 * @return true, if successful
	 */
	public static boolean in(int value, int offset, int end, byte[] array) {
		for (int index = offset; index < end; index++) {
			int currentValue = array[index];
			if (currentValue == value) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Copy.
	 *
	 * @param array the array
	 * @return the byte[]
	 */
	public static byte[] copy(byte[] array) {
		byte[] newArray = new byte[array.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	/**
	 * Copy.
	 *
	 * @param array the array
	 * @param offset the offset
	 * @param length the length
	 * @return the byte[]
	 */
	public static byte[] copy(byte[] array, int offset, int length) {
		byte[] newArray = new byte[length];
		System.arraycopy(array, offset, newArray, 0, length);
		return newArray;
	}

	/**
	 * Adds the.
	 *
	 * @param array the array
	 * @param v the v
	 * @return the byte[]
	 */
	public static byte[] add(byte[] array, byte v) {
		byte[] newArray = new byte[array.length + 1];
		System.arraycopy(array, 0, newArray, 0, array.length);
		newArray[array.length] = v;
		return newArray;
	}

	/**
	 * Adds the.
	 *
	 * @param array the array
	 * @param array2 the array 2
	 * @return the byte[]
	 */
	public static byte[] add(byte[] array, byte[] array2) {
		byte[] newArray = new byte[array.length + array2.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(array2, 0, newArray, array.length, array2.length);
		return newArray;
	}

	/**
	 * Insert.
	 *
	 * @param array the array
	 * @param idx the idx
	 * @param v the v
	 * @return the byte[]
	 */
	public static byte[] insert(final byte[] array, final int idx, final byte v) {

		if (idx >= array.length) {
			return add(array, v);
		}

		final int index = calculateIndex(array, idx);

		// Object newArray =
		// Array.newInstance(array.getClass().getComponentType(),
		// array.length+1);
		byte[] newArray = new byte[array.length + 1];

		if (index != 0) {
			/* Copy up to the length in the array before the index. */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, 0, newArray, 0, index);
		}

		boolean lastIndex = index == array.length - 1;
		int remainingIndex = array.length - index;

		if (lastIndex) {
			/*
			 * Copy the area after the insert. Make sure we don't write over the
			 * end.
			 */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, index, newArray, index + 1, remainingIndex);

		} else {
			/* Copy the area after the insert. */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, index, newArray, index + 1, remainingIndex);

		}

		newArray[index] = v;
		return newArray;
	}

	/**
	 * Insert.
	 *
	 * @param array the array
	 * @param fromIndex the from index
	 * @param values the values
	 * @return the byte[]
	 */
	public static byte[] insert(final byte[] array, final int fromIndex, final byte[] values) {

		if (fromIndex >= array.length) {
			return add(array, values);
		}

		final int index = calculateIndex(array, fromIndex);

		// Object newArray =
		// Array.newInstance(array.getClass().getComponentType(),
		// array.length+1);
		byte[] newArray = new byte[array.length + values.length];

		if (index != 0) {
			/* Copy up to the length in the array before the index. */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, 0, newArray, 0, index);
		}

		boolean lastIndex = index == array.length - 1;

		int toIndex = index + values.length;
		int remainingIndex = newArray.length - toIndex;

		if (lastIndex) {
			/*
			 * Copy the area after the insert. Make sure we don't write over the
			 * end.
			 */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, index, newArray, index + values.length, remainingIndex);

		} else {
			/* Copy the area after the insert. */
			/* src sbegin dst dbegin length of copy */
			System.arraycopy(array, index, newArray, index + values.length, remainingIndex);

		}

		for (int i = index, j = 0; i < toIndex; i++, j++) {
			newArray[i] = values[j];
		}
		return newArray;
	}

	/**
	 * Calculate index.
	 *
	 * @param array the array
	 * @param originalIndex the original index
	 * @return the int
	 */
	/* End universal methods. */
	private static int calculateIndex(byte[] array, int originalIndex) {
		final int length = array.length;

		int index = originalIndex;

		/*
		 * Adjust for reading from the right as in -1 reads the 4th element if
		 * the length is 5
		 */
		if (index < 0) {
			index = length + index;
		}

		/*
		 * Bounds check if it is still less than 0, then they have an negative
		 * index that is greater than length
		 */
		/*
		 * Bounds check if it is still less than 0, then they have an negative
		 * index that is greater than length
		 */
		if (index < 0) {
			index = 0;
		}
		if (index >= length) {
			index = length - 1;
		}
		return index;
	}

	/**
	 * Calculate end index.
	 *
	 * @param array the array
	 * @param originalIndex the original index
	 * @return the int
	 */
	/* End universal methods. */
	private static int calculateEndIndex(byte[] array, int originalIndex) {
		final int length = array.length;

		int index = originalIndex;

		/*
		 * Adjust for reading from the right as in -1 reads the 4th element if
		 * the length is 5
		 */
		if (index < 0) {
			index = length + index;
		}

		/*
		 * Bounds check if it is still less than 0, then they have an negative
		 * index that is greater than length
		 */
		/*
		 * Bounds check if it is still less than 0, then they have an negative
		 * index that is greater than length
		 */
		if (index < 0) {
			index = 0;
		}
		if (index > length) {
			index = length;
		}
		return index;
	}

	/**
	 * Idx int.
	 *
	 * @param bytes the bytes
	 * @param off the off
	 * @return the int
	 */
	public static int idxInt(byte[] bytes, int off) {
		return ((bytes[off + 3] & 0xFF)) + ((bytes[off + 2] & 0xFF) << 8) + ((bytes[off + 1] & 0xFF) << 16) + ((bytes[off]) << 24);
	}

	/**
	 * Adds the int.
	 *
	 * @param array the array
	 * @param v the v
	 * @return the byte[]
	 */
	public static byte[] addInt(byte[] array, int v) {

		byte[] arrayToHoldInt = new byte[4];
		intTo(arrayToHoldInt, 0, v);
		return add(array, arrayToHoldInt);

	}

	/**
	 * Insert int into.
	 *
	 * @param array the array
	 * @param index the index
	 * @param v the v
	 * @return the byte[]
	 */
	public static byte[] insertIntInto(byte[] array, int index, int v) {

		byte[] arrayToHoldInt = new byte[4];
		intTo(arrayToHoldInt, 0, v);
		return insert(array, index, arrayToHoldInt);

	}

	/**
	 * Int to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void intTo(byte[] b, int off, int val) {
		b[off + 3] = (byte) (val);
		b[off + 2] = (byte) (val >>> 8);
		b[off + 1] = (byte) (val >>> 16);
		b[off] = (byte) (val >>> 24);
	}

	/**
	 * Long to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void longTo(byte[] b, int off, long val) {
		b[off + 7] = (byte) (val);
		b[off + 6] = (byte) (val >>> 8);
		b[off + 5] = (byte) (val >>> 16);
		b[off + 4] = (byte) (val >>> 24);
		b[off + 3] = (byte) (val >>> 32);
		b[off + 2] = (byte) (val >>> 40);
		b[off + 1] = (byte) (val >>> 48);
		b[off] = (byte) (val >>> 56);
	}

	/**
	 * Adds the long.
	 *
	 * @param array the array
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] addLong(byte[] array, long value) {

		byte[] holder = new byte[8];
		longTo(holder, 0, value);
		return add(array, holder);

	}

	/**
	 * Idx unsigned int.
	 *
	 * @param bytes the bytes
	 * @param off the off
	 * @return the long
	 */
	public static long idxUnsignedInt(byte[] bytes, int off) {
		return ((bytes[off + 3] & 0xFFL)) + ((bytes[off + 2] & 0xFFL) << 8L) + ((bytes[off + 1] & 0xFFL) << 16L) + ((bytes[off] & 0xFFL) << 24L);
	}

	/**
	 * Idx long.
	 *
	 * @param b the b
	 * @param off the off
	 * @return the long
	 */
	public static long idxLong(byte[] b, int off) {
		return ((b[off + 7] & 0xFFL)) + ((b[off + 6] & 0xFFL) << 8) + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24) + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40)
				+ ((b[off + 1] & 0xFFL) << 48) + (((long) b[off]) << 56);
	}

	/**
	 * Idx short.
	 *
	 * @param b the b
	 * @param off the off
	 * @return the short
	 */
	public static short idxShort(byte[] b, int off) {
		return (short) ((b[off + 1] & 0xFF) + (b[off] << 8));
	}

	/**
	 * Adds the short.
	 *
	 * @param array the array
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] addShort(byte[] array, short value) {

		byte[] holder = new byte[2];
		shortTo(holder, 0, value);
		return add(array, holder);

	}

	/**
	 * Insert short into.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] insertShortInto(byte[] array, int index, short value) {

		byte[] holder = new byte[2];
		shortTo(holder, 0, value);
		return insert(array, index, holder);

	}

	/**
	 * Short to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void shortTo(byte[] b, int off, short val) {
		b[off + 1] = (byte) (val);
		b[off] = (byte) (val >>> 8);
	}

	/**
	 * Idx char.
	 *
	 * @param b the b
	 * @param off the off
	 * @return the char
	 */
	public static char idxChar(byte[] b, int off) {
		return (char) ((b[off + 1] & 0xFF) + (b[off] << 8));
	}

	/**
	 * Adds the char.
	 *
	 * @param array the array
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] addChar(byte[] array, char value) {
		byte[] holder = new byte[2];
		charTo(holder, 0, value);
		return add(array, holder);

	}

	/**
	 * Insert char into.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] insertCharInto(byte[] array, int index, char value) {
		byte[] holder = new byte[2];
		charTo(holder, 0, value);
		return insert(array, index, holder);

	}

	/**
	 * Char to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void charTo(byte[] b, int off, char val) {
		b[off + 1] = (byte) (val);
		b[off] = (byte) (val >>> 8);
	}

	/**
	 * Char to.
	 *
	 * @param b the b
	 * @param val the val
	 */
	public static void charTo(byte[] b, char val) {
		b[1] = (byte) (val);
		b[0] = (byte) (val >>> 8);
	}

	/**
	 * Idx float.
	 *
	 * @param array the array
	 * @param off the off
	 * @return the float
	 */
	public static float idxFloat(byte[] array, int off) {
		return Float.intBitsToFloat(idxInt(array, off));
	}

	/**
	 * Adds the float.
	 *
	 * @param array the array
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] addFloat(byte[] array, float value) {

		byte[] holder = new byte[4];
		floatTo(holder, 0, value);
		return add(array, holder);

	}

	/**
	 * Insert float into.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] insertFloatInto(byte[] array, int index, float value) {

		byte[] holder = new byte[4];
		floatTo(holder, 0, value);
		return insert(array, index, holder);

	}

	/**
	 * Float to.
	 *
	 * @param array the array
	 * @param off the off
	 * @param val the val
	 */
	public static void floatTo(byte[] array, int off, float val) {
		intTo(array, off, Float.floatToIntBits(val));
	}

	/**
	 * Adds the double.
	 *
	 * @param array the array
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] addDouble(byte[] array, double value) {
		byte[] holder = new byte[4];
		doubleTo(holder, 0, value);
		return add(array, holder);

	}

	/**
	 * Insert double into.
	 *
	 * @param array the array
	 * @param index the index
	 * @param value the value
	 * @return the byte[]
	 */
	public static byte[] insertDoubleInto(byte[] array, int index, double value) {
		byte[] holder = new byte[4];
		doubleTo(holder, 0, value);
		return insert(array, index, holder);

	}

	/**
	 * Double to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void doubleTo(byte[] b, int off, double val) {
		longTo(b, off, Double.doubleToLongBits(val));
	}

	/**
	 * Idx double.
	 *
	 * @param b the b
	 * @param off the off
	 * @return the double
	 */
	public static double idxDouble(byte[] b, int off) {
		return Double.longBitsToDouble(idxLong(b, off));
	}

	//
	//
	// public static boolean booleanAt(byte[] b, int off) {
	// return b[off] != 0;
	// }
	//
	//
	// public static boolean booleanInBytePos1(int val) {
	// val = val & 0x01;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos2(int val) {
	// val = val & 0x02;
	// return val != 0;
	// }
	//
	//
	// public static boolean booleanInBytePos3(int val) {
	// val = val & 0x04;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos4(int val) {
	// val = val & 0x08;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos1(byte[] b, int off) {
	// int val = b[off] & 0x01;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos2(byte[] b, int off) {
	// int val = b[off] & 0x02;
	// return val != 0;
	// }
	//
	//
	// public static boolean booleanInBytePos3(byte[] b, int off) {
	// int val = b[off] & 0x04;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos4(byte[] b, int off) {
	// int val = b[off] & 0x08;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos5(byte[] b, int off) {
	// int val = b[off] & 0x10;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos6(byte[] b, int off) {
	// int val = b[off] & 0x20;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos7(byte[] b, int off) {
	// int val = b[off] & 0x40;
	// return val != 0;
	// }
	//
	// public static boolean booleanInBytePos8(byte[] b, int off) {
	// int val = b[off] & 0x80;
	// return val != 0;
	// }
	//
	//
	// public static int byteAt(byte[] b, int off) {
	// return b[off];
	// }
	//
	//
	// public static int topNibbleAt(byte[] b, int off) {
	// return topNibbleAt (b[off] );
	// }
	//
	// public static int bottomNibbleAt(byte[] b, int off) {
	// return bottomNibbleAt (b[off] );
	// }
	//
	// public static int topNibbleAt(int val) {
	// return (val & 0xF0);
	// }
	//
	// public static int bottomNibbleAt(int val) {
	// return (val & 0x0F);
	// }
	//
	//
	// public static char charAt1(byte[] b, int off) {
	// return (char) ((b[off + 1] & 0xFF) +
	// (b[off] << 8));
	// }
	//

	/**
	 * Idx.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @param input the input
	 */
	public static void _idx(final byte[] array, int startIndex, byte[] input) {
		try {

			System.arraycopy(input, 0, array, startIndex, input.length);
		} catch (Exception ex) {
			throw new KriptonRuntimeException(String.format("array size %d, startIndex %d, input length %d", array.length, startIndex, input.length), ex);
		}
	}

	/**
	 * Idx.
	 *
	 * @param array the array
	 * @param startIndex the start index
	 * @param input the input
	 * @param length the length
	 */
	public static void _idx(final byte[] array, int startIndex, byte[] input, int length) {
		try {

			System.arraycopy(input, 0, array, startIndex, length);
		} catch (Exception ex) {
			throw new KriptonRuntimeException(String.format("array size %d, startIndex %d, input length %d", array.length, startIndex, input.length), ex);
		}
	}

	/**
	 * Idx.
	 *
	 * @param output the output
	 * @param ouputStartIndex the ouput start index
	 * @param input the input
	 * @param inputOffset the input offset
	 * @param length the length
	 */
	public static void _idx(final byte[] output, int ouputStartIndex, byte[] input, int inputOffset, int length) {
		try {

			System.arraycopy(input, inputOffset, output, ouputStartIndex, length);
		} catch (Exception ex) {
			throw new KriptonRuntimeException(String.format("array size %d, startIndex %d, input length %d", output.length, ouputStartIndex, input.length), ex);
		}
	}

	/**
	 * Idx unsigned short.
	 *
	 * @param buffer the buffer
	 * @param off the off
	 * @return the int
	 */
	public static int idxUnsignedShort(byte[] buffer, int off) {

		int ch1 = buffer[off] & 0xFF;
		int ch2 = buffer[off + 1] & 0xFF;

		return (ch1 << 8) + (ch2);

	}

	/**
	 * Idx unsigned byte.
	 *
	 * @param array the array
	 * @param location the location
	 * @return the short
	 */
	public static short idxUnsignedByte(byte[] array, int location) {
		return (short) (array[location] & 0xFF);
	}

	/**
	 * Unsigned int to.
	 *
	 * @param b the b
	 * @param off the off
	 * @param val the val
	 */
	public static void unsignedIntTo(byte[] b, int off, long val) {
		b[off + 3] = (byte) (val);
		b[off + 2] = (byte) (val >>> 8);
		b[off + 1] = (byte) (val >>> 16);
		b[off] = (byte) (val >>> 24);
	}

	/**
	 * Unsigned short to.
	 *
	 * @param buffer the buffer
	 * @param off the off
	 * @param value the value
	 */
	public static void unsignedShortTo(byte[] buffer, int off, int value) {

		buffer[off + 1] = (byte) (value);
		buffer[off] = (byte) (value >>> 8);

	}

	/**
	 * Unsigned byte to.
	 *
	 * @param buffer the buffer
	 * @param off the off
	 * @param value the value
	 */
	public static void unsignedByteTo(byte[] buffer, int off, short value) {
		buffer[off] = (byte) (value);

	}

	/**
	 * Utf string.
	 *
	 * @param jsonBytes the json bytes
	 * @return the string
	 */
	public static String utfString(byte[] jsonBytes) {
		return new String(jsonBytes, StandardCharsets.UTF_8);
	}

	/**
	 * Checks to see if two arrays are equals.
	 *
	 * @param expected            expected array
	 * @param got            got array
	 * @return true if equal or throws exception if not.
	 */
	public static boolean equalsOrDie(byte[] expected, byte[] got) {

		if (expected.length != got.length) {
			throw new KriptonRuntimeException("Lengths did not match, expected length" + expected.length + "but got" + got.length);
		}

		for (int index = 0; index < expected.length; index++) {
			if (expected[index] != got[index]) {
				throw new KriptonRuntimeException("value at index did not match index" + index + "expected value" + expected[index] + "but got"+ got[index]);

			}
		}
		return true;
	}

	/**
	 * Checks to see if two arrays are equals.
	 *
	 * @param expected            expected array
	 * @param got            got array
	 * @return true if equal or false if not.
	 */
	public static boolean equals(byte[] expected, byte[] got) {

		if (expected.length != got.length) {
			return false;
		}

		for (int index = 0; index < expected.length; index++) {
			if (expected[index] != got[index]) {
				return false;
			}
		}
		return true;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy