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  
42  /**
43   * A simple self contained, repeatable non-blocking entity that retrieves
44   * its content from a byte array.
45   *
46   * @since 4.0
47   */
48  @SuppressWarnings("deprecation")
49  @NotThreadSafe
50  public class NByteArrayEntity extends AbstractHttpEntity
51                                implements HttpAsyncContentProducer, ProducingNHttpEntity {
52  
53      private final byte[] b;
54      private final int off, len;
55      private final ByteBuffer buf;
56      /**
57       * @deprecated (4.2)
58       */
59      @Deprecated
60      protected final byte[] content;
61      /**
62       * @deprecated (4.2)
63       */
64      @Deprecated
65      protected final ByteBuffer buffer;
66  
67      /**
68       * @since 4.2
69       */
70      public NByteArrayEntity(final byte[] b, final ContentType contentType) {
71          super();
72          if (b == null) {
73              throw new IllegalArgumentException("Source byte array may not be null");
74          }
75          this.b = b;
76          this.off = 0;
77          this.len = b.length;
78          this.buf = ByteBuffer.wrap(b);
79          this.content = b;
80          this.buffer = this.buf;
81          if (contentType != null) {
82              setContentType(contentType.toString());
83          }
84      }
85  
86      /**
87       * @since 4.2
88       */
89      public NByteArrayEntity(final byte[] b, int off, int len, final ContentType contentType) {
90          super();
91          if (b == null) {
92              throw new IllegalArgumentException("Source byte array may not be null");
93          }
94          if ((off < 0) || (off > b.length) || (len < 0) ||
95                  ((off + len) < 0) || ((off + len) > b.length)) {
96              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
97          }
98          this.b = b;
99          this.off = off;
100         this.len = len;
101         this.buf = ByteBuffer.wrap(b, off, len);
102         this.content = b;
103         this.buffer = this.buf;
104         if (contentType != null) {
105             setContentType(contentType.toString());
106         }
107     }
108 
109     public NByteArrayEntity(final byte[] b) {
110         this(b, null);
111     }
112 
113     public NByteArrayEntity(final byte[] b, int off, int len) {
114         this(b, off, len, null);
115     }
116 
117     /**
118      * {@inheritDoc}
119      *
120      * @since 4.2
121      */
122     public void close() {
123         this.buf.rewind();
124     }
125 
126     /**
127      * {@inheritDoc}
128      *
129      * @deprecated (4.2) use {@link #close()}
130      */
131     public void finish() {
132         close();
133     }
134 
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     public long getContentLength() {
144         return this.len;
145     }
146 
147     public boolean isRepeatable() {
148         return true;
149     }
150 
151     public boolean isStreaming() {
152         return false;
153     }
154 
155     public InputStream getContent() {
156         return new ByteArrayInputStream(this.b, this.off, this.len);
157     }
158 
159     public void writeTo(final OutputStream outstream) throws IOException {
160         if (outstream == null) {
161             throw new IllegalArgumentException("Output stream may not be null");
162         }
163         outstream.write(this.b, this.off, this.len);
164         outstream.flush();
165     }
166 
167 }