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.client.fluent;
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.entity.AbstractHttpEntity;
36  import org.apache.http.entity.ContentType;
37  import org.apache.http.util.Args;
38  
39  class InternalByteArrayEntity extends AbstractHttpEntity implements Cloneable {
40  
41      private final byte[] b;
42      private final int off, len;
43  
44      public InternalByteArrayEntity(final byte[] b, final ContentType contentType) {
45          super();
46          Args.notNull(b, "Source byte array");
47          this.b = b;
48          this.off = 0;
49          this.len = this.b.length;
50          if (contentType != null) {
51              setContentType(contentType.toString());
52          }
53      }
54  
55      public InternalByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
56          super();
57          Args.notNull(b, "Source byte array");
58          if ((off < 0) || (off > b.length) || (len < 0) ||
59                  ((off + len) < 0) || ((off + len) > b.length)) {
60              throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
61          }
62          this.b = b;
63          this.off = off;
64          this.len = len;
65          if (contentType != null) {
66              setContentType(contentType.toString());
67          }
68      }
69  
70      public InternalByteArrayEntity(final byte[] b) {
71          this(b, null);
72      }
73  
74      public InternalByteArrayEntity(final byte[] b, final int off, final int len) {
75          this(b, off, len, null);
76      }
77  
78      @Override
79      public boolean isRepeatable() {
80          return true;
81      }
82  
83      @Override
84      public long getContentLength() {
85          return this.len;
86      }
87  
88      @Override
89      public InputStream getContent() {
90          return new ByteArrayInputStream(this.b, this.off, this.len);
91      }
92  
93      @Override
94      public void writeTo(final OutputStream outStream) throws IOException {
95          Args.notNull(outStream, "Output stream");
96          outStream.write(this.b, this.off, this.len);
97          outStream.flush();
98      }
99  
100     @Override
101     public boolean isStreaming() {
102         return false;
103     }
104 
105 }