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