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          Args.notNegative(off, "offset");
60          Args.notNegative(len, "length");
61          Args.notNegative(off + len, "off + len");
62          Args.check(off <= b.length, "off %s cannot be greater then b.length %s ", off, b.length);
63          Args.check(off + len <= b.length, "off + len  %s cannot be less then b.length %s ", off + len, b.length);
64          this.b = b;
65          this.off = off;
66          this.len = len;
67      }
68  
69      /**
70       * @since 5.0
71       */
72      public ByteArrayEntity(
73              final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding) {
74          this(b, off, len, contentType, contentEncoding, false);
75      }
76  
77      /**
78       * @since 5.0
79       */
80      public ByteArrayEntity(
81              final byte[] b, final ContentType contentType, final String contentEncoding, final boolean chunked) {
82          super(contentType, contentEncoding, chunked);
83          Args.notNull(b, "Source byte array");
84          this.b = b;
85          this.off = 0;
86          this.len = this.b.length;
87      }
88  
89      /**
90       * @since 5.0
91       */
92      public ByteArrayEntity(final byte[] b, final ContentType contentType, final String contentEncoding) {
93          this(b, contentType, contentEncoding, false);
94      }
95  
96      public ByteArrayEntity(final byte[] b, final ContentType contentType, final boolean chunked) {
97          this(b, contentType, null, chunked);
98      }
99  
100     public ByteArrayEntity(final byte[] b, final ContentType contentType) {
101         this(b, contentType, null, false);
102     }
103 
104     public ByteArrayEntity(
105             final byte[] b, final int off, final int len, final ContentType contentType,  final boolean chunked) {
106         this(b, off, len, contentType, null, chunked);
107     }
108 
109     public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
110         this(b, off, len, contentType, null, false);
111     }
112 
113     @Override
114     public final boolean isRepeatable() {
115         return true;
116     }
117 
118     @Override
119     public final long getContentLength() {
120         return this.len;
121     }
122 
123     @Override
124     public final InputStream getContent() {
125         return new ByteArrayInputStream(this.b, this.off, this.len);
126     }
127 
128     @Override
129     public final void writeTo(final OutputStream outStream) throws IOException {
130         Args.notNull(outStream, "Output stream");
131         outStream.write(this.b, this.off, this.len);
132         outStream.flush();
133     }
134 
135     @Override
136     public final boolean isStreaming() {
137         return false;
138     }
139 
140     @Override
141     public final void close() throws IOException {
142     }
143 
144 }