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.entity.AbstractHttpEntity;
37  import org.apache.http.entity.ContentType;
38  import org.apache.http.nio.ContentEncoder;
39  import org.apache.http.nio.IOControl;
40  import org.apache.http.util.Args;
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  public class NByteArrayEntity extends AbstractHttpEntity
50                                implements HttpAsyncContentProducer, ProducingNHttpEntity {
51  
52      private final byte[] b;
53      private final int off, len;
54      private final ByteBuffer buf;
55      /**
56       * @deprecated (4.2)
57       */
58      @Deprecated
59      protected final byte[] content;
60      /**
61       * @deprecated (4.2)
62       */
63      @Deprecated
64      protected final ByteBuffer buffer;
65  
66      /**
67       * @since 4.2
68       */
69      public NByteArrayEntity(final byte[] b, final ContentType contentType) {
70          super();
71          Args.notNull(b, "Source byte array");
72          this.b = b;
73          this.off = 0;
74          this.len = b.length;
75          this.buf = ByteBuffer.wrap(b);
76          this.content = b;
77          this.buffer = this.buf;
78          if (contentType != null) {
79              setContentType(contentType.toString());
80          }
81      }
82  
83      /**
84       * @since 4.2
85       */
86      public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
87          super();
88          Args.notNull(b, "Source byte array");
89          if ((off < 0) || (off > b.length) || (len < 0) ||
90                  ((off + len) < 0) || ((off + len) > b.length)) {
91              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
92          }
93          this.b = b;
94          this.off = off;
95          this.len = len;
96          this.buf = ByteBuffer.wrap(b, off, len);
97          this.content = b;
98          this.buffer = this.buf;
99          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     @Deprecated
128     public void finish() {
129         close();
130     }
131 
132     @Override
133     public void produceContent(final ContentEncoder encoder, final IOControl ioctrl)
134             throws IOException {
135         encoder.write(this.buf);
136         if(!this.buf.hasRemaining()) {
137             encoder.complete();
138         }
139     }
140 
141     @Override
142     public long getContentLength() {
143         return this.len;
144     }
145 
146     @Override
147     public boolean isRepeatable() {
148         return true;
149     }
150 
151     @Override
152     public boolean isStreaming() {
153         return false;
154     }
155 
156     @Override
157     public InputStream getContent() {
158         return new ByteArrayInputStream(this.b, this.off, this.len);
159     }
160 
161     @Override
162     public void writeTo(final OutputStream outstream) throws IOException {
163         Args.notNull(outstream, "Output stream");
164         outstream.write(this.b, this.off, this.len);
165         outstream.flush();
166     }
167 
168 }