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  public class StringAsyncEntityProducer extends AbstractCharAsyncEntityProducer {
39  
40      private final CharBuffer content;
41      private final AtomicReference<Exception> exception;
42  
43      public StringAsyncEntityProducer(
44              final CharSequence content,
45              final int bufferSize,
46              final int fragmentSizeHint,
47              final ContentType contentType) {
48          super(bufferSize, fragmentSizeHint, contentType);
49          Args.notNull(content, "Content");
50          this.content = CharBuffer.wrap(content);
51          this.exception = new AtomicReference<>(null);
52      }
53  
54      public StringAsyncEntityProducer(final CharSequence content, final int bufferSize, final ContentType contentType) {
55          this(content, bufferSize, -1, contentType);
56      }
57  
58      public StringAsyncEntityProducer(final CharSequence content, final ContentType contentType) {
59          this(content, 4096, contentType);
60      }
61  
62      public StringAsyncEntityProducer(final CharSequence content) {
63          this(content, ContentType.TEXT_PLAIN);
64      }
65  
66      @Override
67      public boolean isRepeatable() {
68          return true;
69      }
70  
71      @Override
72      public long getContentLength() {
73          return -1;
74      }
75  
76      @Override
77      public int available() {
78          return Integer.MAX_VALUE;
79      }
80  
81      @Override
82      protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
83          Asserts.notNull(channel, "Channel");
84          channel.write(content);
85          if (!content.hasRemaining()) {
86              channel.endStream();
87          }
88      }
89  
90      @Override
91      public void failed(final Exception cause) {
92          if (exception.compareAndSet(null, cause)) {
93              releaseResources();
94          }
95      }
96  
97      public Exception getException() {
98          return exception.get();
99      }
100 
101     @Override
102     public void releaseResourcesInternal() {
103         this.content.clear();
104     }
105 
106 }