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      public long getContentLength() {
79          return -1;
80      }
81  
82      @Override
83      public int available() {
84          return Integer.MAX_VALUE;
85      }
86  
87      @Override
88      protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
89          Asserts.notNull(channel, "Channel");
90          channel.write(content);
91          if (!content.hasRemaining()) {
92              channel.endStream();
93          }
94      }
95  
96      @Override
97      public void failed(final Exception cause) {
98          if (exception.compareAndSet(null, cause)) {
99              releaseResources();
100         }
101     }
102 
103     public Exception getException() {
104         return exception.get();
105     }
106 
107     @Override
108     public void releaseResourcesInternal() {
109         this.content.clear();
110     }
111 
112 }