Java ByteOrder increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder, boolean isSigned)

Here you can find the source of increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder, boolean isSigned)

Description

Increase the number of bytes used to store this number without affecting its value.

License

Apache License

Parameter

Parameter Description
originalBytes a parameter
desiredNumberOfBytes a parameter
byteOrder a parameter
isSigned a parameter

Declaration

public static byte[] increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder,
        boolean isSigned) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteOrder;

public class Main {
    public final static int BITS_PER_BYTE = 8;

    /**/*from  ww w  .  j a  va2 s  .c  o m*/
     * Increase the number of bytes used to store this number without affecting its value.
     * 
     * @param originalBytes
     * @param desiredNumberOfBytes
     * @param byteOrder
     * @param isSigned
     * @return
     */
    public static byte[] increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder,
            boolean isSigned) {
        if (originalBytes.length >= desiredNumberOfBytes) {
            throw new IllegalArgumentException(
                    "Cannot increase the number of bytes when the size of the original byte array["
                            + originalBytes.length + "] is greater than or equal to the desired number of bytes["
                            + desiredNumberOfBytes + "].");
        }

        int numberOfBytesToInsert = desiredNumberOfBytes - originalBytes.length;

        byte[] newBytes = new byte[desiredNumberOfBytes];

        byte byteToAdd = (byte) 0;// 00000000
        if (isNegative(originalBytes, byteOrder, isSigned)) {
            byteToAdd = (byte) -1;// 11111111
        }

        if (byteOrder == ByteOrder.BIG_ENDIAN) {
            // add the new bytes at the beginning
            for (int i = 0; i < numberOfBytesToInsert; i++) {
                newBytes[i] = byteToAdd;
            }
            for (int j = 0; j < originalBytes.length; j++) {
                newBytes[numberOfBytesToInsert + j] = originalBytes[j];
            }
        } else if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
            for (int j = 0; j < originalBytes.length; j++) {
                newBytes[j] = originalBytes[j];
            }
            for (int i = 0; i < numberOfBytesToInsert; i++) {
                newBytes[i + originalBytes.length] = byteToAdd;
            }
        } else {
            throw new IllegalStateException("Unrecognized ByteOrder value[" + byteOrder + "].");
        }

        return newBytes;
    }

    /**
     * Detect whether this a negative value.
     * 
     * @param bytes
     * @param byteOrder
     * @param isSigned
     * @return true if the provided details indicate a negative valued number
     */
    private static boolean isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned) {
        boolean isNegative = false;

        if (isSigned) {
            byte mostSignificantByte = getMostSignificantByte(bytes, byteOrder);
            isNegative = isBitOn(mostSignificantByte, BITS_PER_BYTE - 1);
        }

        return isNegative;
    }

    /**
     * Return the most significant byte in the provided byte array based on endianess.
     * 
     * @param bytes
     * @param byteOrder
     * @return the most significant byte based on endianess.
     */
    private static byte getMostSignificantByte(byte[] bytes, ByteOrder byteOrder) {
        byte mostSignificantByte = bytes[0];
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
            // do nothing
            // mostSignificantByte = bytes[0];
        } else if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
            mostSignificantByte = bytes[bytes.length - 1];
        } else {
            throw new IllegalStateException("Unrecognized ByteOrder value[" + byteOrder + "].");
        }
        return mostSignificantByte;
    }

    /**
     * Return true is the bit found at the bitIndex position is on (meaning it has a value of 1).
     * 
     * @param aByte
     * @param bitIndex
     * @return true if the bitIndex position is occupied by a 1.
     */
    public static boolean isBitOn(byte aByte, int bitIndex) {
        boolean isBitOn = getBitValue(aByte, bitIndex) == 1;
        return isBitOn;
    }

    /**
     * Return the bit value(0 or 1) for the bit found at the bitIndex position in the provided byte.
     * 
     * @param aByte
     * @param bitIndex
     * @return 0 or 1 based on the bit value at the bitIndex position in the provided byte.
     */
    private static int getBitValue(byte aByte, int bitIndex) {
        if (bitIndex >= BITS_PER_BYTE) {
            throw new ArrayIndexOutOfBoundsException("The provided bitIndex[" + bitIndex
                    + "] is larger than the size of a byte[" + BITS_PER_BYTE + "].");
        }

        if (bitIndex < 0) {
            throw new ArrayIndexOutOfBoundsException(
                    "The provided bitIndex[" + bitIndex + "] must be greater than or equal to zero.");
        }

        int value = (aByte >> bitIndex) & 1;
        return value;
    }
}

Related

  1. getInt(byte[] b, int start, int end, ByteOrder byteOrder)
  2. getInt(final int offset, final byte[] buffer, final ByteOrder byteOrder)
  3. getLong(byte[] b, int start, int end, ByteOrder byteOrder)
  4. getMostSignificantByte(byte[] bytes, ByteOrder byteOrder)
  5. getUnsignedShort(final int offset, final byte[] buffer, final ByteOrder byteOrder)
  6. isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
  7. longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
  8. opposite(ByteOrder order)
  9. reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)