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.hc.core5.http.io.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import org.apache.hc.core5.http.impl.io.EmptyInputStream;
34  import org.apache.hc.core5.util.Asserts;
35  
36  /**
37   * A generic streamed, non-repeatable entity that obtains its content
38   * from an {@link InputStream}.
39   *
40   * @since 4.0
41   */
42  public class BasicHttpEntity extends AbstractHttpEntity {
43  
44      private InputStream content;
45      private long length;
46  
47      /**
48       * Creates a new basic entity.
49       * The content is initially missing, the content length
50       * is set to a negative number.
51       */
52      public BasicHttpEntity() {
53          super();
54          this.length = -1;
55      }
56  
57      @Override
58      public long getContentLength() {
59          return this.length;
60      }
61  
62      /**
63       * Obtains the content, once only.
64       *
65       * @return  the content, if this is the first call to this method
66       *          since {@link #setContent setContent} has been called
67       *
68       * @throws IllegalStateException
69       *          if the content has not been provided
70       */
71      @Override
72      public InputStream getContent() throws IllegalStateException {
73          Asserts.check(this.content != null, "Content has not been provided");
74          return this.content;
75      }
76  
77      /**
78       * Tells that this entity is not repeatable.
79       *
80       * @return {@code false}
81       */
82      @Override
83      public boolean isRepeatable() {
84          return false;
85      }
86  
87      /**
88       * Specifies the length of the content.
89       *
90       * @param len       the number of bytes in the content, or
91       *                  a negative number to indicate an unknown length
92       */
93      public void setContentLength(final long len) {
94          this.length = len;
95      }
96  
97      /**
98       * Specifies the content.
99       *
100      * @param instream          the stream to return with the next call to
101      *                          {@link #getContent getContent}
102      */
103     public void setContent(final InputStream instream) {
104         this.content = instream;
105     }
106 
107     @Override
108     public boolean isStreaming() {
109         return this.content != null && this.content != EmptyInputStream.INSTANCE;
110     }
111 
112     @Override
113     public void close() throws IOException {
114         if (content != null) {
115             content.close();
116         }
117     }
118 
119 }