@@ -28,10 +28,6 @@ public final class BitUtils {
2828 * Constant for byte size (float)
2929 */
3030 public static final float BYTE_SIZE_F = Byte .SIZE ;
31- /**
32- * Integer size in byte
33- */
34- private static final int INTEGER_BYTE_SIZE = 4 ;
3531 /**
3632 * 255 init value
3733 */
@@ -271,44 +267,60 @@ public String getNextHexaString(final int pSize) {
271267 return BytesUtils .bytesToStringNoSpace (getNextByte (pSize , true ));
272268 }
273269
270+
271+ /**
272+ * This method is used to get an long with the specified size
273+ *
274+ * Be careful with java long bit sign
275+ *
276+ * @param pLength
277+ * the length of the data to read in bit
278+ * @return an long
279+ */
280+ public long getNextLong (final int pLength ) {
281+ // allocate Size of Integer
282+ ByteBuffer buffer = ByteBuffer .allocate (BYTE_SIZE * 2 );
283+ // final value
284+ long finalValue = 0 ;
285+ // Incremental value
286+ long currentValue = 0 ;
287+ // Size to read
288+ int readSize = pLength ;
289+ // length max of the index
290+ int max = currentBitIndex + pLength ;
291+ while (currentBitIndex < max ) {
292+ int mod = currentBitIndex % BYTE_SIZE ;
293+ // apply the mask to the selected byte
294+ currentValue = byteTab [currentBitIndex / BYTE_SIZE ] & getMask (mod , readSize ) & DEFAULT_VALUE ;
295+ // Shift right the read value
296+ int dec = Math .max (BYTE_SIZE - (mod + readSize ), 0 );
297+ currentValue = (currentValue & DEFAULT_VALUE ) >>> dec & DEFAULT_VALUE ;
298+ // Shift left the previously read value and add the current value
299+ finalValue = finalValue << Math .min (readSize , BYTE_SIZE ) | currentValue ;
300+ // calculate read value size
301+ int val = BYTE_SIZE - mod ;
302+ // Decrease the size left
303+ readSize = readSize - val ;
304+ currentBitIndex = Math .min (currentBitIndex + val , max );
305+ }
306+ buffer .putLong (finalValue );
307+ // reset the current bytebuffer index to 0
308+ buffer .rewind ();
309+ // return integer
310+ return buffer .getLong ();
311+ }
312+
274313 /**
275314 * This method is used to get an integer with the specified size
276315 *
316+ * Be careful with java integer bit sign
317+ *
277318 * @param pLength
278319 * the length of the data to read in bit
279320 * @return an integer
280321 */
281322 public int getNextInteger (final int pLength ) {
282- // allocate Size of Integer
283- ByteBuffer buffer = ByteBuffer .allocate (INTEGER_BYTE_SIZE );
284- // final value
285- int finalValue = 0 ;
286- // Incremental value
287- int currentValue = 0 ;
288- // Size to read
289- int readSize = pLength ;
290- // length max of the index
291- int max = currentBitIndex + pLength ;
292- while (currentBitIndex < max ) {
293- int mod = currentBitIndex % BYTE_SIZE ;
294- // apply the mask to the selected byte
295- currentValue = byteTab [currentBitIndex / BYTE_SIZE ] & getMask (mod , readSize ) & DEFAULT_VALUE ;
296- // Shift right the read value
297- int dec = Math .max (BYTE_SIZE - (mod + readSize ), 0 );
298- currentValue = (currentValue & DEFAULT_VALUE ) >>> dec & DEFAULT_VALUE ;
299- // Shift left the previously read value and add the current value
300- finalValue = finalValue << Math .min (readSize , BYTE_SIZE ) | currentValue ;
301- // calculate read value size
302- int val = BYTE_SIZE - mod ;
303- // Decrease the size left
304- readSize = readSize - val ;
305- currentBitIndex = Math .min (currentBitIndex + val , max );
306- }
307- buffer .putInt (finalValue );
308- // reset the current bytebuffer index to 0
309- buffer .rewind ();
310- // return integer
311- return buffer .getInt ();
323+ return (int ) (getNextLong (pLength ));
312324 }
313325
314326 /**
@@ -496,18 +508,20 @@ public void setNextDate(final Date pValue, final String pPattern, final boolean
496508 public void setNextHexaString (final String pValue , final int pLength ) {
497509 setNextByte (BytesUtils .fromString (pValue ), pLength );
498510 }
499-
511+
500512 /**
501- * Add Integer to the current position with the specified size
513+ * Add Long to the current position with the specified size
514+ *
515+ * Be careful with java long bit sign
502516 *
503517 * @param pLength
504- * the length of the integer
518+ * the length of the long
505519 */
506- public void setNextInteger (final int pValue , final int pLength ) {
507- int value = pValue ;
520+ public void setNextLong (final long pValue , final int pLength ) {
521+ long value = pValue ;
508522
509- if (pLength > 31 ) {
510- throw new IllegalArgumentException ("Integer overflow with length > 31 " );
523+ if (pLength > 64 ) {
524+ throw new IllegalArgumentException ("Overflow with length > 64 " );
511525 }
512526
513527 // Set to max value if pValue cannot be stored on pLength bits.
@@ -525,16 +539,33 @@ public void setNextInteger(final int pValue, final int pLength) {
525539 ret = (byte ) (value << BYTE_SIZE - (writeSize + mod ));
526540 } else {
527541 // shift right
528- int length = Integer .toBinaryString (value ).length ();
542+ long length = Long .toBinaryString (value ).length ();
529543 ret = (byte ) (value >> writeSize - length - (BYTE_SIZE - length - mod ));
530544 }
531545 byteTab [currentBitIndex / BYTE_SIZE ] |= ret ;
532- int val = Math .min (writeSize , BYTE_SIZE - mod );
546+ long val = Math .min (writeSize , BYTE_SIZE - mod );
533547 writeSize -= val ;
534548 currentBitIndex += val ;
535549 }
536550 }
537551
552+ /**
553+ * Add Integer to the current position with the specified size
554+ *
555+ * Be careful with java integer bit sign
556+ *
557+ * @param pLength
558+ * the length of the integer
559+ */
560+ public void setNextInteger (final int pValue , final int pLength ) {
561+
562+ if (pLength > 32 ) {
563+ throw new IllegalArgumentException ("Integer overflow with length > 32" );
564+ }
565+
566+ setNextLong (pValue , pLength );
567+ }
568+
538569 /**
539570 * Method to write String
540571 *
0 commit comments