Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 */
28 package ch.boye.httpclientandroidlib.entity;
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;
36 import ch.boye.httpclientandroidlib.protocol.HTTP;
38 /**
39 * A self contained, repeatable entity that obtains its content from
40 * a {@link String}.
41 *
42 * @since 4.0
43 */
44 public class StringEntity extends AbstractHttpEntity implements Cloneable {
46 protected final byte[] content;
48 /**
49 * Creates a StringEntity with the specified content, mimetype and charset
50 *
51 * @param string content to be used. Not {@code null}.
52 * @param mimeType mime type to be used. May be {@code null}, in which case the default is {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain"
53 * @param charset character set to be used. May be {@code null}, in which case the default is {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1"
54 *
55 * @since 4.1
56 * @throws IllegalArgumentException if the string parameter is null
57 */
58 public StringEntity(final String string, String mimeType, String charset)
59 throws UnsupportedEncodingException {
60 super();
61 if (string == null) {
62 throw new IllegalArgumentException("Source string may not be null");
63 }
64 if (mimeType == null) {
65 mimeType = HTTP.PLAIN_TEXT_TYPE;
66 }
67 if (charset == null) {
68 charset = HTTP.DEFAULT_CONTENT_CHARSET;
69 }
70 this.content = string.getBytes(charset);
71 setContentType(mimeType + HTTP.CHARSET_PARAM + charset);
72 }
74 /**
75 * Creates a StringEntity with the specified content and charset.
76 * <br/>
77 * The mime type defaults to {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain".
78 *
79 * @param string content to be used. Not {@code null}.
80 * @param charset character set to be used. May be {@code null}, in which case the default is {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1"
81 *
82 * @throws IllegalArgumentException if the string parameter is null
83 */
84 public StringEntity(final String string, String charset)
85 throws UnsupportedEncodingException {
86 this(string, null, charset);
87 }
89 /**
90 * Creates a StringEntity with the specified content and charset.
91 * <br/>
92 * The charset defaults to {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1".
93 * <br/>
94 * The mime type defaults to {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain".
95 *
96 * @param string content to be used. Not {@code null}.
97 *
98 * @throws IllegalArgumentException if the string parameter is null
99 */
100 public StringEntity(final String string)
101 throws UnsupportedEncodingException {
102 this(string, null);
103 }
105 public boolean isRepeatable() {
106 return true;
107 }
109 public long getContentLength() {
110 return this.content.length;
111 }
113 public InputStream getContent() throws IOException {
114 return new ByteArrayInputStream(this.content);
115 }
117 public void writeTo(final OutputStream outstream) throws IOException {
118 if (outstream == null) {
119 throw new IllegalArgumentException("Output stream may not be null");
120 }
121 outstream.write(this.content);
122 outstream.flush();
123 }
125 /**
126 * Tells that this entity is not streaming.
127 *
128 * @return <code>false</code>
129 */
130 public boolean isStreaming() {
131 return false;
132 }
134 public Object clone() throws CloneNotSupportedException {
135 return super.clone();
136 }
138 } // class StringEntity