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.http.entity.mime.content;
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.io.UnsupportedEncodingException;
37  import java.nio.charset.Charset;
38  
39  import org.apache.http.Consts;
40  import org.apache.http.entity.ContentType;
41  import org.apache.http.entity.mime.MIME;
42  import org.apache.http.util.Args;
43  
44  /**
45   * Text body part backed by a byte array.
46   *
47   * @see org.apache.http.entity.mime.MultipartEntityBuilder
48   *
49   * @since 4.0
50   */
51  public class StringBody extends AbstractContentBody {
52  
53      private final byte[] content;
54  
55      /**
56       * @since 4.1
57       *
58       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
59       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
60       */
61      @Deprecated
62      public static StringBody create(
63              final String text,
64              final String mimeType,
65              final Charset charset) throws IllegalArgumentException {
66          try {
67              return new StringBody(text, mimeType, charset);
68          } catch (final UnsupportedEncodingException ex) {
69              throw new IllegalArgumentException("Charset " + charset + " is not supported", ex);
70          }
71      }
72  
73      /**
74       * @since 4.1
75       *
76       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
77       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
78       */
79      @Deprecated
80      public static StringBody create(
81              final String text, final Charset charset) throws IllegalArgumentException {
82          return create(text, null, charset);
83      }
84  
85      /**
86       * @since 4.1
87       *
88       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
89       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
90       */
91      @Deprecated
92      public static StringBody create(final String text) throws IllegalArgumentException {
93          return create(text, null, null);
94      }
95  
96      /**
97       * Create a StringBody from the specified text, MIME type and character set.
98       *
99       * @param text to be used for the body, not {@code null}
100      * @param mimeType the MIME type, not {@code null}
101      * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
102      * @throws UnsupportedEncodingException
103      * @throws IllegalArgumentException if the {@code text} parameter is null
104      *
105      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
106      *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
107      */
108     @Deprecated
109     public StringBody(
110             final String text,
111             final String mimeType,
112             final Charset charset) throws UnsupportedEncodingException {
113         this(text, ContentType.create(mimeType, charset != null ? charset : Consts.ASCII));
114     }
115 
116     /**
117      * Create a StringBody from the specified text and character set.
118      * The MIME type is set to "text/plain".
119      *
120      * @param text to be used for the body, not {@code null}
121      * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
122      * @throws UnsupportedEncodingException
123      * @throws IllegalArgumentException if the {@code text} parameter is null
124      *
125      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
126      *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
127      */
128     @Deprecated
129     public StringBody(final String text, final Charset charset) throws UnsupportedEncodingException {
130         this(text, "text/plain", charset);
131     }
132 
133     /**
134      * Create a StringBody from the specified text.
135      * The MIME type is set to "text/plain".
136      * The {@linkplain Consts#ASCII ASCII} charset is used.
137      *
138      * @param text to be used for the body, not {@code null}
139      * @throws UnsupportedEncodingException
140      * @throws IllegalArgumentException if the {@code text} parameter is null
141      *
142      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
143      *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
144      */
145     @Deprecated
146     public StringBody(final String text) throws UnsupportedEncodingException {
147         this(text, "text/plain", Consts.ASCII);
148     }
149 
150     /**
151      * @since 4.3
152      */
153     public StringBody(final String text, final ContentType contentType) {
154         super(contentType);
155         Args.notNull(text, "Text");
156         final Charset charset = contentType.getCharset();
157         this.content = text.getBytes(charset != null ? charset : Consts.ASCII);
158     }
159 
160     public Reader getReader() {
161         final Charset charset = getContentType().getCharset();
162         return new InputStreamReader(
163                 new ByteArrayInputStream(this.content),
164                 charset != null ? charset : Consts.ASCII);
165     }
166 
167     @Override
168     public void writeTo(final OutputStream out) throws IOException {
169         Args.notNull(out, "Output stream");
170         final InputStream in = new ByteArrayInputStream(this.content);
171         final byte[] tmp = new byte[4096];
172         int l;
173         while ((l = in.read(tmp)) != -1) {
174             out.write(tmp, 0, l);
175         }
176         out.flush();
177     }
178 
179     @Override
180     public String getTransferEncoding() {
181         return MIME.ENC_8BIT;
182     }
183 
184     @Override
185     public long getContentLength() {
186         return this.content.length;
187     }
188 
189     @Override
190     public String getFilename() {
191         return null;
192     }
193 
194 }