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 an {@link InputStream}.
39   *
40   * @since 4.0
41   */
42  public class InputStreamEntity extends AbstractHttpEntity {
43  
44      private final InputStream content;
45      private final long length;
46  
47      public InputStreamEntity(
48              final InputStream inStream, final long length, final ContentType contentType, final String contentEncoding) {
49          super(contentType, contentEncoding);
50          this.content = Args.notNull(inStream, "Source input stream");
51          this.length = length;
52      }
53  
54      public InputStreamEntity(final InputStream inStream, final long length, final ContentType contentType) {
55          this(inStream, length, contentType, null);
56      }
57  
58      public InputStreamEntity(final InputStream inStream, final ContentType contentType) {
59          this(inStream, -1, contentType, null);
60      }
61  
62      @Override
63      public final boolean isRepeatable() {
64          return false;
65      }
66  
67      /**
68       * @return the content length or {@code -1} if unknown
69       */
70      @Override
71      public final long getContentLength() {
72          return this.length;
73      }
74  
75      @Override
76      public final InputStream getContent() throws IOException {
77          return this.content;
78      }
79  
80      /**
81       * Writes bytes from the {@code InputStream} this entity was constructed
82       * with to an {@code OutputStream}.  The content length
83       * determines how many bytes are written.  If the length is unknown ({@code -1}), the
84       * stream will be completely consumed (to the end of the stream).
85       *
86       */
87      @Override
88      public final void writeTo(final OutputStream outStream) throws IOException {
89          Args.notNull(outStream, "Output stream");
90          try (final InputStream inStream = this.content) {
91              final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
92              int readLen;
93              if (this.length < 0) {
94                  // consume until EOF
95                  while ((readLen = inStream.read(buffer)) != -1) {
96                      outStream.write(buffer, 0, readLen);
97                  }
98              } else {
99                  // consume no more than length
100                 long remaining = this.length;
101                 while (remaining > 0) {
102                     readLen = inStream.read(buffer, 0, (int) Math.min(OUTPUT_BUFFER_SIZE, remaining));
103                     if (readLen == -1) {
104                         break;
105                     }
106                     outStream.write(buffer, 0, readLen);
107                     remaining -= readLen;
108                 }
109             }
110         }
111     }
112 
113     @Override
114     public final boolean isStreaming() {
115         return true;
116     }
117 
118     @Override
119     public final void close() throws IOException {
120         content.close();
121     }
122 
123 }