@@ -2182,14 +2182,16 @@ public static CBORObject Read(InputStream stream) {
2182
2182
2183
2183
private static void WriteObjectArray (
2184
2184
List <CBORObject > list ,
2185
- OutputStream outputStream ) throws java .io .IOException {
2186
- WriteObjectArray (list , outputStream , null );
2185
+ OutputStream outputStream ,
2186
+ CBOREncodeOptions options ) throws java .io .IOException {
2187
+ WriteObjectArray (list , outputStream , null , options );
2187
2188
}
2188
2189
2189
2190
private static void WriteObjectMap (
2190
2191
Map <CBORObject , CBORObject > map ,
2191
- OutputStream outputStream ) throws java .io .IOException {
2192
- WriteObjectMap (map , outputStream , null );
2192
+ OutputStream outputStream ,
2193
+ CBOREncodeOptions options ) throws java .io .IOException {
2194
+ WriteObjectMap (map , outputStream , null , options );
2193
2195
}
2194
2196
2195
2197
private static List <Object > PushObject (
@@ -2213,20 +2215,21 @@ private static List<Object> WriteChildObject(
2213
2215
Object parentThisItem ,
2214
2216
CBORObject child ,
2215
2217
OutputStream outputStream ,
2216
- List <Object > stack ) throws java .io .IOException {
2218
+ List <Object > stack ,
2219
+ CBOREncodeOptions options ) throws java .io .IOException {
2217
2220
if (child == null ) {
2218
2221
outputStream .write (0xf6 );
2219
2222
} else {
2220
2223
int type = child .getItemType ();
2221
2224
if (type == CBORObjectTypeArray ) {
2222
2225
stack = PushObject (stack , parentThisItem , child .getThisItem ());
2223
2226
child .WriteTags (outputStream );
2224
- WriteObjectArray (child .AsList (), outputStream , stack );
2227
+ WriteObjectArray (child .AsList (), outputStream , stack , options );
2225
2228
stack .remove (stack .size () - 1 );
2226
2229
} else if (type == CBORObjectTypeMap ) {
2227
2230
stack = PushObject (stack , parentThisItem , child .getThisItem ());
2228
2231
child .WriteTags (outputStream );
2229
- WriteObjectMap (child .AsMap (), outputStream , stack );
2232
+ WriteObjectMap (child .AsMap (), outputStream , stack , options );
2230
2233
stack .remove (stack .size () - 1 );
2231
2234
} else {
2232
2235
child .WriteTo (outputStream );
@@ -2238,25 +2241,37 @@ private static List<Object> WriteChildObject(
2238
2241
private static void WriteObjectArray (
2239
2242
List <CBORObject > list ,
2240
2243
OutputStream outputStream ,
2241
- List <Object > stack ) throws java .io .IOException {
2244
+ List <Object > stack ,
2245
+ CBOREncodeOptions options ) throws java .io .IOException {
2242
2246
Object thisObj = list ;
2243
2247
WritePositiveInt (4 , list .size (), outputStream );
2244
2248
for (CBORObject i : list ) {
2245
- stack = WriteChildObject (thisObj , i , outputStream , stack );
2249
+ stack = WriteChildObject (thisObj , i , outputStream , stack , options );
2246
2250
}
2247
2251
}
2248
2252
2249
2253
private static void WriteObjectMap (
2250
2254
Map <CBORObject , CBORObject > map ,
2251
2255
OutputStream outputStream ,
2252
- List <Object > stack ) throws java .io .IOException {
2256
+ List <Object > stack ,
2257
+ CBOREncodeOptions options ) throws java .io .IOException {
2253
2258
Object thisObj = map ;
2254
2259
WritePositiveInt (5 , map .size (), outputStream );
2255
2260
for (Map .Entry <CBORObject , CBORObject > entry : map .entrySet ()) {
2256
2261
CBORObject key = entry .getKey ();
2257
2262
CBORObject value = entry .getValue ();
2258
- stack = WriteChildObject (thisObj , key , outputStream , stack );
2259
- stack = WriteChildObject (thisObj , value , outputStream , stack );
2263
+ stack = WriteChildObject (
2264
+ thisObj ,
2265
+ key ,
2266
+ outputStream ,
2267
+ stack ,
2268
+ options );
2269
+ stack = WriteChildObject (
2270
+ thisObj ,
2271
+ value ,
2272
+ outputStream ,
2273
+ stack ,
2274
+ options );
2260
2275
}
2261
2276
}
2262
2277
@@ -2434,6 +2449,38 @@ public static void Write(String str, OutputStream stream) throws java.io.IOExcep
2434
2449
}
2435
2450
}
2436
2451
2452
+ /**
2453
+ * Writes a string in CBOR format to a data stream.
2454
+ * @param str The string to write. Can be null.
2455
+ * @param stream A writable data stream.
2456
+ * @param options Options for encoding the data to CBOR.
2457
+ * @throws NullPointerException The parameter {@code stream} is null.
2458
+ * @throws java.io.IOException An I/O error occurred.
2459
+ */
2460
+ public static void Write (
2461
+ String str ,
2462
+ OutputStream stream ,
2463
+ CBOREncodeOptions options ) throws java .io .IOException {
2464
+ if (stream == null ) {
2465
+ throw new NullPointerException ("stream" );
2466
+ }
2467
+ if (str == null ) {
2468
+ stream .write (0xf6 ); // Write null instead of String
2469
+ } else {
2470
+ CBOREncodeOptions noIndef =
2471
+ options .And (CBOREncodeOptions .NoIndefLengthStrings );
2472
+ if (noIndef .getValue () != 0 ) {
2473
+ // NOTE: Length of a String Object won't be higher than the maximum
2474
+ // allowed for definite-length strings
2475
+ long codePointLength = DataUtilities .GetUtf8Length (str , true );
2476
+ WritePositiveInt64 (3 , codePointLength , stream );
2477
+ DataUtilities .WriteUtf8 (str , stream , true );
2478
+ } else {
2479
+ WriteStreamedString (str , stream );
2480
+ }
2481
+ }
2482
+ }
2483
+
2437
2484
/**
2438
2485
* Writes a binary floating-point number in CBOR format to a data stream as
2439
2486
* follows: <ul><li>If the value is null, writes the byte 0xF6.</li>
@@ -2660,6 +2707,17 @@ public static void Write(BigInteger bigint, OutputStream stream) throws java.io.
2660
2707
* @throws java.io.IOException An I/O error occurred.
2661
2708
*/
2662
2709
public void WriteTo (OutputStream stream ) throws java .io .IOException {
2710
+ this .WriteTo (stream , CBOREncodeOptions .None );
2711
+ }
2712
+
2713
+ /**
2714
+ * Writes this CBOR object to a data stream.
2715
+ * @param stream A writable data stream.
2716
+ * @param options Options for encoding the data to CBOR.
2717
+ * @throws NullPointerException The parameter {@code stream} is null.
2718
+ * @throws java.io.IOException An I/O error occurred.
2719
+ */
2720
+ public void WriteTo (OutputStream stream , CBOREncodeOptions options ) throws java .io .IOException {
2663
2721
if (stream == null ) {
2664
2722
throw new NullPointerException ("stream" );
2665
2723
}
@@ -2677,9 +2735,9 @@ public void WriteTo(OutputStream stream) throws java.io.IOException {
2677
2735
stream );
2678
2736
stream .write (arr , 0 , arr .length );
2679
2737
} else if (type == CBORObjectTypeTextString ) {
2680
- Write ((String )this .getThisItem (), stream );
2738
+ Write ((String )this .getThisItem (), stream , options );
2681
2739
} else if (type == CBORObjectTypeArray ) {
2682
- WriteObjectArray (this .AsList (), stream );
2740
+ WriteObjectArray (this .AsList (), stream , options );
2683
2741
} else if (type == CBORObjectTypeExtendedDecimal ) {
2684
2742
ExtendedDecimal dec = (ExtendedDecimal )this .getThisItem ();
2685
2743
Write (dec , stream );
@@ -2690,7 +2748,7 @@ public void WriteTo(OutputStream stream) throws java.io.IOException {
2690
2748
ExtendedRational flo = (ExtendedRational )this .getThisItem ();
2691
2749
Write (flo , stream );
2692
2750
} else if (type == CBORObjectTypeMap ) {
2693
- WriteObjectMap (this .AsMap (), stream );
2751
+ WriteObjectMap (this .AsMap (), stream , options );
2694
2752
} else if (type == CBORObjectTypeSimpleValue ) {
2695
2753
int value = ((Integer )this .getThisItem ()).intValue ();
2696
2754
if (value < 24 ) {
@@ -2911,6 +2969,15 @@ private static byte[] GetOptimizedBytesIfShortAscii(
2911
2969
* @return A byte array in CBOR format.
2912
2970
*/
2913
2971
public byte [] EncodeToBytes () {
2972
+ return this .EncodeToBytes (CBOREncodeOptions .None );
2973
+ }
2974
+
2975
+ /**
2976
+ * Gets the binary representation of this data item.
2977
+ * @param options Options for encoding the data to CBOR.
2978
+ * @return A byte array in CBOR format.
2979
+ */
2980
+ public byte [] EncodeToBytes (CBOREncodeOptions options ) {
2914
2981
// For some types, a memory stream is a lot of
2915
2982
// overhead since the amount of memory the types
2916
2983
// use is fixed and small
@@ -3014,7 +3081,7 @@ public byte[] EncodeToBytes() {
3014
3081
try {
3015
3082
ms = new java .io .ByteArrayOutputStream (16 );
3016
3083
3017
- this .WriteTo (ms );
3084
+ this .WriteTo (ms , options );
3018
3085
return ms .toByteArray ();
3019
3086
}
3020
3087
finally {
@@ -3042,18 +3109,31 @@ public static void Write(CBORObject value, OutputStream stream) throws java.io.I
3042
3109
}
3043
3110
}
3044
3111
3112
+ /**
3113
+ * Not documented yet.
3114
+ * @param objValue An arbitrary object.
3115
+ * @param stream A writable data stream.
3116
+ */
3117
+ public static void Write (Object objValue , OutputStream stream ) throws java .io .IOException {
3118
+ Write (objValue , stream , CBOREncodeOptions .None );
3119
+ }
3120
+
3045
3121
/**
3046
3122
* Writes an arbitrary object to a CBOR data stream. Currently, the following
3047
3123
* objects are supported: <ul><li>Lists of CBORObject.</li> <li>Maps of
3048
3124
* CBORObject.</li> <li>Null.</li> <li>Any object accepted by the
3049
3125
* FromObject static methods.</li> </ul>
3050
3126
* @param objValue The value to write.
3051
3127
* @param stream A writable data stream.
3128
+ * @param options Options for encoding the data to CBOR.
3052
3129
* @throws IllegalArgumentException The object's type is not supported.
3053
3130
* @throws NullPointerException The parameter {@code stream} is null.
3054
3131
*/
3055
3132
@ SuppressWarnings ("unchecked" )
3056
- public static void Write (Object objValue , OutputStream stream ) throws java .io .IOException {
3133
+ public static void Write (
3134
+ Object objValue ,
3135
+ OutputStream stream ,
3136
+ CBOREncodeOptions options ) throws java .io .IOException {
3057
3137
if (stream == null ) {
3058
3138
throw new NullPointerException ("stream" );
3059
3139
}
@@ -3068,11 +3148,17 @@ public static void Write(Object objValue, OutputStream stream) throws java.io.IO
3068
3148
return ;
3069
3149
}
3070
3150
if (objValue instanceof List <?>) {
3071
- WriteObjectArray ((List <CBORObject >)objValue , stream );
3151
+ WriteObjectArray (
3152
+ (List <CBORObject >)objValue ,
3153
+ stream ,
3154
+ options );
3072
3155
return ;
3073
3156
}
3074
3157
if (objValue instanceof Map <?, ?>) {
3075
- WriteObjectMap ((Map <CBORObject , CBORObject >)objValue , stream );
3158
+ WriteObjectMap (
3159
+ (Map <CBORObject , CBORObject >)objValue ,
3160
+ stream ,
3161
+ options );
3076
3162
return ;
3077
3163
}
3078
3164
FromObject (objValue ).WriteTo (stream );
0 commit comments