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