Skip to content

Commit 4fd6404

Browse files
committed
#37 added uint into jbbp to java converter
1 parent 51da9c7 commit 4fd6404

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJavaConverter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static com.igormaznitsa.jbbp.compiler.JBBPCompiler.CODE_UBYTE;
2828
import static com.igormaznitsa.jbbp.compiler.JBBPCompiler.CODE_USHORT;
2929

30-
3130
import com.igormaznitsa.jbbp.JBBPParser;
3231
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
3332
import com.igormaznitsa.jbbp.compiler.tokenizer.JBBPFieldTypeParameterContainer;
@@ -662,6 +661,9 @@ public void visitPrimitiveField(
662661

663662
if (altFieldType) {
664663
switch (type) {
664+
case BYTE:
665+
type = FieldType.UINT;
666+
break;
665667
case INT:
666668
type = FieldType.FLOAT;
667669
break;
@@ -1333,6 +1335,10 @@ private enum FieldType {
13331335
"%s.readStringArray(%s,%s)", "%s.writeString(%s,%s)",
13341336
"for(int I=0;I<%3$s;I++){%1$s.writeString(%2$s[I],%4$s);}",
13351337
"%1$s.writeStringArray(%2$s,%3$s)"),
1338+
UINT(BinType.UINT, BinType.UINT_ARRAY, -8, "long", "long", "%s.readUInt(%s)",
1339+
"%s.readUIntArray(%s,%s)", "%s.writeUInt(%s,%s)",
1340+
"for(int I=0;I<%3$s;I++){%1$s.writeUInt(%2$s[I],%4$s);}",
1341+
"for(int I=0;I<%2$s.length;I++){%1$s.writeUInt(%2$s[I],%3$s);}"),
13361342
UNKNOWN(BinType.UNDEFINED, BinType.UNDEFINED, Integer.MIN_VALUE, "", "", "", "", "", "", "");
13371343

13381344
private final BinType binType;

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,49 @@ public int[] readIntArray(final int items, final JBBPByteOrder byteOrder) throws
359359
}
360360
}
361361

362+
/**
363+
* Read number of unsigned integer items from the input stream.
364+
*
365+
* @param items number of items to be read from the input stream, if less than
366+
* zero then all stream till the end will be read
367+
* @param byteOrder the order of bytes to be used to decode values
368+
* @return read items as an unsigned integer array represented through long
369+
* @throws IOException it will be thrown for any transport problem during the
370+
* operation
371+
* @see JBBPByteOrder#BIG_ENDIAN
372+
* @see JBBPByteOrder#LITTLE_ENDIAN
373+
* @since 2.0.4
374+
*/
375+
public long[] readUIntArray(final int items, final JBBPByteOrder byteOrder) throws IOException {
376+
int pos = 0;
377+
if (items < 0) {
378+
long[] buffer = new long[INITIAL_ARRAY_BUFFER_SIZE];
379+
// till end
380+
while (hasAvailableData()) {
381+
final long next = readUInt(byteOrder);
382+
if (buffer.length == pos) {
383+
final long[] newBuffer = new long[buffer.length << 1];
384+
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
385+
buffer = newBuffer;
386+
}
387+
buffer[pos++] = next;
388+
}
389+
if (buffer.length == pos) {
390+
return buffer;
391+
}
392+
final long[] result = new long[pos];
393+
System.arraycopy(buffer, 0, result, 0, pos);
394+
return result;
395+
} else {
396+
// number
397+
final long[] buffer = new long[items];
398+
for (int i = 0; i < items; i++) {
399+
buffer[i] = readUInt(byteOrder);
400+
}
401+
return buffer;
402+
}
403+
}
404+
362405
/**
363406
* Read number of float items from the input stream.
364407
*
@@ -530,6 +573,26 @@ public int readInt(final JBBPByteOrder byteOrder) throws IOException {
530573
}
531574
}
532575

576+
/**
577+
* Read an unsigned integer value from the stream.
578+
*
579+
* @param byteOrder the order of bytes to be used to decode the read value
580+
* @return the unsigned integer value from the stream
581+
* @throws IOException it will be thrown for any transport problem during the
582+
* operation
583+
* @throws EOFException if the end of the stream has been reached
584+
* @see JBBPByteOrder#BIG_ENDIAN
585+
* @see JBBPByteOrder#LITTLE_ENDIAN
586+
* @since 2.0.4
587+
*/
588+
public long readUInt(final JBBPByteOrder byteOrder) throws IOException {
589+
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
590+
return (((long) readUnsignedShort(byteOrder) << 16) | readUnsignedShort(byteOrder));
591+
} else {
592+
return readUnsignedShort(byteOrder) | ((long) readUnsignedShort(byteOrder) << 16);
593+
}
594+
}
595+
533596
/**
534597
* Read a float value from the stream.
535598
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ public void writeInt(final int value, final JBBPByteOrder byteOrder) throws IOEx
117117
}
118118
}
119119

120+
/**
121+
* Write an unsigned integer value into the output stream.
122+
*
123+
* @param value a value to be written into the output stream.
124+
* @param byteOrder the byte order of the value bytes to be used for writing.
125+
* @throws IOException it will be thrown for transport errors
126+
* @see JBBPByteOrder#BIG_ENDIAN
127+
* @see JBBPByteOrder#LITTLE_ENDIAN
128+
* @since 2.0.4
129+
*/
130+
public void writeUInt(final long value, final JBBPByteOrder byteOrder) throws IOException {
131+
final int v = (int) value;
132+
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
133+
this.writeShort(v >>> 16, byteOrder);
134+
this.writeShort(v, byteOrder);
135+
} else {
136+
this.writeShort(v, byteOrder);
137+
this.writeShort(v >>> 16, byteOrder);
138+
}
139+
}
140+
120141
/**
121142
* Write an float value into the output stream.
122143
*

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/BinType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public enum BinType {
8787
INT(JBBPFieldInt.class, false),
8888
/**
8989
* A Mapping field will be mapped to a parsed unsigned integer field.
90+
*
91+
* @since 2.0.4
9092
*/
9193
UINT(JBBPFieldUInt.class, false),
9294
/**

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,19 @@ protected void onFieldInt(final Object obj, final Field field, final Bin annotat
20062006
}
20072007
}
20082008

2009+
@Override
2010+
protected void onFieldUInt(Object obj, Field field, Bin annotation,
2011+
int value) {
2012+
try {
2013+
UInt(value);
2014+
if (this.arrayCounter == 0) {
2015+
Comment(makeFieldDescription(field, annotation));
2016+
}
2017+
} catch (IOException ex) {
2018+
throw new JBBPIOException("Can't log uint field", ex);
2019+
}
2020+
}
2021+
20092022
@Override
20102023
protected void onFieldFloat(final Object obj, final Field field, final Bin annotation,
20112024
final float value) {

jbbp/src/test/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJavaConverterReadWriteTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static org.junit.jupiter.api.Assertions.assertTrue;
2929
import static org.junit.jupiter.api.Assertions.fail;
3030

31-
3231
import com.igormaznitsa.jbbp.JBBPCustomFieldTypeProcessor;
3332
import com.igormaznitsa.jbbp.JBBPParser;
3433
import com.igormaznitsa.jbbp.TestUtils;
@@ -69,6 +68,8 @@ public void testReadWrite_ExtendsSuperClassAndUseItsFields() throws Exception {
6968
final JBBPParser parser = JBBPParser.prepare(
7069
"stringj str;"
7170
+ "stringj [2] strarr;"
71+
+ "uint uintf;"
72+
+ "uint [2] uintarr;"
7273
+ "floatj flt;"
7374
+ "floatj [2] fltarr;"
7475
+ "doublej dbl;"
@@ -112,6 +113,9 @@ public void testReadWrite_ExtendsSuperClassAndUseItsFields() throws Exception {
112113
final byte[] etalon = new byte[] {
113114
0,
114115
2, 49, 50, 1, 51,
116+
(byte) 0xFA, (byte) 0xFB, (byte) 0xFC, (byte) 0xFD,
117+
(byte) 0x1A, (byte) 0x1B, (byte) 0x1C, (byte) 0x1D, (byte) 0x2A, (byte) 0x2B, (byte) 0x2C,
118+
(byte) 0x2D,
115119
1, 2, 3, 4,
116120
5, 6, 7, 8, 9, 10, 11, 12,
117121
1, 2, 3, 4, 5, 6, 7, 8,
@@ -127,6 +131,8 @@ public void testReadWrite_ExtendsSuperClassAndUseItsFields() throws Exception {
127131

128132
assertNull(null, parsed.str);
129133
assertArrayEquals(new String[] {"12", "3"}, parsed.strarr);
134+
assertEquals(0xFAFBFCFDL, parsed.uintf);
135+
assertArrayEquals(new long[] {0x1A1B1C1DL, 0x2A2B2C2DL}, parsed.uintarr);
130136
assertEquals(2.3879393E-38f, parsed.flt);
131137
assertArrayEquals(new float[] {6.301941E-36f, 1.661634E-33f}, parsed.fltarr);
132138
assertEquals(8.20788039913184E-304d, parsed.dbl);
@@ -295,6 +301,17 @@ public void testReadWite_Double_SingleValue() throws Exception {
295301
assertArrayEquals(etalon, callWrite(instance));
296302
}
297303

304+
@Test
305+
public void testReadWite_UInt_SingleValue() throws Exception {
306+
final Object instance = compileAndMakeInstance("uint value;");
307+
final byte[] etalon = new byte[] {1, 2, 3, 4};
308+
309+
callRead(instance, etalon.clone());
310+
311+
assertEquals(0x01020304L, getField(instance, "value", Long.class).longValue());
312+
assertArrayEquals(etalon, callWrite(instance));
313+
}
314+
298315
@Test
299316
public void testReadWite_DoubleArrayWholeStream() throws Exception {
300317
final Object instance = compileAndMakeInstance("doublej [_] doubleArray;");
@@ -312,6 +329,22 @@ public void testReadWite_DoubleArrayWholeStream() throws Exception {
312329
assertArrayEquals(etalon, callWrite(instance));
313330
}
314331

332+
@Test
333+
public void testReadWite_UIntArrayWholeStream() throws Exception {
334+
final Object instance = compileAndMakeInstance("uint [_] uintArray;");
335+
assertNull(getField(instance, "uintarray", long[].class), "by default must be null");
336+
337+
final byte[] etalon =
338+
new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 22, 33};
339+
340+
callRead(instance, etalon.clone());
341+
342+
assertArrayEquals(
343+
new long[] {0x01020304L, 0x05060708L, 0x09001621L},
344+
getField(instance, "uintarray", long[].class));
345+
assertArrayEquals(etalon, callWrite(instance));
346+
}
347+
315348
@Test
316349
public void testReadWite_BitArrayWholeStream() throws Exception {
317350
final Object instance = compileAndMakeInstance("bit [_] bitArray;");
@@ -335,7 +368,7 @@ public void testReadWite_FloatFieldAsCounter() throws Exception {
335368

336369
callRead(instance, data.clone());
337370

338-
assertEquals(3.3f, getField(instance, "len", Float.class).floatValue(), TestUtils.FLOAT_DELTA);
371+
assertEquals(3.3f, getField(instance, "len", Float.class), TestUtils.FLOAT_DELTA);
339372
assertArrayEquals(new byte[] {1, 2, 3}, getField(instance, "data", byte[].class));
340373
assertArrayEquals(data, callWrite(instance));
341374
}
@@ -1099,6 +1132,9 @@ public interface ByteTestInterface {
10991132
}
11001133

11011134
public static class TestSuperclass {
1135+
1136+
public long uintf;
1137+
public long[] uintarr;
11021138
public String str;
11031139
public String[] strarr;
11041140
public float flt;

0 commit comments

Comments
 (0)