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