/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.qpid.proton4j.codec.decoders.primitives;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.qpid.proton4j.codec.DecoderState;
import org.apache.qpid.proton4j.codec.EncodingCodes;
import org.apache.qpid.proton4j.codec.PrimitiveTypeDecoder;
import org.apache.qpid.proton4j.codec.TypeDecoder;

import io.netty.buffer.ByteBuf;

/**
 * Base for the various List type decoders needed to read AMQP List values.
 */
public abstract class AbstractListTypeDecoder implements ListTypeDecoder {

    @SuppressWarnings("rawtypes")
    @Override
    public List<Object> readValue(ByteBuf buffer, DecoderState state) throws IOException {
        int size = readSize(buffer);
        int count = readCount(buffer);

        // Ensure we do not allocate an array of size greater then the available data, otherwise there is a risk for an OOM error
        if (count > buffer.readableBytes()) {
            throw new IllegalArgumentException(String.format(
                    "List element size %d is specified to be greater than the amount " +
                    "of data available (%d)", size, buffer.readableBytes()));
        }

        TypeDecoder typeDecoder = null;

        List<Object> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            if (typeDecoder == null) {
                typeDecoder = state.getDecoder().readNextTypeDecoder(buffer, state);
            } else {
                byte encodingCode = buffer.getByte(buffer.readerIndex());
                if (encodingCode == EncodingCodes.DESCRIBED_TYPE_INDICATOR) {
                    typeDecoder = state.getDecoder().readNextTypeDecoder(buffer, state);
                } else {
                    PrimitiveTypeDecoder primitiveTypeDecoder = (PrimitiveTypeDecoder) typeDecoder;
                    if (encodingCode != primitiveTypeDecoder.getTypeCode()) {
                        typeDecoder = state.getDecoder().readNextTypeDecoder(buffer, state);
                    } else {
                        buffer.readByte();
                    }
                }
            }

            list.add(typeDecoder.readValue(buffer, state));
        }

        return list;
    }

    @Override
    public void readValue(ByteBuf buffer, DecoderState state, ListEntryHandler handler, Object target) throws IOException {
        int size = readSize(buffer);
        int count = readCount(buffer);

        // Ensure we do not allocate an array of size greater then the available data, otherwise there is a risk for an OOM error
        if (count > buffer.readableBytes()) {
            throw new IllegalArgumentException(String.format(
                    "List element size %d is specified to be greater than the amount " +
                    "of data available (%d)", size, buffer.readableBytes()));
        }

        for (int i = 0; i < count; i++) {
            handler.onListEntry(i, state.getDecoder().readObject(buffer, state), target);
        }
    }

    protected abstract int readSize(ByteBuf buffer);

    protected abstract int readCount(ByteBuf buffer);

}

@@ -1998,16 +1998,19 @@
 eDecoder
+%3C?%3E
  typeDec
@@ -2022,16 +2022,16 @@
 = null;%0A
-
 %0A       
@@ -2254,32 +2254,75 @@
        %7D else %7B%0A
+                buffer.markReaderIndex();%0A%0A
                 
@@ -2352,36 +2352,17 @@
 fer.
-getByte(buffer.readerIndex()
+readByte(
 );%0A 
@@ -2434,19 +2434,119 @@
 NDICATOR
-) %7B
+ %7C%7C !(typeDecoder instanceof PrimitiveTypeDecoder%3C?%3E)) %7B%0A                    buffer.resetReaderIndex();
 %0A       
@@ -2826,24 +2826,75 @@
 peCode()) %7B%0A
+                        buffer.resetReaderIndex();%0A
             
@@ -2978,80 +2978,8 @@
 e);%0A
-                    %7D else %7B%0A                        buffer.readByte();%0A
     

Clean up optimized list decode impl