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.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.nio.charset.Charset;
35  import java.nio.charset.StandardCharsets;
36  
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * A self contained, repeatable entity that obtains its content from a {@link String}.
44   *
45   * @since 4.0
46   */
47  @Contract(threading = ThreadingBehavior.IMMUTABLE)
48  public class StringEntity extends AbstractHttpEntity {
49  
50      private final byte[] content;
51  
52      /**
53       * Creates a StringEntity with the specified content and content type.
54       *
55       * @param string content to be used. Not {@code null}.
56       * @param contentType content type to be used. May be {@code null}, in which case the default
57       *   MIME type {@link ContentType#TEXT_PLAIN} is assumed.
58       *
59       * @since 5.0
60       */
61      public StringEntity(
62              final String string, final ContentType contentType, final String contentEncoding, final boolean chunked) {
63          super(contentType, contentEncoding, chunked);
64          Args.notNull(string, "Source string");
65          final Charset charset = ContentType.getCharset(contentType, StandardCharsets.ISO_8859_1);
66          this.content = string.getBytes(charset);
67      }
68  
69      public StringEntity(final String string, final ContentType contentType, final boolean chunked) {
70          this(string, contentType, null, chunked);
71      }
72  
73      public StringEntity(final String string, final ContentType contentType) {
74          this(string, contentType, null, false);
75      }
76  
77      /**
78       * Creates a StringEntity with the specified content and charset. The MIME type defaults
79       * to "text/plain".
80       *
81       * @param string content to be used. Not {@code null}.
82       * @param charset character set to be used. May be {@code null}, in which case the default
83       *   is {@link StandardCharsets#ISO_8859_1} is assumed
84       *
85       * @since 4.2
86       */
87      public StringEntity(final String string, final Charset charset) {
88          this(string, ContentType.TEXT_PLAIN.withCharset(charset));
89      }
90  
91      public StringEntity(final String string, final Charset charset, final boolean chunked) {
92          this(string, ContentType.TEXT_PLAIN.withCharset(charset), chunked);
93      }
94  
95      /**
96       * Creates a StringEntity with the specified content. The content type defaults to
97       * {@link ContentType#TEXT_PLAIN}.
98       *
99       * @param string content to be used. Not {@code null}.
100      *
101      * @throws IllegalArgumentException if the string parameter is null
102      */
103     public StringEntity(final String string) {
104         this(string, ContentType.DEFAULT_TEXT);
105     }
106 
107     @Override
108     public final boolean isRepeatable() {
109         return true;
110     }
111 
112     @Override
113     public final long getContentLength() {
114         return this.content.length;
115     }
116 
117     @Override
118     public final InputStream getContent() throws IOException {
119         return new ByteArrayInputStream(this.content);
120     }
121 
122     @Override
123     public final void writeTo(final OutputStream outStream) throws IOException {
124         Args.notNull(outStream, "Output stream");
125         outStream.write(this.content);
126         outStream.flush();
127     }
128 
129     @Override
130     public final boolean isStreaming() {
131         return false;
132     }
133 
134     @Override
135     public final void close() throws IOException {
136         // nothing to do
137     }
138 
139 }