/*
 * Copyright (c) 2011 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

package com.ning.http.client.providers.grizzly;

import com.ning.http.client.AsyncHttpProvider;
import com.ning.http.client.HttpResponseBodyPart;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.http.HttpContent;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;

/**
 * {@link HttpResponseBodyPart} implementation using the Grizzly 2.0 HTTP client
 * codec.
 *
 * @author The Grizzly Team
 */
public class GrizzlyResponseBodyPart extends HttpResponseBodyPart {

    private final HttpContent content;
    private final AtomicReference<byte[]> contentBytes =
            new AtomicReference<byte[]>();


    // ------------------------------------------------------------ Constructors


    public GrizzlyResponseBodyPart(final HttpContent content,
                                   final URI uri,
                                   final AsyncHttpProvider provider) {
        super(uri, provider);
        this.content = content;
    }


    // --------------------------------------- Methods from HttpResponseBodyPart


    /**
     * {@inheritDoc}
     */
    @Override
    public byte[] getBodyPartBytes() {

        byte[] bytes = contentBytes.get();
        if (bytes != null) {
            return bytes;
        }
        final Buffer b = content.getContent();
        final int origPos = b.position();
        bytes = new byte[b.remaining()];
        b.get(bytes);
        b.flip();
        b.position(origPos);
        contentBytes.compareAndSet(null, bytes);
        return bytes;

    }


    /**
     * {@inheritDoc}
     */
    @Override
    public int writeTo(OutputStream outputStream) throws IOException {

        final byte[] bytes = getBodyPartBytes();
        outputStream.write(getBodyPartBytes());
        return bytes.length;

    }


    /**
     * {@inheritDoc}
     */
    @Override
    public ByteBuffer getBodyByteBuffer() {

        return ByteBuffer.wrap(getBodyPartBytes());

    }


    // ----------------------------------------------- Package Protected Methods


    Buffer getBodyBuffer() {

        return content.getContent();

    }

}

@@ -2718,32 +2718,313 @@
 tes());%0A%0A    %7D%0A%0A
+    @Override%0A    public boolean isLast() %7B%0A        return content.isLast();%0A    %7D%0A%0A    @Override%0A    public void markUnderlyingConnectionAsClosed() %7B%0A        // TODO%0A    %7D%0A%0A    @Override%0A    public boolean closeUnderlyingConnection() %7B%0A        // TODO%0A        return false;%0A    %7D%0A
 %0A    // --------

Update based on API changes.