Skip to content

Commit bb93b49

Browse files
committed
#26 fixed saving of MSB0 array bytes if byte aligned
1 parent bcd666d commit bb93b49

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/io/JBBPBitOutputStream.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,16 @@ public void flush() throws IOException {
229229
@SuppressWarnings("NullableProblems")
230230
@Override
231231
public void write(final byte[] b, final int off, final int len) throws IOException {
232-
if (this.bitBufferCount == 0) {
233-
out.write(b, off, len);
234-
this.byteCounter += len;
235-
} else {
232+
if (this.msb0 || this.bitBufferCount != 0) {
236233
int i = off;
237234
int cnt = len;
238235
while (cnt > 0) {
239236
this.write(b[i++]);
240237
cnt--;
241238
}
239+
} else {
240+
out.write(b, off, len);
241+
this.byteCounter += len;
242242
}
243243
}
244244

jbbp/src/test/java/com/igormaznitsa/jbbp/io/JBBPBitInputStreamTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616

1717
package com.igormaznitsa.jbbp.io;
1818

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.fail;
24+
25+
1926
import com.igormaznitsa.jbbp.TestUtils;
2027
import com.igormaznitsa.jbbp.utils.JBBPUtils;
21-
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.function.Executable;
23-
2428
import java.io.ByteArrayInputStream;
2529
import java.io.EOFException;
2630
import java.io.IOException;
2731
import java.util.Random;
28-
29-
import static org.junit.jupiter.api.Assertions.*;
32+
import org.junit.jupiter.api.Test;
3033

3134
public class JBBPBitInputStreamTest {
3235

@@ -162,6 +165,11 @@ public void testReadString_BigEndan_ShortString() throws Exception {
162165
assertEquals("ABC", asInputStream(0x03, 65, 66, 67).readString(JBBPByteOrder.BIG_ENDIAN));
163166
}
164167

168+
@Test
169+
public void testReadString_BigEndan_Msb0_ShortString() throws Exception {
170+
assertEquals("zzzz", asInputStreamMSB0(0x20, '^', '^', '^', '^').readString(JBBPByteOrder.BIG_ENDIAN));
171+
}
172+
165173
@Test
166174
public void testReadString_LittleEndan_Null() throws Exception {
167175
assertEquals(null, asInputStream(0xFF).readString(JBBPByteOrder.LITTLE_ENDIAN));

jbbp/src/test/java/com/igormaznitsa/jbbp/io/JBBPOutTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testString() throws Exception {
5151

5252
assertArrayEquals(new byte[] {(byte) 0xFF}, BeginBin(JBBPBitOrder.MSB0).String(null).End().toByteArray());
5353
assertArrayEquals(new byte[] {0}, BeginBin(JBBPBitOrder.MSB0).String("").End().toByteArray());
54-
assertArrayEquals(new byte[] {(byte) 0xc0, (byte) 0x41, (byte) 0x42, (byte) 0x43}, BeginBin(JBBPBitOrder.MSB0).String("ABC").End().toByteArray());
54+
assertArrayEquals(new byte[] {(byte) 0xc0, (byte) 0x82, (byte) 0x42, (byte) 0xC2}, BeginBin(JBBPBitOrder.MSB0).String("ABC").End().toByteArray());
5555

5656
assertArrayEquals(new byte[] {(byte) 0xFF}, BeginBin().ByteOrder(JBBPByteOrder.LITTLE_ENDIAN).String(null).End().toByteArray());
5757
assertArrayEquals(new byte[] {0}, BeginBin().ByteOrder(JBBPByteOrder.LITTLE_ENDIAN).String("").End().toByteArray());

jbbp/src/test/java/com/igormaznitsa/jbbp/it/BasedOnQuestionsAndCasesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.igormaznitsa.jbbp.model.JBBPFieldArrayByte;
3636
import com.igormaznitsa.jbbp.model.JBBPFieldArrayLong;
3737
import com.igormaznitsa.jbbp.model.JBBPFieldLong;
38+
import com.igormaznitsa.jbbp.model.JBBPFieldString;
3839
import com.igormaznitsa.jbbp.model.JBBPFieldStruct;
3940
import com.igormaznitsa.jbbp.model.JBBPNumericField;
4041
import com.igormaznitsa.jbbp.utils.JBBPDslBuilder;
@@ -234,6 +235,19 @@ class Bits {
234235
assertArrayEquals(new byte[] {1, 0, 0, 1, 0, 0, 1, 0}, parsed.bit);
235236
}
236237

238+
/**
239+
* Case 03-feb-2020
240+
* <a href="https://github.com/raydac/java-binary-block-parser/issues/26">Issue #26, Bug in parsing of stringj written in MSB0</a>
241+
*
242+
* @throws Exception for any error
243+
*/
244+
@Test
245+
public void testStringMsb0() throws Exception {
246+
JBBPOut joparam = JBBPOut.BeginBin(JBBPByteOrder.BIG_ENDIAN, JBBPBitOrder.MSB0).String("zzzz");
247+
final JBBPFieldStruct bitflds = JBBPParser.prepare("stringj fin;", JBBPBitOrder.MSB0).parse(joparam.End().toByteArray());
248+
assertEquals("zzzz", bitflds.findFieldForNameAndType("fin", JBBPFieldString.class).getAsString());
249+
}
250+
237251
/**
238252
* Case 10-aug-2017
239253
* NullPointer exception when referencing a JBBPCustomFieldTypeProcessor parsed field.

0 commit comments

Comments
 (0)