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.client5.http.entity.mime;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStreamReader;
33  import java.io.OutputStream;
34  import java.io.Reader;
35  import java.nio.charset.Charset;
36  import java.nio.charset.StandardCharsets;
37  
38  import org.apache.hc.core5.http.ContentType;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * Text body part backed by a byte array.
43   *
44   * @see org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
45   *
46   * @since 4.0
47   */
48  public class StringBody extends AbstractContentBody {
49  
50      private final byte[] content;
51  
52      /**
53       * @since 4.3
54       */
55      public StringBody(final String text, final ContentType contentType) {
56          super(contentType);
57          Args.notNull(text, "Text");
58          final Charset charset = contentType.getCharset();
59          this.content = text.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
60      }
61  
62      public Reader getReader() {
63          final Charset charset = getContentType().getCharset();
64          return new InputStreamReader(
65                  new ByteArrayInputStream(this.content),
66                  charset != null ? charset : StandardCharsets.US_ASCII);
67      }
68  
69      @Override
70      public void writeTo(final OutputStream out) throws IOException {
71          Args.notNull(out, "Output stream");
72          out.write(this.content);
73      }
74  
75      @Override
76      public long getContentLength() {
77          return this.content.length;
78      }
79  
80      @Override
81      public String getFilename() {
82          return null;
83      }
84  
85  }