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      public boolean isRepeatable() {
79          return true;
80      }
81  
82      public long getContentLength() {
83          return this.len;
84      }
85  
86      public InputStream getContent() {
87          return new ByteArrayInputStream(this.b, this.off, this.len);
88      }
89  
90      public void writeTo(final OutputStream outstream) throws IOException {
91          Args.notNull(outstream, "Output stream");
92          outstream.write(this.b, this.off, this.len);
93          outstream.flush();
94      }
95  
96      public boolean isStreaming() {
97          return false;
98      }
99  
100 }