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

Quick Search    Search Deep

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


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/entity/BasicHttpEntity.java $
3    * $Revision: 385289 $
4    * $Date: 2006-03-12 15:04:36 +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.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  /**
37   * A generic streamed entity being received on a connection.
38   *
39   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
40   *
41   * @version $Revision: 385289 $
42   * 
43   * @since 4.0
44   */
45  public class BasicHttpEntity extends AbstractHttpEntity {
46  
47      private InputStream content;
48      private boolean contentObtained;
49      private long length;
50  
51      /**
52       * Creates a new basic entity.
53       * The content is initially missing, the content length
54       * is set to a negative number.
55       */
56      public BasicHttpEntity() {
57          super();
58          this.length = -1;
59      }
60  
61      // non-javadoc, see interface HttpEntity
62      public long getContentLength() {
63          return this.length;
64      }
65  
66      /**
67       * Obtains the content, once only.
68       *
69       * @return  the content, if this is the first call to this method
70       *          since {@link #setContent setContent} has been called
71       *
72       * @throws IllegalStateException
73       *          if the content has been obtained before, or
74       *          has not yet been provided
75       */
76      public InputStream getContent()
77          throws IllegalStateException {
78          if (this.content == null) {
79              throw new IllegalStateException("Content has not been provided");
80          }
81          if (this.contentObtained) {
82              throw new IllegalStateException("Content has been consumed");
83          }
84          this.contentObtained = true;
85          return this.content;
86  
87      } // getContent
88  
89      /**
90       * Tells that this entity is not repeatable.
91       *
92       * @return <code>false</code>
93       */
94      public boolean isRepeatable() {
95          return false;
96      }
97  
98      /**
99       * Specifies the length of the content.
100      *
101      * @param len       the number of bytes in the content, or
102      *                  a negative number to indicate an unknown length
103      */
104     public void setContentLength(long len) {
105         this.length = len;
106     }
107 
108     /**
109      * Specifies the content.
110      *
111      * @param instream          the stream to return with the next call to
112      *                          {@link #getContent getContent}
113      */
114     public void setContent(final InputStream instream) {
115         this.content = instream;
116         this.contentObtained = false; 
117     }
118 
119     // non-javadoc, see interface HttpEntity
120     public void writeTo(final OutputStream outstream) throws IOException {
121         if (outstream == null) {
122             throw new IllegalArgumentException("Output stream may not be null");
123         }
124         InputStream instream = getContent();
125         int l;
126         byte[] tmp = new byte[2048];
127         while ((l = instream.read(tmp)) != -1) {
128             outstream.write(tmp, 0, l);
129         }
130     }
131 
132     // non-javadoc, see interface HttpEntity
133     public boolean isStreaming() {
134         return !this.contentObtained && this.content != null;
135     }
136 
137     // non-javadoc, see interface HttpEntity
138     public void consumeContent() throws IOException {
139         if (content != null) {
140             content.close(); // reads to the end of the entity
141         }
142     }
143     
144 } // class BasicHttpEntity