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 org.apache.http.nio.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.io.UnsupportedEncodingException;
35 import java.nio.ByteBuffer;
36 import java.nio.charset.Charset;
37
38 import org.apache.http.entity.AbstractHttpEntity;
39 import org.apache.http.entity.ContentType;
40 import org.apache.http.nio.ContentEncoder;
41 import org.apache.http.nio.IOControl;
42 import org.apache.http.protocol.HTTP;
43 import org.apache.http.util.Args;
44
45
46
47
48
49
50
51
52 @SuppressWarnings("deprecation")
53 public class NStringEntity extends AbstractHttpEntity
54 implements HttpAsyncContentProducer, ProducingNHttpEntity {
55
56 private final byte[] b;
57 private final ByteBuffer buf;
58
59
60
61 @Deprecated
62 protected final byte[] content;
63
64
65
66 @Deprecated
67 protected final ByteBuffer buffer;
68
69
70
71
72
73
74
75
76
77
78
79
80 public NStringEntity(final String s, final ContentType contentType) {
81 Args.notNull(s, "Source string");
82 Charset charset = contentType != null ? contentType.getCharset() : null;
83 if (charset == null) {
84 charset = HTTP.DEF_CONTENT_CHARSET;
85 }
86 this.b = s.getBytes(charset);
87 this.buf = ByteBuffer.wrap(this.b);
88 this.content = b;
89 this.buffer = this.buf;
90 if (contentType != null) {
91 setContentType(contentType.toString());
92 }
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public NStringEntity(final String s, final String charset)
108 throws UnsupportedEncodingException {
109 this(s, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public NStringEntity(final String s, final Charset charset) {
125 this(s, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
126 }
127
128
129
130
131
132
133
134
135
136
137 public NStringEntity(final String s) throws UnsupportedEncodingException {
138 this(s, ContentType.DEFAULT_TEXT);
139 }
140
141 @Override
142 public boolean isRepeatable() {
143 return true;
144 }
145
146 @Override
147 public long getContentLength() {
148 return this.b.length;
149 }
150
151
152
153
154
155
156 @Override
157 public void close() {
158 this.buf.rewind();
159 }
160
161
162
163
164
165
166 @Override
167 @Deprecated
168 public void finish() {
169 close();
170 }
171
172 @Override
173 public void produceContent(
174 final ContentEncoder encoder, final IOControl ioControl) throws IOException {
175 encoder.write(this.buf);
176 if (!this.buf.hasRemaining()) {
177 encoder.complete();
178 }
179 }
180
181 @Override
182 public boolean isStreaming() {
183 return false;
184 }
185
186 @Override
187 public InputStream getContent() {
188 return new ByteArrayInputStream(this.b);
189 }
190
191 @Override
192 public void writeTo(final OutputStream outStream) throws IOException {
193 Args.notNull(outStream, "Output stream");
194 outStream.write(this.b);
195 outStream.flush();
196 }
197
198 }