View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.nio.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.nio.ByteBuffer;
35  
36  import org.apache.http.annotation.NotThreadSafe;
37  import org.apache.http.entity.AbstractHttpEntity;
38  import org.apache.http.entity.ContentType;
39  import org.apache.http.nio.ContentEncoder;
40  import org.apache.http.nio.IOControl;
41  import org.apache.http.util.Args;
42  
43  /**
44   * A simple self contained, repeatable non-blocking entity that retrieves
45   * its content from a byte array.
46   *
47   * @since 4.0
48   */
49  @SuppressWarnings("deprecation")
50  @NotThreadSafe
51  public class NByteArrayEntity extends AbstractHttpEntity
52                                implements HttpAsyncContentProducer, ProducingNHttpEntity {
53  
54      private final byte[] b;
55      private final int off, len;
56      private final ByteBuffer buf;
57      /**
58       * @deprecated (4.2)
59       */
60      @Deprecated
61      protected final byte[] content;
62      /**
63       * @deprecated (4.2)
64       */
65      @Deprecated
66      protected final ByteBuffer buffer;
67  
68      /**
69       * @since 4.2
70       */
71      public NByteArrayEntity(final byte[] b, final ContentType contentType) {
72          super();
73          Args.notNull(b, "Source byte array");
74          this.b = b;
75          this.off = 0;
76          this.len = b.length;
77          this.buf = ByteBuffer.wrap(b);
78          this.content = b;
79          this.buffer = this.buf;
80          if (contentType != null) {
81              setContentType(contentType.toString());
82          }
83      }
84  
85      /**
86       * @since 4.2
87       */
88      public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
89          super();
90          Args.notNull(b, "Source byte array");
91          if ((off < 0) || (off > b.length) || (len < 0) ||
92                  ((off + len) < 0) || ((off + len) > b.length)) {
93              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
94          }
95          this.b = b;
96          this.off = off;
97          this.len = len;
98          this.buf = ByteBuffer.wrap(b, off, len);
99          this.content = b;
100         this.buffer = this.buf;
101         if (contentType != null) {
102             setContentType(contentType.toString());
103         }
104     }
105 
106     public NByteArrayEntity(final byte[] b) {
107         this(b, null);
108     }
109 
110     public NByteArrayEntity(final byte[] b, final int off, final int len) {
111         this(b, off, len, null);
112     }
113 
114     /**
115      * {@inheritDoc}
116      *
117      * @since 4.2
118      */
119     @Override
120     public void close() {
121         this.buf.rewind();
122     }
123 
124     /**
125      * {@inheritDoc}
126      *
127      * @deprecated (4.2) use {@link #close()}
128      */
129     @Deprecated
130     public void finish() {
131         close();
132     }
133 
134     @Override
135     public void produceContent(final ContentEncoder encoder, final IOControl ioctrl)
136             throws IOException {
137         encoder.write(this.buf);
138         if(!this.buf.hasRemaining()) {
139             encoder.complete();
140         }
141     }
142 
143     @Override
144     public long getContentLength() {
145         return this.len;
146     }
147 
148     @Override
149     public boolean isRepeatable() {
150         return true;
151     }
152 
153     @Override
154     public boolean isStreaming() {
155         return false;
156     }
157 
158     @Override
159     public InputStream getContent() {
160         return new ByteArrayInputStream(this.b, this.off, this.len);
161     }
162 
163     @Override
164     public void writeTo(final OutputStream outstream) throws IOException {
165         Args.notNull(outstream, "Output stream");
166         outstream.write(this.b, this.off, this.len);
167         outstream.flush();
168     }
169 
170 }