Skip to content

Commit

Permalink
Fix setNextDate, add parameter to getNextByte and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
devnied committed Dec 30, 2013
1 parent 5aa0937 commit 489db1e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/main/java/fr/devnied/bitlib/BitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,31 @@ public boolean getNextBoolean() {
}

/**
* Method to get The next bytes with the specified size
* Method used to get the next byte and shift read data to the beginning of the array.<br/>
* (Ex 00110000b if we start read 2 bit at index 2 the data returned will be 11000000b)
*
* @param pSize
* the length of byte to read
* @return a byte tab
* the size in bit to read
* @return the byte array read
*/
public byte[] getNextByte(final int pSize) {
return getNextByte(pSize, true);
}

/**
* Method to get The next bytes with the specified size
*
* @param pSize
* the size in bit to read
* @param pShift
* boolean to indicate if the data read will be shift to the left.<br/>
* <ul>
* <li>if true : (Ex 10110000b if we start read 2 bit at index 2 the returned data will be 11000000b)</li>
* <li>if false : (Ex 10110000b if we start read 2 bit at index 2 the returned data will be 00110000b)</li>
* </ul>
* @return a byte array
*/
public byte[] getNextByte(final int pSize, final boolean pShift) {
byte[] tab = new byte[(int) Math.ceil(pSize / BYTE_SIZE_F)];

if (currentBitIndex % BYTE_SIZE != 0) {
Expand All @@ -171,17 +189,27 @@ public byte[] getNextByte(final int pSize) {
int modTab = index % BYTE_SIZE;
int length = Math.min(max - currentBitIndex, Math.min(BYTE_SIZE - mod, BYTE_SIZE - modTab));
byte val = (byte) (byteTab[currentBitIndex / BYTE_SIZE] & getMask(mod, length));
if (mod != 0) {
val = (byte) (val << Math.min(mod, BYTE_SIZE - length));
} else {
val = (byte) ((val & DEFAULT_VALUE) >> modTab);
if (pShift || pSize % BYTE_SIZE == 0) {
if (mod != 0) {
val = (byte) (val << Math.min(mod, BYTE_SIZE - length));
} else {
val = (byte) ((val & DEFAULT_VALUE) >> modTab);
}
}
tab[index / BYTE_SIZE] |= val;
currentBitIndex += length;
index += length;
}
if (!pShift && pSize % BYTE_SIZE != 0) {
tab[tab.length - 1] = (byte) (tab[tab.length - 1] & getMask((max - pSize - 1) % BYTE_SIZE, BYTE_SIZE));
}
} else {
System.arraycopy(byteTab, currentBitIndex / BYTE_SIZE, tab, 0, tab.length);
int val = pSize % BYTE_SIZE;
if (val == 0) {
val = BYTE_SIZE;
}
tab[tab.length - 1] = (byte) (tab[tab.length - 1] & getMask(currentBitIndex % BYTE_SIZE, val));
currentBitIndex += pSize;
}

Expand Down Expand Up @@ -219,7 +247,7 @@ public Date getNextDate(final int pSize, final String pPattern) {
* @return the string
*/
public String getNextHexaString(final int pSize) {
return BytesUtils.bytesToStringNoSpace(getNextByte(pSize));
return BytesUtils.bytesToStringNoSpace(getNextByte(pSize, true));
}

/**
Expand Down Expand Up @@ -263,8 +291,7 @@ public int getNextInteger(final int pLength) {
}

/**
* This method is used to get the next String with the specified size with
* the charset ASCII
* This method is used to get the next String with the specified size with the charset ASCII
*
* @param pSize
* the length of the string in bit
Expand All @@ -284,7 +311,7 @@ public String getNextString(final int pSize) {
* @return the string
*/
public String getNextString(final int pSize, final Charset pCharset) {
return new String(getNextByte(pSize), pCharset);
return new String(getNextByte(pSize, true), pCharset);
}

/**
Expand Down Expand Up @@ -400,7 +427,7 @@ public void setNextDate(final Date pValue, final String pPattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pPattern);
String value = sdf.format(pValue);
// get String
setNextString(value, value.length());
setNextString(value, value.length() * 8);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/fr/devnied/bitlib/BitUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ public void testGetHexaString() {
Assertions.assertThat(bit.getNextHexaString(8)).isEqualTo("EF");
}

@Test
public void testGetNextByte() {
BitUtils bit = new BitUtils(test);
Assertions.assertThat(bit.getNextByte(4, false)).isEqualTo(new byte[] { 0x10 });
Assertions.assertThat(bit.getNextByte(4, false)).isEqualTo(new byte[] { 0x01 });
Assertions.assertThat(bit.getNextByte(16, false)).isEqualTo(new byte[] { 0x00, 0x00 });
Assertions.assertThat(bit.getNextByte(2, false)).isEqualTo(new byte[] { 0x40 });
Assertions.assertThat(bit.getNextByte(2, false)).isEqualTo(new byte[] { 0x20 });
bit.reset();
Assertions.assertThat(bit.getNextByte(2, false)).isEqualTo(new byte[] { 0x00 });
Assertions.assertThat(bit.getNextByte(8, false)).isEqualTo(new byte[] { 0x44 });
bit.reset();
Assertions.assertThat(bit.getNextByte(4)).isEqualTo(new byte[] { 0x10 });
Assertions.assertThat(bit.getNextByte(4)).isEqualTo(new byte[] { 0x10 });

bit = new BitUtils(new byte[] { 0x44, (byte) 0xFE });
Assertions.assertThat(bit.getNextByte(4, false)).isEqualTo(new byte[] { 0x40 });
Assertions.assertThat(bit.getNextByte(8, false)).isEqualTo(new byte[] { (byte) 0x4F });
Assertions.assertThat(bit.getNextByte(4, false)).isEqualTo(new byte[] { (byte) 0x0E });
bit.reset();
Assertions.assertThat(bit.getNextByte(4, true)).isEqualTo(new byte[] { 0x40 });
Assertions.assertThat(bit.getNextByte(8, true)).isEqualTo(new byte[] { (byte) 0x4F });
Assertions.assertThat(bit.getNextByte(4, true)).isEqualTo(new byte[] { (byte) 0xE0 });
}

/**
* @throws ParseException
*/
Expand Down Expand Up @@ -303,6 +328,18 @@ public void testSetNextByte2() {
Assertions.assertThat(bit.getNextHexaString(8)).isEqualTo("89");
}

@Test
public void testSetNextDate() throws ParseException {

String val = "20130108";
BitUtils bit = new BitUtils(100);
SimpleDateFormat sdf = new SimpleDateFormat(BitUtils.DATE_FORMAT);
Date d = sdf.parse(val);
bit.setNextDate(d, BitUtils.DATE_FORMAT);
bit.reset();
Assertions.assertThat(sdf.format(bit.getNextDate(8 * 8, BitUtils.DATE_FORMAT))).isEqualTo(val);
}

/**
* Test the method to set an hexa string
*/
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/fr/devnied/bitlib/BytesUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public void testBytesToStringNoSpace() {
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(tab2)).isEqualTo("00010245");
}

@Test(expected = IllegalAccessException.class)
public void testConstructorPrivate() throws Exception {
BytesUtils.class.newInstance();
Assert.fail("Utility class constructor should be private");
}

/**
*
*/
Expand Down

0 comments on commit 489db1e

Please sign in to comment.