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.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  import org.apache.http.annotation.NotThreadSafe;
36  
37  /**
38   * A self contained, repeatable entity that obtains its content from a byte array.
39   *
40   * @since 4.0
41   */
42  @NotThreadSafe
43  public class ByteArrayEntity extends AbstractHttpEntity implements Cloneable {
44  
45      /**
46       * @deprecated (4.2)
47       */
48      @Deprecated
49      protected final byte[] content;
50      private final byte[] b;
51      private final int off, len;
52  
53      /**
54       * @since 4.2
55       */
56      public ByteArrayEntity(final byte[] b, final ContentType contentType) {
57          super();
58          if (b == null) {
59              throw new IllegalArgumentException("Source byte array may not be null");
60          }
61          this.content = b;
62          this.b = b;
63          this.off = 0;
64          this.len = this.b.length;
65          if (contentType != null) {
66              setContentType(contentType.toString());
67          }
68      }
69  
70      /**
71       * @since 4.2
72       */
73      public ByteArrayEntity(final byte[] b, int off, int len, final ContentType contentType) {
74          super();
75          if (b == null) {
76              throw new IllegalArgumentException("Source byte array may not be null");
77          }
78          if ((off < 0) || (off > b.length) || (len < 0) ||
79                  ((off + len) < 0) || ((off + len) > b.length)) {
80              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
81          }
82          this.content = b;
83          this.b = b;
84          this.off = off;
85          this.len = len;
86          if (contentType != null) {
87              setContentType(contentType.toString());
88          }
89      }
90  
91      public ByteArrayEntity(final byte[] b) {
92          this(b, null);
93      }
94  
95      public ByteArrayEntity(final byte[] b, int off, int len) {
96          this(b, off, len, null);
97      }
98  
99      public boolean isRepeatable() {
100         return true;
101     }
102 
103     public long getContentLength() {
104         return this.len;
105     }
106 
107     public InputStream getContent() {
108         return new ByteArrayInputStream(this.b, this.off, this.len);
109     }
110 
111     public void writeTo(final OutputStream outstream) throws IOException {
112         if (outstream == null) {
113             throw new IllegalArgumentException("Output stream may not be null");
114         }
115         outstream.write(this.b, this.off, this.len);
116         outstream.flush();
117     }
118 
119 
120     /**
121      * Tells that this entity is not streaming.
122      *
123      * @return <code>false</code>
124      */
125     public boolean isStreaming() {
126         return false;
127     }
128 
129     @Override
130     public Object clone() throws CloneNotSupportedException {
131         return super.clone();
132     }
133 
134 } // class ByteArrayEntity