Skip to content

Commit 1121cbf

Browse files
committed
Add unsigned number methods to CBORGenerator
1 parent 37c10ae commit 1121cbf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORGenerator.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,20 @@ public void writeNull() throws IOException {
11421142
_writeByte(BYTE_NULL);
11431143
}
11441144

1145+
/**
1146+
* Method for outputting given value as an unsigned integer.
1147+
* Can be called in any context where a value is expected
1148+
* (Array value, Object field value, root-level value).
1149+
*
1150+
* @param i Number value to write
1151+
* @throws IOException if there is either an underlying I/O problem or encoding
1152+
* issue at format layer
1153+
*/
1154+
public void writeNumberUnsigned(int i) throws IOException {
1155+
_verifyValueWrite("write number unsigned");
1156+
_writeIntMinimal(PREFIX_TYPE_INT_POS, i);
1157+
}
1158+
11451159
@Override
11461160
public void writeNumber(int i) throws IOException {
11471161
_verifyValueWrite("write number");
@@ -1183,6 +1197,33 @@ public void writeNumber(int i) throws IOException {
11831197
_outputBuffer[_outputTail++] = b0;
11841198
}
11851199

1200+
/**
1201+
* Method for outputting given value as an unsigned integer.
1202+
* Can be called in any context where a value is expected
1203+
* (Array value, Object field value, root-level value).
1204+
*
1205+
* @param l Number value to write
1206+
* @throws IOException if there is either an underlying I/O problem or encoding
1207+
* issue at format layer
1208+
*/
1209+
public void writeNumberUnsigned(long l) throws IOException {
1210+
if (_cfgMinimalInts && l >= 0 && l < 0x100000000L) {
1211+
writeNumberUnsigned((int) l);
1212+
return;
1213+
}
1214+
_verifyValueWrite("write number unsigned");
1215+
_ensureRoomForOutput(9);
1216+
_outputBuffer[_outputTail++] = (byte) (PREFIX_TYPE_INT_POS + SUFFIX_UINT64_ELEMENTS);
1217+
_outputBuffer[_outputTail++] = (byte) (l >> 56);
1218+
_outputBuffer[_outputTail++] = (byte) (l >> 48);
1219+
_outputBuffer[_outputTail++] = (byte) (l >> 40);
1220+
_outputBuffer[_outputTail++] = (byte) (l >> 32);
1221+
_outputBuffer[_outputTail++] = (byte) (l >> 24);
1222+
_outputBuffer[_outputTail++] = (byte) (l >> 16);
1223+
_outputBuffer[_outputTail++] = (byte) (l >> 8);
1224+
_outputBuffer[_outputTail++] = (byte) l;
1225+
}
1226+
11861227
@Override
11871228
public void writeNumber(long l) throws IOException {
11881229
_verifyValueWrite("write number");

0 commit comments

Comments
 (0)