Class BitUtils


  • public final class BitUtils
    extends java.lang.Object
    Class to manage bit with java
    Author:
    Millau Julien
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BYTE_SIZE
      Constant for byte size
      static float BYTE_SIZE_F
      Constant for byte size (float)
      static java.lang.String DATE_FORMAT
      Simple date format (yyyMMdd)
    • Constructor Summary

      Constructors 
      Constructor Description
      BitUtils​(byte[] pByte)
      Constructor of the class
      BitUtils​(int pSize)
      Constructor for empty byte tab
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addCurrentBitIndex​(int pIndex)
      Add pIndex to the current value of bitIndex
      void clear()
      Method used to clear data and reset current bit index
      int getCurrentBitIndex()
      Getter for the currentBitIndex
      byte[] getData()
      Method to get all data
      byte getMask​(int pIndex, int pLength)
      This method is used to get a mask dynamically
      boolean getNextBoolean()
      Get the Next boolean (read 1 bit)
      byte[] getNextByte​(int pSize)
      Method used to get the next byte and shift read data to the beginning of the array.
      (Ex 00110000b if we start read 2 bit at index 2 the data returned will be 11000000b)
      byte[] getNextByte​(int pSize, boolean pShift)
      Method to get The next bytes with the specified size
      java.util.Date getNextDate​(int pSize, java.lang.String pPattern)
      Method to get the next date
      java.util.Date getNextDate​(int pSize, java.lang.String pPattern, boolean pUseBcd)
      Method to get the next date
      java.lang.String getNextHexaString​(int pSize)
      This method is used to get the next String in Hexa
      int getNextInteger​(int pLength)
      This method is used to get an integer with the specified size Be careful with java integer bit sign.
      int getNextIntegerSigned​(int pLength)
      Method used to get get a signed integer with the specified size
      long getNextLong​(int pLength)
      This method is used to get a long with the specified size Be careful with java long bit sign.
      long getNextLongSigned​(int pLength)
      Method used to get get a signed long with the specified size
      java.lang.String getNextString​(int pSize)
      This method is used to get the next String with the specified size with the charset ASCII
      java.lang.String getNextString​(int pSize, java.nio.charset.Charset pCharset)
      This method is used to get the next String with the specified size
      int getSize()
      Method used to get the size of the bit array
      void reset()
      Reset the current bit index to the initial position
      void resetNextBits​(int pLength)
      Set to 0 the next N bits
      void setCurrentBitIndex​(int pCurrentBitIndex)
      Setter currentBitIndex
      void setNextBoolean​(boolean pBoolean)
      Method to set a boolean
      void setNextByte​(byte[] pValue, int pLength)
      Method to write bytes with the max length
      void setNextByte​(byte[] pValue, int pLength, boolean pPadBefore)
      Method to write bytes with the max length
      void setNextDate​(java.util.Date pValue, java.lang.String pPattern)
      Method to write a date
      void setNextDate​(java.util.Date pValue, java.lang.String pPattern, boolean pUseBcd)
      Method to write a date
      void setNextHexaString​(java.lang.String pValue, int pLength)
      Method to write Hexa String with the max length
      void setNextInteger​(int pValue, int pLength)
      Add Integer to the current position with the specified size Be careful with java integer bit sign
      void setNextLong​(long pValue, int pLength)
      Add Long to the current position with the specified size Be careful with java long bit sign
      void setNextString​(java.lang.String pValue, int pLength)
      Method to write String
      void setNextString​(java.lang.String pValue, int pLength, boolean pPaddedBefore)
      Method to write a String
      protected void setNextValue​(long pValue, int pLength, int pMaxSize)
      Add Value to the current position with the specified size
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BYTE_SIZE_F

        public static final float BYTE_SIZE_F
        Constant for byte size (float)
        See Also:
        Constant Field Values
      • DATE_FORMAT

        public static final java.lang.String DATE_FORMAT
        Simple date format (yyyMMdd)
        See Also:
        Constant Field Values
    • Constructor Detail

      • BitUtils

        public BitUtils​(byte[] pByte)
        Constructor of the class
        Parameters:
        pByte - byte read
      • BitUtils

        public BitUtils​(int pSize)
        Constructor for empty byte tab
        Parameters:
        pSize - the size of the tab in bit
    • Method Detail

      • addCurrentBitIndex

        public void addCurrentBitIndex​(int pIndex)
        Add pIndex to the current value of bitIndex
        Parameters:
        pIndex - the value to add to bitIndex
      • getCurrentBitIndex

        public int getCurrentBitIndex()
        Getter for the currentBitIndex
        Returns:
        the currentBitIndex
      • getData

        public byte[] getData()
        Method to get all data
        Returns:
        a byte tab which contain all data
      • getMask

        public byte getMask​(int pIndex,
                            int pLength)
        This method is used to get a mask dynamically
        Parameters:
        pIndex - start index of the mask
        pLength - size of mask
        Returns:
        the mask in byte
      • getNextBoolean

        public boolean getNextBoolean()
        Get the Next boolean (read 1 bit)
        Returns:
        true or false
      • getNextByte

        public byte[] getNextByte​(int pSize)
        Method used to get the next byte and shift read data to the beginning of the array.
        (Ex 00110000b if we start read 2 bit at index 2 the data returned will be 11000000b)
        Parameters:
        pSize - the size in bit to read
        Returns:
        the byte array read
      • getNextByte

        public byte[] getNextByte​(int pSize,
                                  boolean pShift)
        Method to get The next bytes with the specified size
        Parameters:
        pSize - the size in bit to read
        pShift - boolean to indicate if the data read will be shift to the left.
        • if true : (Ex 10110000b if we start read 2 bit at index 2 the returned data will be 11000000b)
        • if false : (Ex 10110000b if we start read 2 bit at index 2 the returned data will be 00110000b)
        Returns:
        a byte array
      • getNextDate

        public java.util.Date getNextDate​(int pSize,
                                          java.lang.String pPattern)
        Method to get the next date
        Parameters:
        pSize - the size of the string date in bit
        pPattern - the Date pattern
        Returns:
        a date object or null
      • getNextDate

        public java.util.Date getNextDate​(int pSize,
                                          java.lang.String pPattern,
                                          boolean pUseBcd)
        Method to get the next date
        Parameters:
        pSize - the size of the string date in bit
        pPattern - the Date pattern
        pUseBcd - get the Date with BCD format (Binary coded decimal)
        Returns:
        a date object or null
      • getNextHexaString

        public java.lang.String getNextHexaString​(int pSize)
        This method is used to get the next String in Hexa
        Parameters:
        pSize - the length of the string in bit
        Returns:
        the string
      • getNextLongSigned

        public long getNextLongSigned​(int pLength)
        Method used to get get a signed long with the specified size
        Parameters:
        pLength - length of long to get (must be lower than 64)
        Returns:
        the long value
      • getNextIntegerSigned

        public int getNextIntegerSigned​(int pLength)
        Method used to get get a signed integer with the specified size
        Parameters:
        pLength - the length of the integer (must be lower than 32)
        Returns:
        the integer value
      • getNextLong

        public long getNextLong​(int pLength)
        This method is used to get a long with the specified size Be careful with java long bit sign. This method doesn't handle signed values.
        For that, @see BitUtils.getNextLongSigned()
        Parameters:
        pLength - the length of the data to read in bit
        Returns:
        an long
      • getNextInteger

        public int getNextInteger​(int pLength)
        This method is used to get an integer with the specified size Be careful with java integer bit sign. This method doesn't handle signed values.
        For that, @see BitUtils.getNextIntegerSigned()
        Parameters:
        pLength - the length of the data to read in bit
        Returns:
        an integer
      • getNextString

        public java.lang.String getNextString​(int pSize)
        This method is used to get the next String with the specified size with the charset ASCII
        Parameters:
        pSize - the length of the string in bit
        Returns:
        the string
      • getNextString

        public java.lang.String getNextString​(int pSize,
                                              java.nio.charset.Charset pCharset)
        This method is used to get the next String with the specified size
        Parameters:
        pSize - the length of the string int bit
        pCharset - the charset
        Returns:
        the string
      • getSize

        public int getSize()
        Method used to get the size of the bit array
        Returns:
        the size in bits of the current bit array
      • reset

        public void reset()
        Reset the current bit index to the initial position
      • clear

        public void clear()
        Method used to clear data and reset current bit index
      • resetNextBits

        public void resetNextBits​(int pLength)
        Set to 0 the next N bits
        Parameters:
        pLength - the number of bit to set at 0
      • setCurrentBitIndex

        public void setCurrentBitIndex​(int pCurrentBitIndex)
        Setter currentBitIndex
        Parameters:
        pCurrentBitIndex - the currentBitIndex to set
      • setNextBoolean

        public void setNextBoolean​(boolean pBoolean)
        Method to set a boolean
        Parameters:
        pBoolean - the boolean to set
      • setNextByte

        public void setNextByte​(byte[] pValue,
                                int pLength)
        Method to write bytes with the max length
        Parameters:
        pValue - the value to write
        pLength - the length of the data in bits
      • setNextByte

        public void setNextByte​(byte[] pValue,
                                int pLength,
                                boolean pPadBefore)
        Method to write bytes with the max length
        Parameters:
        pValue - the value to write
        pLength - the length of the data in bits
        pPadBefore - if true pad with 0
      • setNextDate

        public void setNextDate​(java.util.Date pValue,
                                java.lang.String pPattern)
        Method to write a date
        Parameters:
        pValue - the value to write
        pPattern - the Date pattern
      • setNextDate

        public void setNextDate​(java.util.Date pValue,
                                java.lang.String pPattern,
                                boolean pUseBcd)
        Method to write a date
        Parameters:
        pValue - the value to write
        pPattern - the Date pattern
        pUseBcd - write date as BCD (binary coded decimal)
      • setNextHexaString

        public void setNextHexaString​(java.lang.String pValue,
                                      int pLength)
        Method to write Hexa String with the max length
        Parameters:
        pValue - the value to write
        pLength - the length of the data in bits
      • setNextLong

        public void setNextLong​(long pValue,
                                int pLength)
        Add Long to the current position with the specified size Be careful with java long bit sign
        Parameters:
        pValue - the value to set
        pLength - the length of the long
      • setNextValue

        protected void setNextValue​(long pValue,
                                    int pLength,
                                    int pMaxSize)
        Add Value to the current position with the specified size
        Parameters:
        pValue - value to add
        pLength - length of the value
        pMaxSize - max size in bits
      • setNextInteger

        public void setNextInteger​(int pValue,
                                   int pLength)
        Add Integer to the current position with the specified size Be careful with java integer bit sign
        Parameters:
        pValue - the value to set
        pLength - the length of the integer
      • setNextString

        public void setNextString​(java.lang.String pValue,
                                  int pLength)
        Method to write String
        Parameters:
        pValue - the string to write
        pLength - the length of the integer
      • setNextString

        public void setNextString​(java.lang.String pValue,
                                  int pLength,
                                  boolean pPaddedBefore)
        Method to write a String
        Parameters:
        pValue - the string to write
        pLength - the string length
        pPaddedBefore - indicate if the string is padded before or after