Package org.asynchttpclient.providers.grizzly

Source Code of org.asynchttpclient.providers.grizzly.GrizzlyResponseBodyPart

/*
* Copyright (c) 2012 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 org.asynchttpclient.providers.grizzly;

import org.asynchttpclient.HttpResponseBodyPart;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.http.HttpContent;
import org.glassfish.grizzly.utils.BufferInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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
* @since 1.7.0
*/
class GrizzlyResponseBodyPart extends HttpResponseBodyPart {

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

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

    public GrizzlyResponseBodyPart(final HttpContent content, final Connection<?> connection) {
        this.content = content;
        this.connection = connection;
    }

    // --------------------------------------- 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.position(origPos);
        contentBytes.compareAndSet(null, bytes);
        return bytes;
    }

    @Override
    public InputStream readBodyPartBytes() {
        return new BufferInputStream(content.getContent());
    }

    @Override
    public int length() {
        return content.getContent().remaining();
    }

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

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

    /**
     * {@inheritDoc}
     */
    @Override
    public ByteBuffer getBodyByteBuffer() {
        return content.getContent().toByteBuffer();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isLast() {
        return content.isLast();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void markUnderlyingConnectionAsToBeClosed() {
        ConnectionManager.markConnectionAsDoNotCache(connection);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isUnderlyingConnectionToBeClosed() {
        return !ConnectionManager.isConnectionCacheable(connection);
    }

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

    Buffer getBodyBuffer() {
        return content.getContent();
    }
}
TOP

Related Classes of org.asynchttpclient.providers.grizzly.GrizzlyResponseBodyPart

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.