Package fr.devnied.bitlib
Class BitUtils
- java.lang.Object
-
- fr.devnied.bitlib.BitUtils
-
public final class BitUtils extends java.lang.ObjectClass to manage bit with java- Author:
- Millau Julien
-
-
Field Summary
Fields Modifier and Type Field Description static intBYTE_SIZEConstant for byte sizestatic floatBYTE_SIZE_FConstant for byte size (float)static java.lang.StringDATE_FORMATSimple date format (yyyMMdd)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCurrentBitIndex(int pIndex)Add pIndex to the current value of bitIndexvoidclear()Method used to clear data and reset current bit indexintgetCurrentBitIndex()Getter for the currentBitIndexbyte[]getData()Method to get all databytegetMask(int pIndex, int pLength)This method is used to get a mask dynamicallybooleangetNextBoolean()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 sizejava.util.DategetNextDate(int pSize, java.lang.String pPattern)Method to get the next datejava.util.DategetNextDate(int pSize, java.lang.String pPattern, boolean pUseBcd)Method to get the next datejava.lang.StringgetNextHexaString(int pSize)This method is used to get the next String in HexaintgetNextInteger(int pLength)This method is used to get an integer with the specified size Be careful with java integer bit sign.intgetNextIntegerSigned(int pLength)Method used to get get a signed integer with the specified sizelonggetNextLong(int pLength)This method is used to get a long with the specified size Be careful with java long bit sign.longgetNextLongSigned(int pLength)Method used to get get a signed long with the specified sizejava.lang.StringgetNextString(int pSize)This method is used to get the next String with the specified size with the charset ASCIIjava.lang.StringgetNextString(int pSize, java.nio.charset.Charset pCharset)This method is used to get the next String with the specified sizeintgetSize()Method used to get the size of the bit arrayvoidreset()Reset the current bit index to the initial positionvoidresetNextBits(int pLength)Set to 0 the next N bitsvoidsetCurrentBitIndex(int pCurrentBitIndex)Setter currentBitIndexvoidsetNextBoolean(boolean pBoolean)Method to set a booleanvoidsetNextByte(byte[] pValue, int pLength)Method to write bytes with the max lengthvoidsetNextByte(byte[] pValue, int pLength, boolean pPadBefore)Method to write bytes with the max lengthvoidsetNextDate(java.util.Date pValue, java.lang.String pPattern)Method to write a datevoidsetNextDate(java.util.Date pValue, java.lang.String pPattern, boolean pUseBcd)Method to write a datevoidsetNextHexaString(java.lang.String pValue, int pLength)Method to write Hexa String with the max lengthvoidsetNextInteger(int pValue, int pLength)Add Integer to the current position with the specified size Be careful with java integer bit signvoidsetNextLong(long pValue, int pLength)Add Long to the current position with the specified size Be careful with java long bit signvoidsetNextString(java.lang.String pValue, int pLength)Method to write StringvoidsetNextString(java.lang.String pValue, int pLength, boolean pPaddedBefore)Method to write a Stringprotected voidsetNextValue(long pValue, int pLength, int pMaxSize)Add Value to the current position with the specified size
-
-
-
Field Detail
-
BYTE_SIZE
public static final int BYTE_SIZE
Constant for byte size- See Also:
- Constant Field Values
-
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
-
-
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 maskpLength- 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 readpShift- 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 bitpPattern- 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 bitpPattern- the Date patternpUseBcd- 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 bitpCharset- 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 writepLength- 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 writepLength- the length of the data in bitspPadBefore- 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 writepPattern- 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 writepPattern- the Date patternpUseBcd- 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 writepLength- 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 setpLength- 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 addpLength- length of the valuepMaxSize- 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 setpLength- the length of the integer
-
setNextString
public void setNextString(java.lang.String pValue, int pLength)Method to write String- Parameters:
pValue- the string to writepLength- 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 writepLength- the string lengthpPaddedBefore- indicate if the string is padded before or after
-
-