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