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.entity.mime.MultipartEntityBuilder;
44  import org.apache.http.util.Args;
45  
46  /**
47   * Text body part backed by a byte array.
48   *
49   * @see MultipartEntityBuilder
50   *
51   * @since 4.0
52   */
53  public class StringBody extends AbstractContentBody {
54  
55      private final byte[] content;
56  
57      /**
58       * @since 4.1
59       *
60       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
61       *   or {@link MultipartEntityBuilder}
62       */
63      @Deprecated
64      public static StringBody create(
65              final String text,
66              final String mimeType,
67              final Charset charset) throws IllegalArgumentException {
68          try {
69              return new StringBody(text, mimeType, charset);
70          } catch (final UnsupportedEncodingException ex) {
71              throw new IllegalArgumentException("Charset " + charset + " is not supported", ex);
72          }
73      }
74  
75      /**
76       * @since 4.1
77       *
78       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
79       *   or {@link MultipartEntityBuilder}
80       */
81      @Deprecated
82      public static StringBody create(
83              final String text, final Charset charset) throws IllegalArgumentException {
84          return create(text, null, charset);
85      }
86  
87      /**
88       * @since 4.1
89       *
90       * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
91       *   or {@link MultipartEntityBuilder}
92       */
93      @Deprecated
94      public static StringBody create(final String text) throws IllegalArgumentException {
95          return create(text, null, null);
96      }
97  
98      /**
99       * Create a StringBody from the specified text, mime type and character set.
100      *
101      * @param text to be used for the body, not {@code null}
102      * @param mimeType the mime type, not {@code null}
103      * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
104      * @throws UnsupportedEncodingException
105      * @throws IllegalArgumentException if the {@code text} parameter is null
106      *
107      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
108      *   or {@link MultipartEntityBuilder}
109      */
110     @Deprecated
111     public StringBody(
112             final String text,
113             final String mimeType,
114             final Charset charset) throws UnsupportedEncodingException {
115         this(text, ContentType.create(mimeType, charset));
116     }
117 
118     /**
119      * Create a StringBody from the specified text and character set.
120      * The mime type is set to "text/plain".
121      *
122      * @param text to be used for the body, not {@code null}
123      * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
124      * @throws UnsupportedEncodingException
125      * @throws IllegalArgumentException if the {@code text} parameter is null
126      *
127      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
128      *   or {@link MultipartEntityBuilder}
129      */
130     @Deprecated
131     public StringBody(final String text, final Charset charset) throws UnsupportedEncodingException {
132         this(text, "text/plain", charset);
133     }
134 
135     /**
136      * Create a StringBody from the specified text.
137      * The mime type is set to "text/plain".
138      * The {@linkplain Consts#ASCII ASCII} charset is used.
139      *
140      * @param text to be used for the body, not {@code null}
141      * @throws UnsupportedEncodingException
142      * @throws IllegalArgumentException if the {@code text} parameter is null
143      *
144      * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
145      *   or {@link MultipartEntityBuilder}
146      */
147     @Deprecated
148     public StringBody(final String text) throws UnsupportedEncodingException {
149         this(text, "text/plain", Consts.ASCII);
150     }
151 
152     /**
153      * @since 4.3
154      */
155     public StringBody(final String text, final ContentType contentType) {
156         super(contentType);
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 }