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          Charset charset = contentType != null ? contentType.getCharset() : null;
66          if (charset == null) {
67              charset = StandardCharsets.ISO_8859_1;
68          }
69          this.content = string.getBytes(charset);
70      }
71  
72      public StringEntity(final String string, final ContentType contentType, final boolean chunked) {
73          this(string, contentType, null, chunked);
74      }
75  
76      public StringEntity(final String string, final ContentType contentType) {
77          this(string, contentType, null, false);
78      }
79  
80      /**
81       * Creates a StringEntity with the specified content and charset. The MIME type defaults
82       * to "text/plain".
83       *
84       * @param string content to be used. Not {@code null}.
85       * @param charset character set to be used. May be {@code null}, in which case the default
86       *   is {@link StandardCharsets#ISO_8859_1} is assumed
87       *
88       * @since 4.2
89       */
90      public StringEntity(final String string, final Charset charset) {
91          this(string, ContentType.TEXT_PLAIN.withCharset(charset));
92      }
93  
94      public StringEntity(final String string, final Charset charset, final boolean chunked) {
95          this(string, ContentType.TEXT_PLAIN.withCharset(charset), chunked);
96      }
97  
98      /**
99       * Creates a StringEntity with the specified content. The content type defaults to
100      * {@link ContentType#TEXT_PLAIN}.
101      *
102      * @param string content to be used. Not {@code null}.
103      *
104      * @throws IllegalArgumentException if the string parameter is null
105      */
106     public StringEntity(final String string) {
107         this(string, ContentType.DEFAULT_TEXT);
108     }
109 
110     @Override
111     public final boolean isRepeatable() {
112         return true;
113     }
114 
115     @Override
116     public final long getContentLength() {
117         return this.content.length;
118     }
119 
120     @Override
121     public final InputStream getContent() throws IOException {
122         return new ByteArrayInputStream(this.content);
123     }
124 
125     @Override
126     public final void writeTo(final OutputStream outStream) throws IOException {
127         Args.notNull(outStream, "Output stream");
128         outStream.write(this.content);
129         outStream.flush();
130     }
131 
132     @Override
133     public final boolean isStreaming() {
134         return false;
135     }
136 
137     @Override
138     public final void close() throws IOException {
139         // nothing to do
140     }
141 
142 }