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  import java.io.OutputStream;
33  
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.util.Args;
36  
37  /**
38   * A streamed, non-repeatable entity that obtains its content from
39   * an {@link InputStream}.
40   *
41   * @since 4.0
42   */
43  public class InputStreamEntity extends AbstractHttpEntity {
44  
45      private final InputStream content;
46      private final long length;
47  
48      /**
49       * Creates an entity with an unknown length.
50       * Equivalent to {@code new InputStreamEntity(inStream, -1)}.
51       *
52       * @param inStream input stream
53       * @throws IllegalArgumentException if {@code inStream} is {@code null}
54       * @since 4.3
55       */
56      public InputStreamEntity(final InputStream inStream) {
57          this(inStream, -1);
58      }
59  
60      /**
61       * Creates an entity with a specified content length.
62       *
63       * @param inStream input stream
64       * @param length of the input stream, {@code -1} if unknown
65       * @throws IllegalArgumentException if {@code inStream} is {@code null}
66       */
67      public InputStreamEntity(final InputStream inStream, final long length) {
68          this(inStream, length, null);
69      }
70  
71      /**
72       * Creates an entity with a content type and unknown length.
73       * Equivalent to {@code new InputStreamEntity(inStream, -1, contentType)}.
74       *
75       * @param inStream input stream
76       * @param contentType content type
77       * @throws IllegalArgumentException if {@code inStream} is {@code null}
78       * @since 4.3
79       */
80      public InputStreamEntity(final InputStream inStream, final ContentType contentType) {
81          this(inStream, -1, contentType);
82      }
83  
84      /**
85       * @param inStream input stream
86       * @param length of the input stream, {@code -1} if unknown
87       * @param contentType for specifying the {@code Content-Type} header, may be {@code null}
88       * @throws IllegalArgumentException if {@code inStream} is {@code null}
89       * @since 4.2
90       */
91      public InputStreamEntity(final InputStream inStream, final long length, final ContentType contentType) {
92          super();
93          this.content = Args.notNull(inStream, "Source input stream");
94          this.length = length;
95          if (contentType != null) {
96              setContentType(contentType.toString());
97          }
98      }
99  
100     @Override
101     public boolean isRepeatable() {
102         return false;
103     }
104 
105     /**
106      * @return the content length or {@code -1} if unknown
107      */
108     @Override
109     public long getContentLength() {
110         return this.length;
111     }
112 
113     @Override
114     public InputStream getContent() throws IOException {
115         return this.content;
116     }
117 
118     /**
119      * Writes bytes from the {@code InputStream} this entity was constructed
120      * with to an {@code OutputStream}.  The content length
121      * determines how many bytes are written.  If the length is unknown ({@code -1}), the
122      * stream will be completely consumed (to the end of the stream).
123      *
124      */
125     @Override
126     public void writeTo(final OutputStream outStream) throws IOException {
127         Args.notNull(outStream, "Output stream");
128         final InputStream inStream = this.content;
129         try {
130             final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
131             int readLen;
132             if (this.length < 0) {
133                 // consume until EOF
134                 while ((readLen = inStream.read(buffer)) != -1) {
135                     outStream.write(buffer, 0, readLen);
136                 }
137             } else {
138                 // consume no more than length
139                 long remaining = this.length;
140                 while (remaining > 0) {
141                     readLen = inStream.read(buffer, 0, (int)Math.min(OUTPUT_BUFFER_SIZE, remaining));
142                     if (readLen == -1) {
143                         break;
144                     }
145                     outStream.write(buffer, 0, readLen);
146                     remaining -= readLen;
147                 }
148             }
149         } finally {
150             inStream.close();
151         }
152     }
153 
154     @Override
155     public boolean isStreaming() {
156         return true;
157     }
158 
159     @Override
160     public void close() throws IOException {
161         content.close();
162     }
163 
164 }