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.annotation.NotThreadSafe;
35  import org.apache.http.impl.io.EmptyInputStream;
36  import org.apache.http.util.Args;
37  import org.apache.http.util.Asserts;
38  
39  /**
40   * A generic streamed, non-repeatable entity that obtains its content
41   * from an {@link InputStream}.
42   *
43   * @since 4.0
44   */
45  @NotThreadSafe
46  public class BasicHttpEntity extends AbstractHttpEntity {
47  
48      private InputStream content;
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      @Override
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 not been provided
74       */
75      @Override
76      public InputStream getContent() throws IllegalStateException {
77          Asserts.check(this.content != null, "Content has not been provided");
78          return this.content;
79      }
80  
81      /**
82       * Tells that this entity is not repeatable.
83       *
84       * @return {@code false}
85       */
86      @Override
87      public boolean isRepeatable() {
88          return false;
89      }
90  
91      /**
92       * Specifies the length of the content.
93       *
94       * @param len       the number of bytes in the content, or
95       *                  a negative number to indicate an unknown length
96       */
97      public void setContentLength(final long len) {
98          this.length = len;
99      }
100 
101     /**
102      * Specifies the content.
103      *
104      * @param instream          the stream to return with the next call to
105      *                          {@link #getContent getContent}
106      */
107     public void setContent(final InputStream instream) {
108         this.content = instream;
109     }
110 
111     @Override
112     public void writeTo(final OutputStream outstream) throws IOException {
113         Args.notNull(outstream, "Output stream");
114         final InputStream instream = getContent();
115         try {
116             int l;
117             final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
118             while ((l = instream.read(tmp)) != -1) {
119                 outstream.write(tmp, 0, l);
120             }
121         } finally {
122             instream.close();
123         }
124     }
125 
126     @Override
127     public boolean isStreaming() {
128         return this.content != null && this.content != EmptyInputStream.INSTANCE;
129     }
130 
131 }