Skip to content

Commit 51da9c7

Browse files
committed
#37 added uint type to cover unsigned int case through mapping on long
1 parent de000c0 commit 51da9c7

18 files changed

+959
-95
lines changed

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
- added converter of compiled parser data into Java class sources (1.6+)
5050
- added method to read unsigned short values as char [] into JBBPBitInputStream
5151
- Class version target has been changed to Java 1.6
52-
- fixed compatibiity of tests with Java 1.6
52+
- fixed compatibility of tests with Java 1.6
5353
- Minor refactoring
5454

5555
1.2.0

jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_FIELD_EMPTY;
2020

21-
2221
import com.igormaznitsa.jbbp.compiler.JBBPCompiledBlock;
2322
import com.igormaznitsa.jbbp.compiler.JBBPCompiler;
2423
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
@@ -43,6 +42,7 @@
4342
import com.igormaznitsa.jbbp.model.JBBPFieldArrayString;
4443
import com.igormaznitsa.jbbp.model.JBBPFieldArrayStruct;
4544
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUByte;
45+
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUInt;
4646
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUShort;
4747
import com.igormaznitsa.jbbp.model.JBBPFieldBit;
4848
import com.igormaznitsa.jbbp.model.JBBPFieldBoolean;
@@ -55,6 +55,7 @@
5555
import com.igormaznitsa.jbbp.model.JBBPFieldString;
5656
import com.igormaznitsa.jbbp.model.JBBPFieldStruct;
5757
import com.igormaznitsa.jbbp.model.JBBPFieldUByte;
58+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
5859
import com.igormaznitsa.jbbp.model.JBBPFieldUShort;
5960
import com.igormaznitsa.jbbp.model.JBBPNumericField;
6061
import com.igormaznitsa.jbbp.utils.JBBPIntCounter;
@@ -476,10 +477,15 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream,
476477
case JBBPCompiler.CODE_BYTE: {
477478
if (resultNotIgnored) {
478479
if (arrayLength < 0) {
479-
singleAtomicField = new JBBPFieldByte(name, (byte) inStream.readByte());
480+
singleAtomicField = fieldTypeDiff ?
481+
new JBBPFieldUInt(name, inStream.readInt(byteOrder) & 0xFFFFFFFFL) :
482+
new JBBPFieldByte(name, (byte) inStream.readByte());
480483
} else {
481-
structureFields.add(new JBBPFieldArrayByte(name,
482-
inStream.readByteArray(wholeStreamArray ? -1 : arrayLength, byteOrder)));
484+
structureFields.add(fieldTypeDiff ?
485+
new JBBPFieldArrayUInt(name,
486+
inStream.readIntArray(wholeStreamArray ? -1 : arrayLength, byteOrder)) :
487+
new JBBPFieldArrayByte(name,
488+
inStream.readByteArray(wholeStreamArray ? -1 : arrayLength, byteOrder)));
483489
}
484490
}
485491
}

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.igormaznitsa.jbbp.model.JBBPFieldDouble;
2929
import com.igormaznitsa.jbbp.model.JBBPFieldFloat;
3030
import com.igormaznitsa.jbbp.model.JBBPFieldString;
31+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
3132
import com.igormaznitsa.jbbp.utils.JBBPUtils;
3233
import java.io.ByteArrayOutputStream;
3334
import java.io.IOException;
@@ -630,6 +631,7 @@ private static int prepareCodeForToken(final JBBPToken token,
630631
result |= CODE_UBYTE;
631632
break;
632633
case "byte":
634+
case JBBPFieldUInt.TYPE_NAME:
633635
result |= CODE_BYTE;
634636
break;
635637
case "ushort":

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/tokenizer/JBBPFieldTypeParameterContainer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.igormaznitsa.jbbp.model.JBBPFieldDouble;
2121
import com.igormaznitsa.jbbp.model.JBBPFieldFloat;
2222
import com.igormaznitsa.jbbp.model.JBBPFieldString;
23+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
2324
import java.io.Serializable;
2425

2526
/**
@@ -108,18 +109,21 @@ public boolean hasExpressionAsExtraData() {
108109
}
109110

110111
/**
111-
* Check that the type is a special one ('floatj', 'doublej', 'stringj' or 'value').
112+
* Check that the type is a special one ('floatj', 'doublej', 'stringj', 'uint' or 'value').
112113
*
113114
* @return true if the type is a special one
114115
* @see JBBPFieldFloat#TYPE_NAME
115116
* @see JBBPFieldDouble#TYPE_NAME
116117
* @see JBBPFieldString#TYPE_NAME
118+
* @see JBBPFieldUInt#TYPE_NAME
117119
* @since 1.4.0
118120
*/
119121
public boolean isSpecialField() {
120-
return this.typeName.equals(JBBPFieldFloat.TYPE_NAME) ||
121-
this.typeName.equals(JBBPFieldDouble.TYPE_NAME) ||
122-
this.typeName.equals(JBBPFieldString.TYPE_NAME) || this.typeName.equals("val");
122+
return this.typeName.equals(JBBPFieldFloat.TYPE_NAME)
123+
|| this.typeName.equals(JBBPFieldDouble.TYPE_NAME)
124+
|| this.typeName.equals(JBBPFieldString.TYPE_NAME)
125+
|| this.typeName.equals(JBBPFieldUInt.TYPE_NAME)
126+
|| this.typeName.equals("val");
123127
}
124128

125129
@Override

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/tokenizer/JBBPTokenizer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.igormaznitsa.jbbp.model.JBBPFieldDouble;
2323
import com.igormaznitsa.jbbp.model.JBBPFieldFloat;
2424
import com.igormaznitsa.jbbp.model.JBBPFieldString;
25+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
2526
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2627
import java.util.HashSet;
2728
import java.util.Iterator;
@@ -61,6 +62,7 @@ public final class JBBPTokenizer implements Iterable<JBBPToken>, Iterator<JBBPTo
6162
GLOBAL_RESERVED_TYPE_NAMES.add(JBBPFieldFloat.TYPE_NAME);
6263
GLOBAL_RESERVED_TYPE_NAMES.add(JBBPFieldDouble.TYPE_NAME);
6364
GLOBAL_RESERVED_TYPE_NAMES.add(JBBPFieldString.TYPE_NAME);
65+
GLOBAL_RESERVED_TYPE_NAMES.add(JBBPFieldUInt.TYPE_NAME);
6466
GLOBAL_RESERVED_TYPE_NAMES.add("bit");
6567
GLOBAL_RESERVED_TYPE_NAMES.add("bool");
6668
GLOBAL_RESERVED_TYPE_NAMES.add("byte");

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818

1919
import com.igormaznitsa.jbbp.exceptions.JBBPException;
2020
import com.igormaznitsa.jbbp.exceptions.JBBPIllegalArgumentException;
21-
import com.igormaznitsa.jbbp.mapper.*;
21+
import com.igormaznitsa.jbbp.mapper.Bin;
22+
import com.igormaznitsa.jbbp.mapper.BinFieldFilter;
23+
import com.igormaznitsa.jbbp.mapper.BinType;
24+
import com.igormaznitsa.jbbp.mapper.JBBPMapper;
25+
import com.igormaznitsa.jbbp.mapper.MappedFieldRecord;
2226
import com.igormaznitsa.jbbp.model.JBBPFieldInt;
2327
import com.igormaznitsa.jbbp.model.JBBPFieldLong;
2428
import com.igormaznitsa.jbbp.model.JBBPFieldShort;
2529
import com.igormaznitsa.jbbp.model.JBBPFieldString;
30+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
2631
import com.igormaznitsa.jbbp.utils.BinAnnotationWrapper;
2732
import com.igormaznitsa.jbbp.utils.JBBPUtils;
28-
2933
import java.lang.reflect.Array;
3034
import java.lang.reflect.Field;
3135
import java.util.List;
@@ -252,6 +256,15 @@ protected void processObjectField(
252256
this.onFieldInt(obj, field, annotation, value);
253257
}
254258
break;
259+
case UINT: {
260+
long value;
261+
value = ((Number) readFieldValue(obj, fieldRecord)).longValue();
262+
if (reverseBits) {
263+
value = (int) JBBPFieldUInt.reverseBits(value);
264+
}
265+
this.onFieldUInt(obj, field, annotation, (int) value);
266+
}
267+
break;
255268
case FLOAT: {
256269
float value;
257270
if (float.class == fieldType) {
@@ -261,7 +274,7 @@ protected void processObjectField(
261274
}
262275
if (reverseBits) {
263276
value =
264-
Float.intBitsToFloat((int) JBBPFieldInt.reverseBits(Float.floatToIntBits(value)));
277+
Float.intBitsToFloat((int) JBBPFieldInt.reverseBits(Float.floatToIntBits(value)));
265278
}
266279
this.onFieldFloat(obj, field, annotation, value);
267280
}
@@ -433,13 +446,28 @@ protected void processObjectField(
433446
float value = Array.getFloat(array, i);
434447
if (reverseBits) {
435448
value = Float
436-
.intBitsToFloat((int) JBBPFieldInt.reverseBits(Float.floatToIntBits(value)));
449+
.intBitsToFloat((int) JBBPFieldInt.reverseBits(Float.floatToIntBits(value)));
437450
}
438451
this.onFieldFloat(obj, field, annotation, value);
439452
}
440453
this.onArrayEnd(obj, field, annotation);
441454
}
442455
break;
456+
case UINT_ARRAY: {
457+
assertFieldArray(field);
458+
final int len = Array.getLength(array);
459+
this.onArrayStart(obj, field, annotation, len);
460+
for (int i = 0; i < len; i++) {
461+
long value = ((Number) Array.get(array, i)).longValue();
462+
if (reverseBits) {
463+
value = JBBPFieldUInt.reverseBits(value);
464+
}
465+
this.onFieldUInt(obj, field, annotation, (int) value);
466+
}
467+
468+
this.onArrayEnd(obj, field, annotation);
469+
}
470+
break;
443471
case INT_ARRAY: {
444472
assertFieldArray(field);
445473
final int len = Array.getLength(array);
@@ -602,6 +630,20 @@ protected void onFieldInt(final Object obj, final Field field, final Bin annotat
602630

603631
}
604632

633+
/**
634+
* Notification about unsigned integer field.
635+
*
636+
* @param obj the object instance, must not be null
637+
* @param field the field, must not be null
638+
* @param annotation the annotation for field, must not be null
639+
* @param value the value of the field
640+
* @since 2.0.4
641+
*/
642+
protected void onFieldUInt(final Object obj, final Field field, final Bin annotation,
643+
final int value) {
644+
645+
}
646+
605647
/**
606648
* Notification about float field.
607649
*

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.igormaznitsa.jbbp.model.JBBPFieldShort;
2424
import com.igormaznitsa.jbbp.utils.BinAnnotationWrapper;
2525
import com.igormaznitsa.jbbp.utils.JBBPUtils;
26-
2726
import java.io.ByteArrayOutputStream;
2827
import java.io.IOException;
2928
import java.io.OutputStream;
@@ -808,6 +807,20 @@ public JBBPOut Int(final int... value) throws IOException {
808807
return this;
809808
}
810809

810+
@Override
811+
protected void onFieldUInt(final Object obj, final Field field, final Bin annotation,
812+
final int value) {
813+
final JBBPByteOrder old = this.byteOrder;
814+
try {
815+
this.byteOrder = annotation.byteOrder();
816+
this.UInt(value);
817+
} catch (IOException ex) {
818+
throw new JBBPIOException("Can't write unsigned int value", ex);
819+
} finally {
820+
this.byteOrder = old;
821+
}
822+
}
823+
811824
/**
812825
* Write a float value array as integer bits into the stream.
813826
*
@@ -1221,6 +1234,25 @@ protected void onFieldInt(final Object obj, final Field field, final Bin annotat
12211234
}
12221235
}
12231236

1237+
/**
1238+
* Write each long value as unsigned integer one into the session stream.
1239+
*
1240+
* @param value a long value array which values should be written into
1241+
* @return the DSl session
1242+
* @throws IOException it will be thrown for transport errors
1243+
* @since 2.0.4
1244+
*/
1245+
public JBBPOut UInt(final long... value) throws IOException {
1246+
assertNotEnded();
1247+
assertArrayNotNull(value);
1248+
if (this.processCommands) {
1249+
for (final long v : value) {
1250+
_writeInt((int) v);
1251+
}
1252+
}
1253+
return this;
1254+
}
1255+
12241256
@Override
12251257
protected void onFieldShort(final Object obj, final Field field, final Bin annotation,
12261258
final boolean signed, final int value) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.igormaznitsa.jbbp.model.JBBPFieldArrayString;
2929
import com.igormaznitsa.jbbp.model.JBBPFieldArrayStruct;
3030
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUByte;
31+
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUInt;
3132
import com.igormaznitsa.jbbp.model.JBBPFieldArrayUShort;
3233
import com.igormaznitsa.jbbp.model.JBBPFieldBit;
3334
import com.igormaznitsa.jbbp.model.JBBPFieldBoolean;
@@ -40,6 +41,7 @@
4041
import com.igormaznitsa.jbbp.model.JBBPFieldString;
4142
import com.igormaznitsa.jbbp.model.JBBPFieldStruct;
4243
import com.igormaznitsa.jbbp.model.JBBPFieldUByte;
44+
import com.igormaznitsa.jbbp.model.JBBPFieldUInt;
4345
import com.igormaznitsa.jbbp.model.JBBPFieldUShort;
4446

4547
/**
@@ -83,6 +85,10 @@ public enum BinType {
8385
* A Mapping field will be mapped to a parsed integer field.
8486
*/
8587
INT(JBBPFieldInt.class, false),
88+
/**
89+
* A Mapping field will be mapped to a parsed unsigned integer field.
90+
*/
91+
UINT(JBBPFieldUInt.class, false),
8692
/**
8793
* A Mapping field will be mapped to a parsed double field.
8894
*
@@ -133,6 +139,10 @@ public enum BinType {
133139
* A Mapping field will be mapped to a parsed integer array field.
134140
*/
135141
INT_ARRAY(JBBPFieldArrayInt.class, true),
142+
/**
143+
* A Mapping field will be mapped to a parsed unsigned integer array field.
144+
*/
145+
UINT_ARRAY(JBBPFieldArrayUInt.class, true),
136146
/**
137147
* A Mapping field will be mapped to a parsed long array field.
138148
*/

0 commit comments

Comments
 (0)