summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/raw/DataEncoder.java
blob: 990b7e7b1377d55567f7061ad890318a6396dd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
 * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado.raw;

import java.math.BigDecimal;
import java.math.BigInteger;

import java.io.IOException;
import java.io.OutputStream;

import static com.amazon.carbonado.raw.EncodingConstants.*;

/**
 * A very low-level class that supports encoding of primitive data. For
 * encoding data into keys, see {@link KeyEncoder}.
 *
 * @author Brian S O'Neill
 * @see DataDecoder
 */
public class DataEncoder {
    // Note: Most of these methods are also used by KeyEncoder, which is why
    // they are encoded for supporting proper ordering.

    /**
     * Encodes the given signed integer into exactly 4 bytes.
     *
     * @param value signed integer value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(int value, byte[] dst, int dstOffset) {
        value ^= 0x80000000;
        dst[dstOffset    ] = (byte)(value >> 24);
        dst[dstOffset + 1] = (byte)(value >> 16);
        dst[dstOffset + 2] = (byte)(value >> 8);
        dst[dstOffset + 3] = (byte)value;
    }

    /**
     * Encodes the given signed Integer object into exactly 1 or 5 bytes. If
     * the Integer object is never expected to be null, consider encoding as an
     * int primitive.
     *
     * @param value optional signed Integer value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(Integer value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        } else {
            dst[dstOffset] = NOT_NULL_BYTE_HIGH;
            encode(value.intValue(), dst, dstOffset + 1);
            return 5;
        }
    }

    /**
     * Encodes the given signed long into exactly 8 bytes.
     *
     * @param value signed long value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(long value, byte[] dst, int dstOffset) {
        int w = ((int)(value >> 32)) ^ 0x80000000;
        dst[dstOffset    ] = (byte)(w >> 24);
        dst[dstOffset + 1] = (byte)(w >> 16);
        dst[dstOffset + 2] = (byte)(w >> 8);
        dst[dstOffset + 3] = (byte)w;
        w = (int)value;
        dst[dstOffset + 4] = (byte)(w >> 24);
        dst[dstOffset + 5] = (byte)(w >> 16);
        dst[dstOffset + 6] = (byte)(w >> 8);
        dst[dstOffset + 7] = (byte)w;
    }

    /**
     * Encodes the given signed Long object into exactly 1 or 9 bytes. If the
     * Long object is never expected to be null, consider encoding as a long
     * primitive.
     *
     * @param value optional signed Long value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(Long value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        } else {
            dst[dstOffset] = NOT_NULL_BYTE_HIGH;
            encode(value.longValue(), dst, dstOffset + 1);
            return 9;
        }
    }

    /**
     * Encodes the given signed byte into exactly 1 byte.
     *
     * @param value signed byte value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(byte value, byte[] dst, int dstOffset) {
        dst[dstOffset] = (byte)(value ^ 0x80);
    }

    /**
     * Encodes the given signed Byte object into exactly 1 or 2 bytes. If the
     * Byte object is never expected to be null, consider encoding as a byte
     * primitive.
     *
     * @param value optional signed Byte value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(Byte value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        } else {
            dst[dstOffset] = NOT_NULL_BYTE_HIGH;
            dst[dstOffset + 1] = (byte)(value ^ 0x80);
            return 2;
        }
    }

    /**
     * Encodes the given signed short into exactly 2 bytes.
     *
     * @param value signed short value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(short value, byte[] dst, int dstOffset) {
        value ^= 0x8000;
        dst[dstOffset    ] = (byte)(value >> 8);
        dst[dstOffset + 1] = (byte)value;
    }

    /**
     * Encodes the given signed Short object into exactly 1 or 3 bytes. If the
     * Short object is never expected to be null, consider encoding as a short
     * primitive.
     *
     * @param value optional signed Short value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(Short value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        } else {
            dst[dstOffset] = NOT_NULL_BYTE_HIGH;
            encode(value.shortValue(), dst, dstOffset + 1);
            return 3;
        }
    }

    /**
     * Encodes the given character into exactly 2 bytes.
     *
     * @param value character value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(char value, byte[] dst, int dstOffset) {
        dst[dstOffset    ] = (byte)(value >> 8);
        dst[dstOffset + 1] = (byte)value;
    }

    /**
     * Encodes the given Character object into exactly 1 or 3 bytes. If the
     * Character object is never expected to be null, consider encoding as a
     * char primitive.
     *
     * @param value optional Character value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(Character value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        } else {
            dst[dstOffset] = NOT_NULL_BYTE_HIGH;
            encode(value.charValue(), dst, dstOffset + 1);
            return 3;
        }
    }

    /**
     * Encodes the given boolean into exactly 1 byte.
     *
     * @param value boolean value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(boolean value, byte[] dst, int dstOffset) {
        dst[dstOffset] = value ? (byte)128 : (byte)127;
    }

    /**
     * Encodes the given Boolean object into exactly 1 byte.
     *
     * @param value optional Boolean value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(Boolean value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
        } else {
            dst[dstOffset] = value.booleanValue() ? (byte)128 : (byte)127;
        }
    }

    /**
     * Encodes the given float into exactly 4 bytes.
     *
     * @param value float value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(float value, byte[] dst, int dstOffset) {
        int bits = Float.floatToIntBits(value);
        bits ^= (bits < 0) ? 0xffffffff : 0x80000000;
        dst[dstOffset    ] = (byte)(bits >> 24);
        dst[dstOffset + 1] = (byte)(bits >> 16);
        dst[dstOffset + 2] = (byte)(bits >> 8);
        dst[dstOffset + 3] = (byte)bits;
    }

    /**
     * Encodes the given Float object into exactly 4 bytes. A non-canonical NaN
     * value is used to represent null.
     *
     * @param value optional Float value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(Float value, byte[] dst, int dstOffset) {
        if (value == null) {
            encode(0x7fffffff, dst, dstOffset);
        } else {
            encode(value.floatValue(), dst, dstOffset);
        }
    }

    /**
     * Encodes the given double into exactly 8 bytes.
     *
     * @param value double value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(double value, byte[] dst, int dstOffset) {
        long bits = Double.doubleToLongBits(value);
        bits ^= (bits < 0) ? 0xffffffffffffffffL : 0x8000000000000000L;
        int w = (int)(bits >> 32);
        dst[dstOffset    ] = (byte)(w >> 24);
        dst[dstOffset + 1] = (byte)(w >> 16);
        dst[dstOffset + 2] = (byte)(w >> 8);
        dst[dstOffset + 3] = (byte)w;
        w = (int)bits;
        dst[dstOffset + 4] = (byte)(w >> 24);
        dst[dstOffset + 5] = (byte)(w >> 16);
        dst[dstOffset + 6] = (byte)(w >> 8);
        dst[dstOffset + 7] = (byte)w;
    }

    /**
     * Encodes the given Double object into exactly 8 bytes. A non-canonical
     * NaN value is used to represent null.
     *
     * @param value optional Double value to encode
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     */
    public static void encode(Double value, byte[] dst, int dstOffset) {
        if (value == null) {
            encode(0x7fffffffffffffffL, dst, dstOffset);
        } else {
            encode(value.doubleValue(), dst, dstOffset);
        }
    }

