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