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.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import org.apache.hc.core5.annotation.Contract;
37  import org.apache.hc.core5.annotation.ThreadingBehavior;
38  import org.apache.hc.core5.http.ContentType;
39  import org.apache.hc.core5.io.IOCallback;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * Entity that delegates the process of content generation to a {@link IOCallback}
44   * with {@link OutputStream} as output sink.
45   *
46   * @since 4.0
47   */
48  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
49  public final class EntityTemplate extends AbstractHttpEntity {
50  
51      private final long contentLength;
52      private final IOCallback<OutputStream> callback;
53  
54      public EntityTemplate(
55              final long contentLength, final ContentType contentType, final String contentEncoding,
56              final IOCallback<OutputStream> callback) {
57          super(contentType, contentEncoding);
58          this.contentLength = contentLength;
59          this.callback = Args.notNull(callback, "I/O callback");
60      }
61  
62      @Override
63      public long getContentLength() {
64          return contentLength;
65      }
66  
67      @Override
68      public InputStream getContent() throws IOException {
69          final ByteArrayOutputStream buf = new ByteArrayOutputStream();
70          writeTo(buf);
71          return new ByteArrayInputStream(buf.toByteArray());
72      }
73  
74      @Override
75      public boolean isRepeatable() {
76          return true;
77      }
78  
79      @Override
80      public void writeTo(final OutputStream outStream) throws IOException {
81          Args.notNull(outStream, "Output stream");
82          this.callback.execute(outStream);
83      }
84  
85      @Override
86      public boolean isStreaming() {
87          return false;
88      }
89  
90      @Override
91      public void close() throws IOException {
92      }
93  
94  }