    /**
     * Encodes the given optional BigInteger into a variable amount of
     * bytes. If the BigInteger is null, exactly 1 byte is written. Otherwise,
     * the amount written can be determined by calling calculateEncodedLength.
     *
     * @param value BigInteger value to encode, may be null
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     * @since 1.2
     */
    public static int encode(BigInteger value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        }

        byte[] bytes = value.toByteArray();

        // Write the byte array length first, in a variable amount of bytes.
        int amt = encodeUnsignedVarInt(bytes.length, dst, dstOffset);

        // Now write the byte array.
        System.arraycopy(bytes, 0, dst, dstOffset + amt, bytes.length);

        return amt + bytes.length;
    }

    /**
     * Returns the amount of bytes required to encode the given BigInteger.
     *
     * @param value BigInteger value to encode, may be null
     * @return amount of bytes needed to encode
     * @since 1.2
     */
    public static int calculateEncodedLength(BigInteger value) {
        if (value == null) {
            return 1;
        }
        int byteCount = (value.bitLength() >> 3) + 1;
        return unsignedVarIntLength(byteCount) + byteCount;
    }

    /**
     * Encodes the given optional BigDecimal into a variable amount of
     * bytes. If the BigDecimal is null, exactly 1 byte is written. Otherwise,
     * the amount written can be determined by calling calculateEncodedLength.
     *
     * @param value BigDecimal value to encode, may be null
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     * @since 1.2
     * /
    public static int encode(BigDecimal value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        }
        int amt = encodeSignedVarInt(value.scale(), dst, dstOffset);
        return amt + encode(value.unscaledValue(), dst, dstOffset + amt);
    }

    /**
     * Returns the amount of bytes required to encode the given BigDecimal.
     *
     * @param value BigDecimal value to encode, may be null
     * @return amount of bytes needed to encode
     * @since 1.2
     * /
    public static int calculateEncodedLength(BigDecimal value) {
        if (value == null) {
            return 1;
        }
        return signedVarIntLength(value.scale()) + calculateEncodedLength(value.unscaledValue());
    }
    */

    /**
     * Encodes the given optional byte array into a variable amount of
     * bytes. If the byte array is null, exactly 1 byte is written. Otherwise,
     * the amount written can be determined by calling calculateEncodedLength.
     *
     * @param value byte array value to encode, may be null
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(byte[] value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        }
        return encode(value, 0, value.length, dst, dstOffset);
    }

    /**
     * Encodes the given optional byte array into a variable amount of
     * bytes. If the byte array is null, exactly 1 byte is written. Otherwise,
     * the amount written can be determined by calling calculateEncodedLength.
     *
     * @param value byte array value to encode, may be null
     * @param valueOffset offset into byte array
     * @param valueLength length of data in byte array
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(byte[] value, int valueOffset, int valueLength,
                             byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        }

        // Write the value length first, in a variable amount of bytes.
        int amt = encodeUnsignedVarInt(valueLength, dst, dstOffset);

        // Now write the value.
        System.arraycopy(value, valueOffset, dst, dstOffset + amt, valueLength);

        return amt + valueLength;
    }

    /**
     * Returns the amount of bytes required to encode the given byte array.
     *
     * @param value byte array value to encode, may be null
     * @return amount of bytes needed to encode
     */
    public static int calculateEncodedLength(byte[] value) {
        return value == null ? 1 : calculateEncodedLength(value, 0, value.length);
    }

    /**
     * Returns the amount of bytes required to encode the given byte array.
     *
     * @param value byte array value to encode, may be null
     * @param valueOffset offset into byte array
     * @param valueLength length of data in byte array
     * @return amount of bytes needed to encode
     */
    public static int calculateEncodedLength(byte[] value, int valueOffset, int valueLength) {
        return value == null ? 1 : (unsignedVarIntLength(valueLength) + valueLength);
    }

    /**
     * Encodes the given optional String into a variable amount of bytes. The
     * amount written can be determined by calling
     * calculateEncodedStringLength.
     * <p>
     * Strings are encoded in a fashion similar to UTF-8, in that ASCII
     * characters are written in one byte. This encoding is more efficient than
     * UTF-8, but it isn't compatible with UTF-8.
     *
     * @param value String value to encode, may be null
     * @param dst destination for encoded bytes
     * @param dstOffset offset into destination array
     * @return amount of bytes written
     */
    public static int encode(String value, byte[] dst, int dstOffset) {
        if (value == null) {
            dst[dstOffset] = NULL_BYTE_HIGH;
            return 1;
        }
        final int originalOffset = dstOffset;

        int valueLength = value.length();

        // Write the value length first, in a variable amount of bytes.
        dstOffset += encodeUnsignedVarInt(valueLength, dst, dstOffset);

        for (int i = 0; i < valueLength; i++) {
            int c = value.charAt(i);
            if (c <= 0x7f) {
                dst[dstOffset++] = (byte)c;
            } else if (c <= 0x3fff) {
                dst[dstOffset++] = (byte)(0x80 | (c >> 8));
                dst[dstOffset++] = (byte)(c & 0xff);
            } else {
                if (c >= 0xd800 && c <= 0xdbff) {
                    // Found a high surrogate. Verify that surrogate pair is
                    // well-formed. Low surrogate must follow high surrogate.
                    if (i + 1 < valueLength) {
                        int c2 = value.charAt(i + 1);
                        if (c2 >= 0xdc00 && c2 <= 0xdfff) {
                            c = 0x10000 + (((c & 0x3ff) << 10) | (c2 & 0x3ff));
                            i++;
                        }
                    }
                }
                dst[dstOffset++] = (byte)(0xc0 | (c >> 16));
                dst[dstOffset++] = (byte)((c >> 8) & 0xff);
                dst[dstOffset++] = (byte)(c & 0xff);
            }
        }

        return dstOffset - originalOffset;
    }

    /**
     * Returns the amount of bytes required to encode the given String.
     *
     * @param value String to encode, may be null
     */
    public static int calculateEncodedStringLength(String value) {
        if (value == null) {
            return 1;
        }

        int valueLength = value.length();
        int encodedLen = unsignedVarIntLength(valueLength);

        for (int i = 0; i < valueLength; i++) {
            int c = value.charAt(i);
            if (c <= 0x7f) {
                encodedLen++;
            } else if (c <= 0x3fff) {
                encodedLen += 2;
            } else {
                if (c >= 0xd800 && c <= 0xdbff) {
                    // Found a high surrogate. Verify that surrogate pair is
                    // well-formed. Low surrogate must follow high surrogate.
                    if (i + 1 < valueLength) {
                        int c2 = value.charAt(i + 1);
                        if (c2 >= 0xdc00 && c2 <= 0xdfff) {
                            i++;
                        }
                    }
                }
                encodedLen += 3;
            }
        }

        return encodedLen;
    }

    private static int encodeUnsignedVarInt(int value, byte[] dst, int dstOffset) {
        if (value < 128) {
            dst[dstOffset] = (byte)value;
            return 1;
        } else if (value < 16384) {
            dst[dstOffset++] = (byte)((value >> 8) | 0x80);
            dst[dstOffset] = (byte)value;
            return 2;
        } else if (value < 2097152) {
            dst[dstOffset++] = (byte)((value >> 16) | 0xc0);
            dst[dstOffset++] = (byte)(value >> 8);
            dst[dstOffset] = (byte)value;
            return 3;
        } else if (value < 268435456) {
            dst[dstOffset++] = (byte)((value >> 24) | 0xe0);
            dst[dstOffset++] = (byte)(value >> 16);
            dst[dstOffset++] = (byte)(value >> 8);
            dst[dstOffset] = (byte)value;
            return 4;
        } else {
            dst[dstOffset++] = (byte)0xf0;
            dst[dstOffset++] = (byte)(value >> 24);
            dst[dstOffset++] = (byte)(value >> 16);
            dst[dstOffset++] = (byte)(value >> 8);
            dst[dstOffset] = (byte)value;
            return 5;
        }
    }

    private static int unsignedVarIntLength(int value) {
        if (value < 128) {
            return 1;
        } else if (value < 16384) {
            return 2;
        } else if (value < 2097152) {
            return 3;
        } else if (value < 268435456) {
            return 4;
        } else {
            return 5;
        }
    }

    private static int encodeSignedVarInt(int value, byte[] dst, int dstOffset) {
        value = (value < 0 ? (((~value) << 1) | 1) : (value << 1));
        if (value < 0) {
            dst[dstOffset++] = (byte)0xf0;
            dst[dstOffset++] = (byte)(value >> 24);
            dst[dstOffset++] = (byte)(value >> 16);
            dst[dstOffset++] = (byte)(value >> 8);
            dst[dstOffset] = (byte)value;
            return 5;
        } else {
            return encodeUnsignedVarInt(value, dst, dstOffset);
        }
    }

    private static int signedVarIntLength(int value) {
        value = (value < 0 ? ~value : value) << 1;
        return value < 0 ? 5 : unsignedVarIntLength(value);
    }

    /**
     * Writes a positive length value in up to five bytes.
     *
     * @return number of bytes written
     * @since 1.2
     */
    public static int writeLength(int valueLength, OutputStream out) throws IOException {
        if (valueLength < 128) {
            out.write(valueLength);
            return 1;
        } else if (valueLength < 16384) {
            out.write((valueLength >> 8) | 0x80);
            out.write(valueLength);
            return 2;
        } else if (valueLength < 2097152) {
            out.write((valueLength >> 16) | 0xc0);
            out.write(valueLength >> 8);
            out.write(valueLength);
            return 3;
        } else if (valueLength < 268435456) {
            out.write((valueLength >> 24) | 0xe0);
            out.write(valueLength >> 16);
            out.write(valueLength >> 8);
            out.write(valueLength);
            return 4;
        } else {
            out.write(0xf0);
            out.write(valueLength >> 24);
            out.write(valueLength >> 16);
            out.write(valueLength >> 8);
            out.write(valueLength);
            return 5;
        }
    }

    /**
     * Encodes the given byte array for use when there is only a single
     * property, whose type is a byte array. The original byte array is
     * returned if the padding lengths are zero.
     *
     * @param prefixPadding amount of extra bytes to allocate at start of encoded byte array
     * @param suffixPadding amount of extra bytes to allocate at end of encoded byte array
     */
    public static byte[] encodeSingle(byte[] value, int prefixPadding, int suffixPadding) {
        if (prefixPadding <= 0 && suffixPadding <= 0) {
            return value;
        }
        int length = value.length;
        byte[] dst = new byte[prefixPadding + length + suffixPadding];
        System.arraycopy(value, 0, dst, prefixPadding, length);
        return dst;
    }

    /**
     * Encodes the given byte array for use when there is only a single
     * nullable property, whose type is a byte array.
     */
    public static byte[] encodeSingleNullable(byte[] value) {
        return encodeSingleNullable(value, 0, 0);
    }

    /**
     * Encodes the given byte array for use when there is only a single
     * nullable property, whose type is a byte array.
     *
     * @param prefixPadding amount of extra bytes to allocate at start of encoded byte array
     * @param suffixPadding amount of extra bytes to allocate at end of encoded byte array
     */
    public static byte[] encodeSingleNullable(byte[] value, int prefixPadding, int suffixPadding) {
        if (prefixPadding <= 0 && suffixPadding <= 0) {
            if (value == null) {
                return new byte[] {NULL_BYTE_HIGH};
            }

            int length = value.length;
            if (length == 0) {
                return new byte[] {NOT_NULL_BYTE_HIGH};
            }

            byte[] dst = new byte[1 + length];
            dst[0] = NOT_NULL_BYTE_HIGH;
            System.arraycopy(value, 0, dst, 1, length);
            return dst;
        }

        if (value == null) {
            byte[] dst = new byte[prefixPadding + 1 + suffixPadding];
            dst[prefixPadding] = NULL_BYTE_HIGH;
            return dst;
        }

        int length = value.length;
        byte[] dst = new byte[prefixPadding + 1 + length + suffixPadding];
        dst[prefixPadding] = NOT_NULL_BYTE_HIGH;
        System.arraycopy(value, 0, dst, prefixPadding + 1, length);
        return dst;
    }
}