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