33import org .slf4j .Logger ;
44import org .slf4j .LoggerFactory ;
55
6- import java .nio .Buffer ;
7- import java .nio .ByteBuffer ;
86import java .nio .charset .Charset ;
97import java .text .ParseException ;
108import java .text .SimpleDateFormat ;
@@ -26,10 +24,6 @@ public final class BitUtils {
2624 * Constant for byte size
2725 */
2826 public static final int BYTE_SIZE = Byte .SIZE ;
29- /**
30- * Constant for byte size (float)
31- */
32- public static final float BYTE_SIZE_F = Byte .SIZE ;
3327 /**
3428 * 255 init value
3529 */
@@ -66,8 +60,7 @@ public final class BitUtils {
6660 * byte read
6761 */
6862 public BitUtils (final byte [] pByte ) {
69- byteTab = new byte [pByte .length ];
70- System .arraycopy (pByte , 0 , byteTab , 0 , pByte .length );
63+ byteTab = Arrays .copyOf (pByte , pByte .length );
7164 size = pByte .length * BYTE_SIZE ;
7265 }
7366
@@ -78,7 +71,7 @@ public BitUtils(final byte[] pByte) {
7871 * the size of the tab in bit
7972 */
8073 public BitUtils (final int pSize ) {
81- byteTab = new byte [(int ) Math . ceil ( pSize / BYTE_SIZE_F ) ];
74+ byteTab = new byte [(pSize + BYTE_SIZE - 1 ) / BYTE_SIZE ];
8275 size = pSize ;
8376 }
8477
@@ -110,9 +103,7 @@ public int getCurrentBitIndex() {
110103 * @return a byte tab which contain all data
111104 */
112105 public byte [] getData () {
113- byte [] ret = new byte [byteTab .length ];
114- System .arraycopy (byteTab , 0 , ret , 0 , byteTab .length );
115- return ret ;
106+ return Arrays .copyOf (byteTab , byteTab .length );
116107 }
117108
118109 /**
@@ -178,7 +169,7 @@ public byte[] getNextByte(final int pSize) {
178169 * @return a byte array
179170 */
180171 public byte [] getNextByte (final int pSize , final boolean pShift ) {
181- byte [] tab = new byte [(int ) Math . ceil ( pSize / BYTE_SIZE_F ) ];
172+ byte [] tab = new byte [(pSize + BYTE_SIZE - 1 ) / BYTE_SIZE ];
182173
183174 if (currentBitIndex % BYTE_SIZE != 0 ) {
184175 int index = 0 ;
@@ -311,36 +302,21 @@ public int getNextIntegerSigned(final int pLength) {
311302 * @return an long
312303 */
313304 public long getNextLong (final int pLength ) {
314- // allocate Size of Integer
315- ByteBuffer buffer = ByteBuffer .allocate (BYTE_SIZE * 2 );
316- // final value
317305 long finalValue = 0 ;
318- // Incremental value
319- long currentValue = 0 ;
320- // Size to read
306+ long currentValue ;
321307 int readSize = pLength ;
322- // length max of the index
323308 int max = currentBitIndex + pLength ;
324309 while (currentBitIndex < max ) {
325310 int mod = currentBitIndex % BYTE_SIZE ;
326- // apply the mask to the selected byte
327311 currentValue = byteTab [currentBitIndex / BYTE_SIZE ] & getMask (mod , readSize ) & DEFAULT_VALUE ;
328- // Shift right the read value
329312 int dec = Math .max (BYTE_SIZE - (mod + readSize ), 0 );
330313 currentValue = (currentValue & DEFAULT_VALUE ) >>> dec & DEFAULT_VALUE ;
331- // Shift left the previously read value and add the current value
332314 finalValue = finalValue << Math .min (readSize , BYTE_SIZE ) | currentValue ;
333- // calculate read value size
334315 int val = BYTE_SIZE - mod ;
335- // Decrease the size left
336316 readSize = readSize - val ;
337317 currentBitIndex = Math .min (currentBitIndex + val , max );
338318 }
339- buffer .putLong (finalValue );
340- // reset the current bytebuffer index to 0
341- ((Buffer )buffer ).rewind ();
342- // return integer
343- return buffer .getLong ();
319+ return finalValue ;
344320 }
345321
346322 /**
@@ -469,21 +445,11 @@ public void setNextByte(final byte[] pValue, final int pLength) {
469445 * if true pad with 0
470446 */
471447 public void setNextByte (final byte [] pValue , final int pLength , final boolean pPadBefore ) {
472- int totalSize = (int ) Math .ceil (pLength / BYTE_SIZE_F );
473- ByteBuffer buffer = ByteBuffer .allocate (totalSize );
474- int size = Math .max (totalSize - pValue .length , 0 );
475- if (pPadBefore ) {
476- for (int i = 0 ; i < size ; i ++) {
477- buffer .put ((byte ) 0 );
478- }
479- }
480- buffer .put (pValue , 0 , Math .min (totalSize , pValue .length ));
481- if (!pPadBefore ) {
482- for (int i = 0 ; i < size ; i ++) {
483- buffer .put ((byte ) 0 );
484- }
485- }
486- byte [] tab = buffer .array ();
448+ int totalSize = (pLength + BYTE_SIZE - 1 ) / BYTE_SIZE ;
449+ byte [] tab = new byte [totalSize ];
450+ int padSize = Math .max (totalSize - pValue .length , 0 );
451+ int copyLen = Math .min (totalSize , pValue .length );
452+ System .arraycopy (pValue , 0 , tab , pPadBefore ? padSize : 0 , copyLen );
487453 if (currentBitIndex % BYTE_SIZE != 0 ) {
488454 int index = 0 ;
489455 int max = currentBitIndex + pLength ;
@@ -587,22 +553,18 @@ public void setNextLong(final long pValue, final int pLength) {
587553 private void setNextValue (final long pValue , final int pLength , final int pMaxSize ) {
588554 long value = pValue ;
589555 // Set to max value if pValue cannot be stored on pLength bits.
590- long bitMax = ( long ) Math . pow ( 2 , Math .min (pLength , pMaxSize ) );
591- if (pValue > bitMax ) {
556+ long bitMax = 1L << Math .min (pLength , pMaxSize );
557+ if (bitMax > 0 && pValue >= bitMax ) {
592558 value = bitMax - 1 ;
593559 }
594- // size to wrote
595560 int writeSize = pLength ;
596561 while (writeSize > 0 ) {
597- // modulo
598562 int mod = currentBitIndex % BYTE_SIZE ;
599- byte ret = 0 ;
563+ byte ret ;
600564 if (mod == 0 && writeSize <= BYTE_SIZE || pLength < BYTE_SIZE - mod ) {
601- // shift left value
602565 ret = (byte ) (value << BYTE_SIZE - (writeSize + mod ));
603566 } else {
604- // shift right
605- long length = Long .toBinaryString (value ).length ();
567+ int length = value == 0 ? 1 : Long .SIZE - Long .numberOfLeadingZeros (value );
606568 ret = (byte ) (value >> writeSize - length - (BYTE_SIZE - length - mod ));
607569 }
608570 byteTab [currentBitIndex / BYTE_SIZE ] |= ret ;
@@ -656,6 +618,6 @@ public void setNextString(final String pValue, final int pLength) {
656618 * indicate if the string is padded before or after
657619 */
658620 public void setNextString (final String pValue , final int pLength , final boolean pPaddedBefore ) {
659- setNextByte (pValue .getBytes (Charset . defaultCharset () ), pLength , pPaddedBefore );
621+ setNextByte (pValue .getBytes (DEFAULT_CHARSET ), pLength , pPaddedBefore );
660622 }
661623}
0 commit comments