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  package org.apache.hc.core5.http.nio.entity;
28  
29  import java.io.IOException;
30  import java.nio.CharBuffer;
31  import java.util.concurrent.atomic.AtomicReference;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.http.nio.StreamChannel;
35  import org.apache.hc.core5.util.Args;
36  import org.apache.hc.core5.util.Asserts;
37  
38  /**
39   * Basic {@link org.apache.hc.core5.http.nio.AsyncDataProducer} implementation that
40   * generates data stream from content of a string.
41   *
42   * @since 5.0
43   */
44  public class StringAsyncEntityProducer extends AbstractCharAsyncEntityProducer {
45  
46      private final CharBuffer content;
47      private final AtomicReference<Exception> exception;
48  
49      public StringAsyncEntityProducer(
50              final CharSequence content,
51              final int bufferSize,
52              final int fragmentSizeHint,
53              final ContentType contentType) {
54          super(bufferSize, fragmentSizeHint, contentType);
55          Args.notNull(content, "Content");
56          this.content = CharBuffer.wrap(content);
57          this.exception = new AtomicReference<>(null);
58      }
59  
60      public StringAsyncEntityProducer(final CharSequence content, final int bufferSize, final ContentType contentType) {
61          this(content, bufferSize, -1, contentType);
62      }
63  
64      public StringAsyncEntityProducer(final CharSequence content, final ContentType contentType) {
65          this(content, 4096, contentType);
66      }
67  
68      public StringAsyncEntityProducer(final CharSequence content) {
69          this(content, ContentType.TEXT_PLAIN);
70      }
71  
72      @Override
73      public boolean isRepeatable() {
74          return true;
75      }
76  
77      @Override
78      protected int availableData() {
79          return Integer.MAX_VALUE;
80      }
81  
82      @Override
83      protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
84          Asserts.notNull(channel, "Channel");
85          channel.write(content);
86          if (!content.hasRemaining()) {
87              channel.endStream();
88          }
89      }
90  
91      @Override
92      public void failed(final Exception cause) {
93          if (exception.compareAndSet(null, cause)) {
94              releaseResources();
95          }
96      }
97  
98      public Exception getException() {
99          return exception.get();
100     }
101 
102     @Override
103     public void releaseResources() {
104         this.content.clear();
105         super.releaseResources();
106     }
107 
108 }