aboutsummaryrefslogtreecommitdiffhomepage
path: root/javanano/src/main/java/com/google/protobuf/nano/Extension.java
blob: c458f9b1fc80fd28117b55de11ea5e3df35a09ef (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
// Protocol Buffers - Google's data interchange format
// Copyright 2013 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package com.google.protobuf.nano;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents an extension.
 *
 * @author bduff@google.com (Brian Duff)
 * @author maxtroy@google.com (Max Cai)
 * @param <M> the type of the extendable message this extension is for.
 * @param <T> the Java type of the extension; see {@link #clazz}.
 */
public class Extension<M extends ExtendableMessageNano<M>, T> {

    /*
     * Because we typically only define message-typed extensions, the Extension class hierarchy is
     * designed as follows, to allow a big amount of code in this file to be removed by ProGuard:
     *
     *            Extension          // ready to use for message/group typed extensions
     *                Δ
     *                |
     *       PrimitiveExtension      // for primitive/enum typed extensions
     */

    public static final int TYPE_DOUBLE   = InternalNano.TYPE_DOUBLE;
    public static final int TYPE_FLOAT    = InternalNano.TYPE_FLOAT;
    public static final int TYPE_INT64    = InternalNano.TYPE_INT64;
    public static final int TYPE_UINT64   = InternalNano.TYPE_UINT64;
    public static final int TYPE_INT32    = InternalNano.TYPE_INT32;
    public static final int TYPE_FIXED64  = InternalNano.TYPE_FIXED64;
    public static final int TYPE_FIXED32  = InternalNano.TYPE_FIXED32;
    public static final int TYPE_BOOL     = InternalNano.TYPE_BOOL;
    public static final int TYPE_STRING   = InternalNano.TYPE_STRING;
    public static final int TYPE_GROUP    = InternalNano.TYPE_GROUP;
    public static final int TYPE_MESSAGE  = InternalNano.TYPE_MESSAGE;
    public static final int TYPE_BYTES    = InternalNano.TYPE_BYTES;
    public static final int TYPE_UINT32   = InternalNano.TYPE_UINT32;
    public static final int TYPE_ENUM     = InternalNano.TYPE_ENUM;
    public static final int TYPE_SFIXED32 = InternalNano.TYPE_SFIXED32;
    public static final int TYPE_SFIXED64 = InternalNano.TYPE_SFIXED64;
    public static final int TYPE_SINT32   = InternalNano.TYPE_SINT32;
    public static final int TYPE_SINT64   = InternalNano.TYPE_SINT64;

    /**
     * Creates an {@code Extension} of the given message type and tag number.
     * Should be used by the generated code only.
     *
     * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
     * @deprecated use {@link #createMessageTyped(int, Class, long)} instead.
     */
    @Deprecated
    public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
            Extension<M, T> createMessageTyped(int type, Class<T> clazz, int tag) {
        return new Extension<M, T>(type, clazz, tag, false);
    }

    // Note: these create...() methods take a long for the tag parameter,
    // because tags are represented as unsigned ints, and these values exist
    // in generated code as long values. However, they can fit in 32-bits, so
    // it's safe to cast them to int without loss of precision.

    /**
     * Creates an {@code Extension} of the given message type and tag number.
     * Should be used by the generated code only.
     *
     * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
     */
    public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
            Extension<M, T> createMessageTyped(int type, Class<T> clazz, long tag) {
        return new Extension<M, T>(type, clazz, (int) tag, false);
    }

    /**
     * Creates a repeated {@code Extension} of the given message type and tag number.
     * Should be used by the generated code only.
     *
     * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
     */
    public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
            Extension<M, T[]> createRepeatedMessageTyped(int type, Class<T[]> clazz, long tag) {
        return new Extension<M, T[]>(type, clazz, (int) tag, true);
    }

    /**
     * Creates an {@code Extension} of the given primitive type and tag number.
     * Should be used by the generated code only.
     *
     * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
     * @param clazz the boxed Java type of this extension
     */
    public static <M extends ExtendableMessageNano<M>, T>
            Extension<M, T> createPrimitiveTyped(int type, Class<T> clazz, long tag) {
        return new PrimitiveExtension<M, T>(type, clazz, (int) tag, false, 0, 0);
    }

    /**
     * Creates a repeated {@code Extension} of the given primitive type and tag number.
     * Should be used by the generated code only.
     *
     * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
     * @param clazz the Java array type of this extension, with an unboxed component type
     */
    public static <M extends ExtendableMessageNano<M>, T>
            Extension<M, T> createRepeatedPrimitiveTyped(
                    int type, Class<T> clazz, long tag, long nonPackedTag, long packedTag) {
        return new PrimitiveExtension<M, T>(type, clazz, (int) tag, true,
            (int) nonPackedTag, (int) packedTag);
    }

    /**
     * Protocol Buffer type of this extension; one of the {@code TYPE_} constants.
     */
    protected final int type;

    /**
     * Java type of this extension. For a singular extension, this is the boxed Java type for the
     * Protocol Buffer {@link #type}; for a repeated extension, this is an array type whose
     * component type is the unboxed Java type for {@link #type}. For example, for a singular
     * {@code int32}/{@link #TYPE_INT32} extension, this equals {@code Integer.class}; for a
     * repeated {@code int32} extension, this equals {@code int[].class}.
     */
    protected final Class<T> clazz;

    /**
     * Tag number of this extension. The data should be viewed as an unsigned 32-bit value.
     */
    public final int tag;

    /**
     * Whether this extension is repeated.
     */
    protected final boolean repeated;

    private Extension(int type, Class<T> clazz, int tag, boolean repeated) {
        this.type = type;
        this.clazz = clazz;
        this.tag = tag;
        this.repeated = repeated;
    }

    /**
     * Returns the value of this extension stored in the given list of unknown fields, or
     * {@code null} if no unknown fields matches this extension.
     *
     * @param unknownFields a list of {@link UnknownFieldData}. All of the elements must have a tag
     *                      that matches this Extension's tag.
     *
     */
    final T getValueFrom(List<UnknownFieldData> unknownFields) {
        if (unknownFields == null) {
            return null;
        }
        return repeated ? getRepeatedValueFrom(unknownFields) : getSingularValueFrom(unknownFields);
    }

    private T getRepeatedValueFrom(List<UnknownFieldData> unknownFields) {
        // For repeated extensions, read all matching unknown fields in their original order.
        List<Object> resultList = new ArrayList<Object>();
        for (int i = 0; i < unknownFields.size(); i++) {
            UnknownFieldData data = unknownFields.get(i);
            if (data.bytes.length != 0) {
                readDataInto(data, resultList);
            }
        }

        int resultSize = resultList.size();
        if (resultSize == 0) {
            return null;
        } else {
            T result = clazz.cast(Array.newInstance(clazz.getComponentType(), resultSize));
            for (int i = 0; i < resultSize; i++) {
                Array.set(result, i, resultList.get(i));
            }
            return result;
        }
    }

    private T getSingularValueFrom(List<UnknownFieldData> unknownFields) {
        // For singular extensions, get the last piece of data stored under this extension.
        if (unknownFields.isEmpty()) {
            return null;
        }
        UnknownFieldData lastData = unknownFields.get(unknownFields.size() - 1);
        return clazz.cast(readData(CodedInputByteBufferNano.newInstance(lastData.bytes)));
    }

    protected Object readData(CodedInputByteBufferNano input) {
        // This implementation is for message/group extensions.
        Class<?> messageType = repeated ? clazz.getComponentType() : clazz;
        try {
            switch (type) {
                case TYPE_GROUP:
                    MessageNano group = (MessageNano) messageType.newInstance();
                    input.readGroup(group, WireFormatNano.getTagFieldNumber(tag));
                    return group;
                case TYPE_MESSAGE:
                    MessageNano message = (MessageNano) messageType.newInstance();
                    input.readMessage(message);
                    return message;
                default:
                    throw new IllegalArgumentException("Unknown type " + type);
            }
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(
                    "Error creating instance of class " + messageType, e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(
                    "Error creating instance of class " + messageType, e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Error reading extension field", e);
        }
    }

    protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
        // This implementation is for message/group extensions.
        resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
    }

    void writeTo(Object value, CodedOutputByteBufferNano output) throws IOException {
        if (repeated) {
            writeRepeatedData(value, output);
        } else {
            writeSingularData(value, output);
        }
    }

    protected void writeSingularData(Object value, CodedOutputByteBufferNano out) {
        // This implementation is for message/group extensions.
        try {
            out.writeRawVarint32(tag);
            switch (type) {
                case TYPE_GROUP:
                    MessageNano groupValue = (MessageNano) value;
                    int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
                    out.writeGroupNoTag(groupValue);
                    // The endgroup tag must be included in the data payload.
                    out.writeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP);
                    break;
                case TYPE_MESSAGE:
                    MessageNano messageValue = (MessageNano) value;
                    out.writeMessageNoTag(messageValue);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown type " + type);
            }
        } catch (IOException e) {
            // Should not happen
            throw new IllegalStateException(e);
        }
    }

    protected void writeRepeatedData(Object array, CodedOutputByteBufferNano output) {
        // This implementation is for non-packed extensions.
        int arrayLength = Array.getLength(array);
        for (int i = 0; i < arrayLength; i++) {
            Object element = Array.get(array, i);
            if (element != null) {
                writeSingularData(element, output);
            }
        }
    }

    int computeSerializedSize(Object value) {
        if (repeated) {
            return computeRepeatedSerializedSize(value);
        } else {
            return computeSingularSerializedSize(value);
        }
    }

    protected int computeRepeatedSerializedSize(Object array) {
        // This implementation is for non-packed extensions.
        int size = 0;
        int arrayLength = Array.getLength(array);
        for (int i = 0; i < arrayLength; i++) {
            Object element = Array.get(array, i);
            if (element != null) {
                size += computeSingularSerializedSize(Array.get(array, i));
            }
        }
        return size;
    }

    protected int computeSingularSerializedSize(Object value) {
        // This implementation is for message/group extensions.
        int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
        switch (type) {
            case TYPE_GROUP:
                MessageNano groupValue = (MessageNano) value;
                return CodedOutputByteBufferNano.computeGroupSize(fieldNumber, groupValue);
            case TYPE_MESSAGE:
                MessageNano messageValue = (MessageNano) value;
                return CodedOutputByteBufferNano.computeMessageSize(fieldNumber, messageValue);
            default:
                throw new IllegalArgumentException("Unknown type " + type);
        }
    }

    /**
     * Represents an extension of a primitive (including enum) type. If there is no primitive
     * extensions, this subclass will be removable by ProGuard.
     */
    private static class PrimitiveExtension<M extends ExtendableMessageNano<M>, T>
            extends Extension<M, T> {

        /**
         * Tag of a piece of non-packed data from the wire compatible with this extension.
         */
        private final int nonPackedTag;

        /**
         * Tag of a piece of packed data from the wire compatible with this extension.
         * 0 if the type of this extension is not packable.
         */
        private final int packedTag;

        public PrimitiveExtension(int type, Class<T> clazz, int tag, boolean repeated,
                int nonPackedTag, int packedTag) {
            super(type, clazz, tag, repeated);
            this.nonPackedTag = nonPackedTag;
            this.packedTag = packedTag;
        }

        @Override
        protected Object readData(CodedInputByteBufferNano input) {
            try {
              return input.readPrimitiveField(type);
            } catch (IOException e) {
                throw new IllegalArgumentException("Error reading extension field", e);
            }
        }

        @Override
        protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
            // This implementation is for primitive typed extensions,
            // which can read both packed and non-packed data.
            if (data.tag == nonPackedTag) {
                resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
            } else {
                CodedInputByteBufferNano buffer =
                        CodedInputByteBufferNano.newInstance(data.bytes);
                try {
                    buffer.pushLimit(buffer.readRawVarint32()); // length limit
                } catch (IOException e) {
                    throw new IllegalArgumentException("Error reading extension field", e);
                }
                while (!buffer.isAtEnd()) {
                    resultList.add(readData(buffer));
                }
            }
        }

        @Override
        protected final void writeSingularData(Object value, CodedOutputByteBufferNano output) {
            try {
                output.writeRawVarint32(tag);
                switch (type) {
                    case TYPE_DOUBLE:
                        Double doubleValue = (Double) value;
                        output.writeDoubleNoTag(doubleValue);
                        break;
                    case TYPE_FLOAT:
                        Float floatValue = (Float) value;
                        output.writeFloatNoTag(floatValue);
                        break;
                    case TYPE_INT64:
                        Long int64Value = (Long) value;
                        output.writeInt64NoTag(int64Value);
                        break;
                    case TYPE_UINT64:
                        Long uint64Value = (Long) value;
                        output.writeUInt64NoTag(uint64Value);
                        break;
                    case TYPE_INT32:
                        Integer int32Value = (Integer) value;
                        output.writeInt32NoTag(int32Value);
                        break;
                    case TYPE_FIXED64:
                        Long fixed64Value = (Long) value;
                        output.writeFixed64NoTag(fixed64Value);
                        break;
                    case TYPE_FIXED32:
                        Integer fixed32Value = (Integer) value;
                        output.writeFixed32NoTag(fixed32Value);
                        break;
                    case TYPE_BOOL:
                        Boolean boolValue = (Boolean) value;
                        output.writeBoolNoTag(boolValue);
                        break;
                    case TYPE_STRING:
                        String stringValue = (String) value;
                        output.writeStringNoTag(stringValue);
                        break;
                    case TYPE_BYTES:
                        byte[] bytesValue = (byte[]) value;
                        output.writeBytesNoTag(bytesValue);
                        break;
                    case TYPE_UINT32:
                        Integer uint32Value = (Integer) value;
                        output.writeUInt32NoTag(uint32Value);
                        break;
                    case TYPE_ENUM:
                        Integer enumValue = (Integer) value;
                        output.writeEnumNoTag(enumValue);
                        break;
                    case TYPE_SFIXED32:
                        Integer sfixed32Value = (Integer) value;
                        output.writeSFixed32NoTag(sfixed32Value);
                        break;
                    case TYPE_SFIXED64:
                        Long sfixed64Value = (Long) value;
                        output.writeSFixed64NoTag(sfixed64Value);
                        break;
                    case TYPE_SINT32:
                        Integer sint32Value = (Integer) value;
                        output.writeSInt32NoTag(sint32Value);
                        break;
                    case TYPE_SINT64:
                        Long sint64Value = (Long) value;
                        output.writeSInt64NoTag(sint64Value);
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown type " + type);
                }
            } catch (IOException e) {
                // Should not happen
                throw new IllegalStateException(e);
            }
        }

        @Override
        protected void writeRepeatedData(Object array, CodedOutputByteBufferNano output) {
            if (tag == nonPackedTag) {
                // Use base implementation for non-packed data
                super.writeRepeatedData(array, output);
            } else if (tag == packedTag) {
                // Packed. Note that the array element type is guaranteed to be primitive, so there
                // won't be any null elements, so no null check in this block.
                int arrayLength = Array.getLength(array);
                int dataSize = computePackedDataSize(array);

                try {
                    output.writeRawVarint32(tag);
                    output.writeRawVarint32(dataSize);
                    switch (type) {
                        case TYPE_BOOL:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeBoolNoTag(Array.getBoolean(array, i));
                            }
                            break;
                        case TYPE_FIXED32:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeFixed32NoTag(Array.getInt(array, i));
                            }
                            break;
                        case TYPE_SFIXED32:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeSFixed32NoTag(Array.getInt(array, i));
                            }
                            break;
                        case TYPE_FLOAT:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeFloatNoTag(Array.getFloat(array, i));
                            }
                            break;
                        case TYPE_FIXED64:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeFixed64NoTag(Array.getLong(array, i));
                            }
                            break;
                        case TYPE_SFIXED64:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeSFixed64NoTag(Array.getLong(array, i));
                            }
                            break;
                        case TYPE_DOUBLE:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeDoubleNoTag(Array.getDouble(array, i));
                            }
                            break;
                        case TYPE_INT32:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeInt32NoTag(Array.getInt(array, i));
                            }
                            break;
                        case TYPE_SINT32:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeSInt32NoTag(Array.getInt(array, i));
                            }
                            break;
                        case TYPE_UINT32:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeUInt32NoTag(Array.getInt(array, i));
                            }
                            break;
                        case TYPE_INT64:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeInt64NoTag(Array.getLong(array, i));
                            }
                            break;
                        case TYPE_SINT64:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeSInt64NoTag(Array.getLong(array, i));
                            }
                            break;
                        case TYPE_UINT64:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeUInt64NoTag(Array.getLong(array, i));
                            }
                            break;
                        case TYPE_ENUM:
                            for (int i = 0; i < arrayLength; i++) {
                                output.writeEnumNoTag(Array.getInt(array, i));
                            }
                            break;
                        default:
                            throw new IllegalArgumentException("Unpackable type " + type);
                    }
                } catch (IOException e) {
                    // Should not happen.
                    throw new IllegalStateException(e);
                }
            } else {
                throw new IllegalArgumentException("Unexpected repeated extension tag " + tag
                        + ", unequal to both non-packed variant " + nonPackedTag
                        + " and packed variant " + packedTag);
            }
        }

        private int computePackedDataSize(Object array) {
            int dataSize = 0;
            int arrayLength = Array.getLength(array);
            switch (type) {
                case TYPE_BOOL:
                    // Bools are stored as int32 but just as 0 or 1, so 1 byte each.
                    dataSize = arrayLength;
                    break;
                case TYPE_FIXED32:
                case TYPE_SFIXED32:
                case TYPE_FLOAT:
                    dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_32_SIZE;
                    break;
                case TYPE_FIXED64:
                case TYPE_SFIXED64:
                case TYPE_DOUBLE:
                    dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_64_SIZE;
                    break;
                case TYPE_INT32:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeInt32SizeNoTag(
                                Array.getInt(array, i));
                    }
                    break;
                case TYPE_SINT32:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeSInt32SizeNoTag(
                                Array.getInt(array, i));
                    }
                    break;
                case TYPE_UINT32:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeUInt32SizeNoTag(
                                Array.getInt(array, i));
                    }
                    break;
                case TYPE_INT64:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeInt64SizeNoTag(
                                Array.getLong(array, i));
                    }
                    break;
                case TYPE_SINT64:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeSInt64SizeNoTag(
                                Array.getLong(array, i));
                    }
                    break;
                case TYPE_UINT64:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeUInt64SizeNoTag(
                                Array.getLong(array, i));
                    }
                    break;
                case TYPE_ENUM:
                    for (int i = 0; i < arrayLength; i++) {
                        dataSize += CodedOutputByteBufferNano.computeEnumSizeNoTag(
                                Array.getInt(array, i));
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected non-packable type " + type);
            }
            return dataSize;
        }

        @Override
        protected int computeRepeatedSerializedSize(Object array) {
            if (tag == nonPackedTag) {
                // Use base implementation for non-packed data
                return super.computeRepeatedSerializedSize(array);
            } else if (tag == packedTag) {
                // Packed.
                int dataSize = computePackedDataSize(array);
                int payloadSize =
                        dataSize + CodedOutputByteBufferNano.computeRawVarint32Size(dataSize);
                return payloadSize + CodedOutputByteBufferNano.computeRawVarint32Size(tag);
            } else {
                throw new IllegalArgumentException("Unexpected repeated extension tag " + tag
                        + ", unequal to both non-packed variant " + nonPackedTag
                        + " and packed variant " + packedTag);
            }
        }

        @Override
        protected final int computeSingularSerializedSize(Object value) {
            int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
            switch (type) {
                case TYPE_DOUBLE:
                    Double doubleValue = (Double) value;
                    return CodedOutputByteBufferNano.computeDoubleSize(fieldNumber, doubleValue);
                case TYPE_FLOAT:
                    Float floatValue = (Float) value;
                    return CodedOutputByteBufferNano.computeFloatSize(fieldNumber, floatValue);
                case TYPE_INT64:
                    Long int64Value = (Long) value;
                    return CodedOutputByteBufferNano.computeInt64Size(fieldNumber, int64Value);
                case TYPE_UINT64:
                    Long uint64Value = (Long) value;
                    return CodedOutputByteBufferNano.computeUInt64Size(fieldNumber, uint64Value);
                case TYPE_INT32:
                    Integer int32Value = (Integer) value;
                    return CodedOutputByteBufferNano.computeInt32Size(fieldNumber, int32Value);
                case TYPE_FIXED64:
                    Long fixed64Value = (Long) value;
                    return CodedOutputByteBufferNano.computeFixed64Size(fieldNumber, fixed64Value);
                case TYPE_FIXED32:
                    Integer fixed32Value = (Integer) value;
                    return CodedOutputByteBufferNano.computeFixed32Size(fieldNumber, fixed32Value);
                case TYPE_BOOL:
                    Boolean boolValue = (Boolean) value;
                    return CodedOutputByteBufferNano.computeBoolSize(fieldNumber, boolValue);
                case TYPE_STRING:
                    String stringValue = (String) value;
                    return CodedOutputByteBufferNano.computeStringSize(fieldNumber, stringValue);
                case TYPE_BYTES:
                    byte[] bytesValue = (byte[]) value;
                    return CodedOutputByteBufferNano.computeBytesSize(fieldNumber, bytesValue);
                case TYPE_UINT32:
                    Integer uint32Value = (Integer) value;
                    return CodedOutputByteBufferNano.computeUInt32Size(fieldNumber, uint32Value);
                case TYPE_ENUM:
                    Integer enumValue = (Integer) value;
                    return CodedOutputByteBufferNano.computeEnumSize(fieldNumber, enumValue);
                case TYPE_SFIXED32:
                    Integer sfixed32Value = (Integer) value;
                    return CodedOutputByteBufferNano.computeSFixed32Size(fieldNumber,
                            sfixed32Value);
                case TYPE_SFIXED64:
                    Long sfixed64Value = (Long) value;
                    return CodedOutputByteBufferNano.computeSFixed64Size(fieldNumber,
                            sfixed64Value);
                case TYPE_SINT32:
                    Integer sint32Value = (Integer) value;
                    return CodedOutputByteBufferNano.computeSInt32Size(fieldNumber, sint32Value);
                case TYPE_SINT64:
                    Long sint64Value = (Long) value;
                    return CodedOutputByteBufferNano.computeSInt64Size(fieldNumber, sint64Value);
                default:
                    throw new IllegalArgumentException("Unknown type " + type);
            }
        }
    }
}