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.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.http.HttpEntity;
36  import org.apache.http.annotation.NotThreadSafe;
37  import org.apache.http.util.EntityUtils;
38  
39  /**
40   * A wrapping entity that buffers it content if necessary.
41   * The buffered entity is always repeatable.
42   * If the wrapped entity is repeatable itself, calls are passed through.
43   * If the wrapped entity is not repeatable, the content is read into a
44   * buffer once and provided from there as often as required.
45   *
46   * @since 4.0
47   */
48  @NotThreadSafe
49  public class BufferedHttpEntity extends HttpEntityWrapper {
50  
51      private final byte[] buffer;
52  
53      /**
54       * Creates a new buffered entity wrapper.
55       *
56       * @param entity   the entity to wrap, not null
57       * @throws IllegalArgumentException if wrapped is null
58       */
59      public BufferedHttpEntity(final HttpEntity entity) throws IOException {
60          super(entity);
61          if (!entity.isRepeatable() || entity.getContentLength() < 0) {
62              this.buffer = EntityUtils.toByteArray(entity);
63          } else {
64              this.buffer = null;
65          }
66      }
67  
68      @Override
69      public long getContentLength() {
70          if (this.buffer != null) {
71              return this.buffer.length;
72          } else {
73              return wrappedEntity.getContentLength();
74          }
75      }
76  
77      @Override
78      public InputStream getContent() throws IOException {
79          if (this.buffer != null) {
80              return new ByteArrayInputStream(this.buffer);
81          } else {
82              return wrappedEntity.getContent();
83          }
84      }
85  
86      /**
87       * Tells that this entity does not have to be chunked.
88       *
89       * @return  <code>false</code>
90       */
91      @Override
92      public boolean isChunked() {
93          return (buffer == null) && wrappedEntity.isChunked();
94      }
95  
96      /**
97       * Tells that this entity is repeatable.
98       *
99       * @return  <code>true</code>
100      */
101     @Override
102     public boolean isRepeatable() {
103         return true;
104     }
105 
106 
107     @Override
108     public void writeTo(final OutputStream outstream) throws IOException {
109         if (outstream == null) {
110             throw new IllegalArgumentException("Output stream may not be null");
111         }
112         if (this.buffer != null) {
113             outstream.write(this.buffer);
114         } else {
115             wrappedEntity.writeTo(outstream);
116         }
117     }
118 
119 
120     // non-javadoc, see interface HttpEntity
121     @Override
122     public boolean isStreaming() {
123         return (buffer == null) && wrappedEntity.isStreaming();
124     }
125 
126 } // class BufferedHttpEntity