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