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.hc.core5.http.io.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.hc.core5.http.ContentType;
36  import org.apache.hc.core5.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  public class ByteArrayEntity extends AbstractHttpEntity {
44  
45      private final byte[] b;
46      private final int off, len;
47  
48      /**
49       * @since 4.2
50       */
51      public ByteArrayEntity(final byte[] b, final ContentType contentType) {
52          super();
53          Args.notNull(b, "Source byte array");
54          this.b = b;
55          this.off = 0;
56          this.len = this.b.length;
57          if (contentType != null) {
58              setContentType(contentType.toString());
59          }
60      }
61  
62      /**
63       * @since 4.2
64       */
65      public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
66          super();
67          Args.notNull(b, "Source byte array");
68          if ((off < 0) || (off > b.length) || (len < 0) ||
69                  ((off + len) < 0) || ((off + len) > b.length)) {
70              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
71          }
72          this.b = b;
73          this.off = off;
74          this.len = len;
75          if (contentType != null) {
76              setContentType(contentType.toString());
77          }
78      }
79  
80      public ByteArrayEntity(final byte[] b) {
81          this(b, null);
82      }
83  
84      public ByteArrayEntity(final byte[] b, final int off, final int len) {
85          this(b, off, len, null);
86      }
87  
88      @Override
89      public boolean isRepeatable() {
90          return true;
91      }
92  
93      @Override
94      public long getContentLength() {
95          return this.len;
96      }
97  
98      @Override
99      public InputStream getContent() {
100         return new ByteArrayInputStream(this.b, this.off, this.len);
101     }
102 
103     @Override
104     public void writeTo(final OutputStream outStream) throws IOException {
105         Args.notNull(outStream, "Output stream");
106         outStream.write(this.b, this.off, this.len);
107         outStream.flush();
108     }
109 
110     /**
111      * Tells that this entity is not streaming.
112      *
113      * @return {@code false}
114      */
115     @Override
116     public boolean isStreaming() {
117         return false;
118     }
119 
120     @Override
121     public void close() throws IOException {
122     }
123 
124 }