Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/entity/BufferedHttpEntity.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/entity/BufferedHttpEntity.java $
3    * $Revision: 385308 $
4    * $Date: 2006-03-12 17:25:59 +0100 (Sun, 12 Mar 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.entity;
31  
32  import java.io.ByteArrayInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  
37  import org.apache.http.HttpEntity;
38  import org.apache.http.util.EntityUtils;
39  
40  /**
41   * A wrapping entity that buffers it content if necessary.
42   * The buffered entity is always repeatable.
43   * If the wrapped entity is repeatable itself, calls are passed through.
44   * If the wrapped entity is not repeatable, the content is read into a
45   * buffer once and provided from there as often as required.
46   *
47   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48   *
49   * @version $Revision: 385308 $
50   * 
51   * @since 4.0
52   */
53  public class BufferedHttpEntity extends HttpEntityWrapper {
54        
55      private final byte[] buffer;
56        
57      public BufferedHttpEntity(final HttpEntity entity) throws IOException {
58          super(entity);
59          if (!entity.isRepeatable() || entity.getContentLength() < 0) {
60              this.buffer = EntityUtils.toByteArray(entity);
61          } else {
62              this.buffer = null;
63          }
64      }
65  
66      public long getContentLength() {
67          if (this.buffer != null) {
68              return this.buffer.length;
69          } else {
70              return wrappedEntity.getContentLength();
71          }
72      }
73      
74      public InputStream getContent() throws IOException {
75          if (this.buffer != null) {
76              return new ByteArrayInputStream(this.buffer);
77          } else {
78              return wrappedEntity.getContent();
79          }
80      }
81  
82      /**
83       * Tells that this entity does not have to be chunked.
84       *
85       * @return  <code>false</code>
86       */
87      public boolean isChunked() {
88          return (buffer == null) && wrappedEntity.isChunked();
89      }
90      
91      /**
92       * Tells that this entity is repeatable.
93       *
94       * @return  <code>true</code>
95       */
96      public boolean isRepeatable() {
97          return true;
98      }
99  
100     
101     public void writeTo(final OutputStream outstream) throws IOException {
102         if (outstream == null) {
103             throw new IllegalArgumentException("Output stream may not be null");
104         }
105         if (this.buffer != null) {
106             outstream.write(this.buffer);
107         } else {
108             wrappedEntity.writeTo(outstream);
109         }
110     }
111 
112 
113     // non-javadoc, see interface HttpEntity
114     public boolean isStreaming() {
115         return (buffer == null) && wrappedEntity.isStreaming();
116     }
117     
118 } // class BufferedHttpEntity