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.ByteBuffer;
31  import java.nio.CharBuffer;
32  import java.nio.charset.Charset;
33  import java.nio.charset.StandardCharsets;
34  import java.util.Set;
35  import java.util.concurrent.atomic.AtomicReference;
36  
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
39  import org.apache.hc.core5.http.nio.DataStreamChannel;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * @since 5.0
44   */
45  public class BasicAsyncEntityProducer implements AsyncEntityProducer {
46  
47      private final ByteBuffer bytebuf;
48      private final int length;
49      private final ContentType contentType;
50      private final AtomicReference<Exception> exception;
51  
52      public BasicAsyncEntityProducer(final byte[] content, final ContentType contentType) {
53          Args.notNull(content, "Content");
54          this.bytebuf = ByteBuffer.wrap(content);
55          this.length = this.bytebuf.remaining();
56          this.contentType = contentType;
57          this.exception = new AtomicReference<>(null);
58      }
59  
60      public BasicAsyncEntityProducer(final byte[] content) {
61          this(content, ContentType.APPLICATION_OCTET_STREAM);
62      }
63  
64      public BasicAsyncEntityProducer(final CharSequence content, final ContentType contentType) {
65          Args.notNull(content, "Content");
66          this.contentType = contentType;
67          Charset charset = contentType != null ? contentType.getCharset() : null;
68          if (charset == null) {
69              charset = StandardCharsets.US_ASCII;
70          }
71          this.bytebuf = charset.encode(CharBuffer.wrap(content));
72          this.length = this.bytebuf.remaining();
73          this.exception = new AtomicReference<>(null);
74      }
75  
76      public BasicAsyncEntityProducer(final CharSequence content) {
77          this(content, ContentType.TEXT_PLAIN);
78      }
79  
80      @Override
81      public boolean isRepeatable() {
82          return true;
83      }
84  
85      @Override
86      public final String getContentType() {
87          return contentType != null ? contentType.toString() : null;
88      }
89  
90      @Override
91      public long getContentLength() {
92          return length;
93      }
94  
95      @Override
96      public int available() {
97          return Integer.MAX_VALUE;
98      }
99  
100     @Override
101     public String getContentEncoding() {
102         return null;
103     }
104 
105     @Override
106     public boolean isChunked() {
107         return false;
108     }
109 
110     @Override
111     public Set<String> getTrailerNames() {
112         return null;
113     }
114 
115     @Override
116     public final void produce(final DataStreamChannel channel) throws IOException {
117         channel.write(bytebuf);
118         if (!bytebuf.hasRemaining()) {
119             channel.endStream();
120         }
121     }
122 
123     @Override
124     public final void failed(final Exception cause) {
125         if (exception.compareAndSet(null, cause)) {
126             releaseResources();
127         }
128     }
129 
130     public final Exception getException() {
131         return exception.get();
132     }
133 
134     @Override
135     public void releaseResources() {
136         bytebuf.clear();
137     }
138 
139 }