/*
 * 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.nio.ByteBuffer;

import org.apache.qpid.proton4j.buffer.ProtonBuffer;
import org.apache.qpid.proton4j.codec.DecoderState;

/**
 * Base for the various String type Decoders used to read AMQP String values.
 */
public abstract class AbstractStringTypeDecoder implements StringTypeDecoder {

    @Override
    public String readValue(ProtonBuffer buffer, DecoderState state) throws IOException {
        int length = readSize(buffer);

        // TODO - If we provide a slice and dice on the ProtonBuffer and then use the
        //        toByteBuffer methods we could avoid an actual copy of these bytes
        //        which might not but massively faster it would reduce GC overhead.
        byte[] bytes = new byte[length];
        buffer.readBytes(bytes, 0, length);

        return state.decodeUTF8(ByteBuffer.wrap(bytes));
    }

    @Override
    public void skipValue(ProtonBuffer buffer, DecoderState state) throws IOException {
        buffer.skipBytes(readSize(buffer));
    }

    protected abstract int readSize(ProtonBuffer buffer);

}

@@ -888,36 +888,8 @@
 ion;
-%0Aimport java.nio.ByteBuffer;
 %0A%0Aim
@@ -1314,293 +1314,97 @@
     
-// TODO - If we provide a slice and dice on the ProtonBuffer and then use the%0A        //        toByteBuffer methods we could avoid an actual copy of these bytes%0A        //        which might not but massively faster it would reduce GC overhead.%0A        byte%5B%5D bytes = new byte%5B
+ProtonBuffer duplicate = buffer.duplicate().setWriteIndex(buffer.getReadIndex() + 
 length
-%5D
+)
 ;%0A  
@@ -1420,27 +1420,44 @@
 fer.
-readBytes(bytes, 0,
+setReadIndex(buffer.getReadIndex() +
  len
@@ -1499,29 +1499,31 @@
 TF8(
-ByteBuffer.wrap(bytes
+duplicate.toByteBuffer(
 ));%0A

Stop copying the string bytes and just use buffer duplicates