mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/StringEntity.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/StringEntity.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.entity;
    1.32 +
    1.33 +import java.io.ByteArrayInputStream;
    1.34 +import java.io.IOException;
    1.35 +import java.io.InputStream;
    1.36 +import java.io.OutputStream;
    1.37 +import java.io.UnsupportedEncodingException;
    1.38 +
    1.39 +import ch.boye.httpclientandroidlib.protocol.HTTP;
    1.40 +
    1.41 +/**
    1.42 + * A self contained, repeatable entity that obtains its content from
    1.43 + * a {@link String}.
    1.44 + *
    1.45 + * @since 4.0
    1.46 + */
    1.47 +public class StringEntity extends AbstractHttpEntity implements Cloneable {
    1.48 +
    1.49 +    protected final byte[] content;
    1.50 +
    1.51 +    /**
    1.52 +     * Creates a StringEntity with the specified content, mimetype and charset
    1.53 +     *
    1.54 +     * @param string content to be used. Not {@code null}.
    1.55 +     * @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"
    1.56 +     * @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"
    1.57 +     *
    1.58 +     * @since 4.1
    1.59 +     * @throws IllegalArgumentException if the string parameter is null
    1.60 +     */
    1.61 +    public StringEntity(final String string, String mimeType, String charset)
    1.62 +            throws UnsupportedEncodingException {
    1.63 +        super();
    1.64 +        if (string == null) {
    1.65 +            throw new IllegalArgumentException("Source string may not be null");
    1.66 +        }
    1.67 +        if (mimeType == null) {
    1.68 +            mimeType = HTTP.PLAIN_TEXT_TYPE;
    1.69 +        }
    1.70 +        if (charset == null) {
    1.71 +            charset = HTTP.DEFAULT_CONTENT_CHARSET;
    1.72 +        }
    1.73 +        this.content = string.getBytes(charset);
    1.74 +        setContentType(mimeType + HTTP.CHARSET_PARAM + charset);
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Creates a StringEntity with the specified content and charset.
    1.79 +     * <br/>
    1.80 +     * The mime type defaults to {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain".
    1.81 +     *
    1.82 +     * @param string content to be used. Not {@code null}.
    1.83 +     * @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"
    1.84 +     *
    1.85 +     * @throws IllegalArgumentException if the string parameter is null
    1.86 +     */
    1.87 +    public StringEntity(final String string, String charset)
    1.88 +            throws UnsupportedEncodingException {
    1.89 +        this(string, null, charset);
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * Creates a StringEntity with the specified content and charset.
    1.94 +     * <br/>
    1.95 +     * The charset defaults to {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1".
    1.96 +     * <br/>
    1.97 +     * The mime type defaults to {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain".
    1.98 +     *
    1.99 +     * @param string content to be used. Not {@code null}.
   1.100 +     *
   1.101 +     * @throws IllegalArgumentException if the string parameter is null
   1.102 +     */
   1.103 +    public StringEntity(final String string)
   1.104 +            throws UnsupportedEncodingException {
   1.105 +        this(string, null);
   1.106 +    }
   1.107 +
   1.108 +    public boolean isRepeatable() {
   1.109 +        return true;
   1.110 +    }
   1.111 +
   1.112 +    public long getContentLength() {
   1.113 +        return this.content.length;
   1.114 +    }
   1.115 +
   1.116 +    public InputStream getContent() throws IOException {
   1.117 +        return new ByteArrayInputStream(this.content);
   1.118 +    }
   1.119 +
   1.120 +    public void writeTo(final OutputStream outstream) throws IOException {
   1.121 +        if (outstream == null) {
   1.122 +            throw new IllegalArgumentException("Output stream may not be null");
   1.123 +        }
   1.124 +        outstream.write(this.content);
   1.125 +        outstream.flush();
   1.126 +    }
   1.127 +
   1.128 +    /**
   1.129 +     * Tells that this entity is not streaming.
   1.130 +     *
   1.131 +     * @return <code>false</code>
   1.132 +     */
   1.133 +    public boolean isStreaming() {
   1.134 +        return false;
   1.135 +    }
   1.136 +
   1.137 +    public Object clone() throws CloneNotSupportedException {
   1.138 +        return super.clone();
   1.139 +    }
   1.140 +
   1.141 +} // class StringEntity

mercurial