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.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * A self contained, repeatable entity that obtains its content from a byte array.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
46  public class ByteArrayEntity extends AbstractHttpEntity {
47  
48      private final byte[] b;
49      private final int off, len;
50  
51      /**
52       * @since 5.0
53       */
54      public ByteArrayEntity(
55              final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding,
56              final boolean chunked) {
57          super(contentType, contentEncoding, chunked);
58          Args.notNull(b, "Source byte array");
59          if ((off < 0) || (off > b.length) || (len < 0) ||
60                  ((off + len) < 0) || ((off + len) > b.length)) {
61              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
62          }
63          this.b = b;
64          this.off = off;
65          this.len = len;
66      }
67  
68      /**
69       * @since 5.0
70       */
71      public ByteArrayEntity(
72              final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding) {
73          this(b, off, len, contentType, contentEncoding, false);
74      }
75  
76      /**
77       * @since 5.0
78       */
79      public ByteArrayEntity(
80              final byte[] b, final ContentType contentType, final String contentEncoding, final boolean chunked) {
81          super(contentType, contentEncoding, chunked);
82          Args.notNull(b, "Source byte array");
83          this.b = b;
84          this.off = 0;
85          this.len = this.b.length;
86      }
87  
88      /**
89       * @since 5.0
90       */
91      public ByteArrayEntity(final byte[] b, final ContentType contentType, final String contentEncoding) {
92          this(b, contentType, contentEncoding, false);
93      }
94  
95      public ByteArrayEntity(final byte[] b, final ContentType contentType, final boolean chunked) {
96          this(b, contentType, null, chunked);
97      }
98  
99      public ByteArrayEntity(final byte[] b, final ContentType contentType) {
100         this(b, contentType, null, false);
101     }
102 
103     public ByteArrayEntity(
104             final byte[] b, final int off, final int len, final ContentType contentType,  final boolean chunked) {
105         this(b, off, len, contentType, null, chunked);
106     }
107 
108     public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
109         this(b, off, len, contentType, null, false);
110     }
111 
112     @Override
113     public final boolean isRepeatable() {
114         return true;
115     }
116 
117     @Override
118     public final long getContentLength() {
119         return this.len;
120     }
121 
122     @Override
123     public final InputStream getContent() {
124         return new ByteArrayInputStream(this.b, this.off, this.len);
125     }
126 
127     @Override
128     public final void writeTo(final OutputStream outStream) throws IOException {
129         Args.notNull(outStream, "Output stream");
130         outStream.write(this.b, this.off, this.len);
131         outStream.flush();
132     }
133 
134     @Override
135     public final boolean isStreaming() {
136         return false;
137     }
138 
139     @Override
140     public final void close() throws IOException {
141     }
142 
143 }