001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.nio.entity;
029
030import java.io.ByteArrayInputStream;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034import java.nio.ByteBuffer;
035
036import org.apache.http.entity.AbstractHttpEntity;
037import org.apache.http.entity.ContentType;
038import org.apache.http.nio.ContentEncoder;
039import org.apache.http.nio.IOControl;
040import org.apache.http.util.Args;
041
042/**
043 * A simple self contained, repeatable non-blocking entity that retrieves
044 * its content from a byte array.
045 *
046 * @since 4.0
047 */
048@SuppressWarnings("deprecation")
049public class NByteArrayEntity extends AbstractHttpEntity
050                              implements HttpAsyncContentProducer, ProducingNHttpEntity {
051
052    private final byte[] b;
053    private final int off, len;
054    private final ByteBuffer buf;
055    /**
056     * @deprecated (4.2)
057     */
058    @Deprecated
059    protected final byte[] content;
060    /**
061     * @deprecated (4.2)
062     */
063    @Deprecated
064    protected final ByteBuffer buffer;
065
066    /**
067     * @since 4.2
068     */
069    public NByteArrayEntity(final byte[] b, final ContentType contentType) {
070        super();
071        Args.notNull(b, "Source byte array");
072        this.b = b;
073        this.off = 0;
074        this.len = b.length;
075        this.buf = ByteBuffer.wrap(b);
076        this.content = b;
077        this.buffer = this.buf;
078        if (contentType != null) {
079            setContentType(contentType.toString());
080        }
081    }
082
083    /**
084     * @since 4.2
085     */
086    public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
087        super();
088        Args.notNull(b, "Source byte array");
089        if ((off < 0) || (off > b.length) || (len < 0) ||
090                ((off + len) < 0) || ((off + len) > b.length)) {
091            throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
092        }
093        this.b = b;
094        this.off = off;
095        this.len = len;
096        this.buf = ByteBuffer.wrap(b, off, len);
097        this.content = b;
098        this.buffer = this.buf;
099        if (contentType != null) {
100            setContentType(contentType.toString());
101        }
102    }
103
104    public NByteArrayEntity(final byte[] b) {
105        this(b, null);
106    }
107
108    public NByteArrayEntity(final byte[] b, final int off, final int len) {
109        this(b, off, len, null);
110    }
111
112    /**
113     * {@inheritDoc}
114     *
115     * @since 4.2
116     */
117    @Override
118    public void close() {
119        this.buf.rewind();
120    }
121
122    /**
123     * {@inheritDoc}
124     *
125     * @deprecated (4.2) use {@link #close()}
126     */
127    @Override
128    @Deprecated
129    public void finish() {
130        close();
131    }
132
133    @Override
134    public void produceContent(final ContentEncoder encoder, final IOControl ioctrl)
135            throws IOException {
136        encoder.write(this.buf);
137        if(!this.buf.hasRemaining()) {
138            encoder.complete();
139        }
140    }
141
142    @Override
143    public long getContentLength() {
144        return this.len;
145    }
146
147    @Override
148    public boolean isRepeatable() {
149        return true;
150    }
151
152    @Override
153    public boolean isStreaming() {
154        return false;
155    }
156
157    @Override
158    public InputStream getContent() {
159        return new ByteArrayInputStream(this.b, this.off, this.len);
160    }
161
162    @Override
163    public void writeTo(final OutputStream outstream) throws IOException {
164        Args.notNull(outstream, "Output stream");
165        outstream.write(this.b, this.off, this.len);
166        outstream.flush();
167    }
168
169}