Skip to content

Commit cb1b7f0

Browse files
committed
Update Integer size issue + Add Long
Fix issue #4 Add setNextLong and getNextLong methods
1 parent 9af115c commit cb1b7f0

File tree

2 files changed

+163
-44
lines changed

2 files changed

+163
-44
lines changed

src/main/java/fr/devnied/bitlib/BitUtils.java

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

src/test/java/fr/devnied/bitlib/BitUtilsTest.java

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,100 @@ public void testSetInteger() {
293293

294294
bit.reset();
295295
try {
296-
bit.setNextInteger(500, 32);
296+
bit.setNextInteger(500, 33);
297297
Assert.fail();
298298
} catch (IllegalArgumentException iae) {
299+
}
300+
}
301+
302+
@Test
303+
public void testMaxMinValue(){
304+
BitUtils bit = new BitUtils(64);
305+
bit.setNextHexaString("FFFFFFFF", 32);
306+
bit.reset();
307+
Assertions.assertThat(bit.getNextInteger(32)).isEqualTo(-1);
308+
}
309+
310+
311+
312+
/**
313+
* Test the method to set an long
314+
*/
315+
@Test
316+
public void testSetLong() {
317+
318+
BitUtils bit3 = new BitUtils(128);
319+
bit3.setNextLong(10, 6);
320+
bit3.setNextLong(23, 12);
321+
bit3.setNextLong(5, 8);
322+
bit3.setNextLong(930, 16);
323+
bit3.setNextLong(5, 3);
324+
bit3.setNextLong(159, 8);
325+
bit3.setNextLong(7, 3);
326+
bit3.reset();
327+
328+
Assertions.assertThat(bit3.getNextLong(6)).isEqualTo(10);
329+
Assertions.assertThat(bit3.getNextLong(12)).isEqualTo(23);
330+
Assertions.assertThat(bit3.getNextLong(8)).isEqualTo(5);
331+
Assertions.assertThat(bit3.getNextLong(16)).isEqualTo(930);
332+
Assertions.assertThat(bit3.getNextLong(3)).isEqualTo(5);
333+
Assertions.assertThat(bit3.getNextLong(8)).isEqualTo(159);
334+
Assertions.assertThat(bit3.getNextLong(3)).isEqualTo(7);
335+
336+
BitUtils bit2 = new BitUtils(128);
337+
bit2.setNextLong(3, 2);
338+
bit2.setNextLong(1057, 14);
339+
bit2.setNextLong(1, 1);
340+
bit2.setNextLong(1532, 15);
341+
bit2.setNextLong(8, 8);
342+
bit2.setNextLong(1532, 15);
299343

344+
bit2.reset();
345+
346+
Assertions.assertThat(bit2.getNextLong(2)).isEqualTo(3);
347+
Assertions.assertThat(bit2.getNextLong(14)).isEqualTo(1057);
348+
Assertions.assertThat(bit2.getNextLong(1)).isEqualTo(1);
349+
Assertions.assertThat(bit2.getNextLong(15)).isEqualTo(1532);
350+
Assertions.assertThat(bit2.getNextLong(8)).isEqualTo(8);
351+
Assertions.assertThat(bit2.getNextLong(15)).isEqualTo(1532);
352+
353+
BitUtils bit = new BitUtils(64);
354+
bit.setNextLong(3, 2);
355+
bit.setNextLong(255, 8);
356+
bit.setNextLong(0, 2);
357+
bit.setNextLong(15, 4);
358+
bit.setNextLong(2, 2);
359+
bit.setNextLong(3, 3);
360+
bit.setNextLong(1, 1);
361+
bit.setNextLong(0, 1);
362+
bit.setNextLong(1, 1);
363+
bit.reset();
364+
365+
Assertions.assertThat(bit.getNextLong(2)).isEqualTo(3);
366+
Assertions.assertThat(bit.getNextLong(8)).isEqualTo(255);
367+
Assertions.assertThat(bit.getNextLong(2)).isEqualTo(0);
368+
Assertions.assertThat(bit.getNextLong(4)).isEqualTo(15);
369+
Assertions.assertThat(bit.getNextLong(2)).isEqualTo(2);
370+
Assertions.assertThat(bit.getNextLong(3)).isEqualTo(3);
371+
Assertions.assertThat(bit.getNextLong(1)).isEqualTo(1);
372+
Assertions.assertThat(bit.getNextLong(1)).isEqualTo(0);
373+
Assertions.assertThat(bit.getNextLong(1)).isEqualTo(1);
374+
375+
bit.reset();
376+
try {
377+
bit.setNextLong(500, 65);
378+
Assert.fail();
379+
} catch (IllegalArgumentException iae) {
300380
}
301381
}
382+
383+
@Test
384+
public void testLongMaxMinValue(){
385+
BitUtils bit = new BitUtils(64);
386+
bit.setNextHexaString("FFFFFFFFFFFFFFFF", 64);
387+
bit.reset();
388+
Assertions.assertThat(bit.getNextInteger(64)).isEqualTo(-1);
389+
}
302390

303391
/**
304392
* Test the method to set an integer

0 commit comments

Comments
 (0)