1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
45
46
47
48
49
50 public class StringBody extends AbstractContentBody {
51
52 private final byte[] content;
53
54
55
56
57
58
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
74
75
76
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
86
87
88
89
90 @Deprecated
91 public static StringBody create(final String text) throws IllegalArgumentException {
92 return create(text, null, null);
93 }
94
95
96
97
98
99
100
101
102
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
114
115
116
117
118
119
120
121
122 public StringBody(final String text, final Charset charset) throws UnsupportedEncodingException {
123 this(text, "text/plain", charset);
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public StringBody(final String text) throws UnsupportedEncodingException {
137 this(text, "text/plain", Consts.UTF_8);
138 }
139
140
141
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 }