1/*
2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v 1.11 2004/04/18 23:51:37 jsdever Exp $
3 * $Revision: 480424 $
4 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
5 *
6 * ====================================================================
7 *
8 *  Licensed to the Apache Software Foundation (ASF) under one or more
9 *  contributor license agreements.  See the NOTICE file distributed with
10 *  this work for additional information regarding copyright ownership.
11 *  The ASF licenses this file to You under the Apache License, Version 2.0
12 *  (the "License"); you may not use this file except in compliance with
13 *  the License.  You may obtain a copy of the License at
14 *
15 *      http://www.apache.org/licenses/LICENSE-2.0
16 *
17 *  Unless required by applicable law or agreed to in writing, software
18 *  distributed under the License is distributed on an "AS IS" BASIS,
19 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 *  See the License for the specific language governing permissions and
21 *  limitations under the License.
22 * ====================================================================
23 *
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation.  For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
28 *
29 */
30
31package com.android.internal.http.multipart;
32
33import java.io.OutputStream;
34import java.io.IOException;
35
36import org.apache.http.util.EncodingUtils;
37import org.apache.commons.logging.Log;
38import org.apache.commons.logging.LogFactory;
39
40/**
41 * Simple string parameter for a multipart post
42 *
43 * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
44 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
45 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
46 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
47 *
48 * @since 2.0
49 */
50public class StringPart extends PartBase {
51
52    /** Log object for this class. */
53    private static final Log LOG = LogFactory.getLog(StringPart.class);
54
55    /** Default content encoding of string parameters. */
56    public static final String DEFAULT_CONTENT_TYPE = "text/plain";
57
58    /** Default charset of string parameters*/
59    public static final String DEFAULT_CHARSET = "US-ASCII";
60
61    /** Default transfer encoding of string parameters*/
62    public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
63
64    /** Contents of this StringPart. */
65    private byte[] content;
66
67    /** The String value of this part. */
68    private String value;
69
70    /**
71     * Constructor.
72     *
73     * @param name The name of the part
74     * @param value the string to post
75     * @param charset the charset to be used to encode the string, if <code>null</code>
76     * the {@link #DEFAULT_CHARSET default} is used
77     */
78    public StringPart(String name, String value, String charset) {
79
80        super(
81            name,
82            DEFAULT_CONTENT_TYPE,
83            charset == null ? DEFAULT_CHARSET : charset,
84            DEFAULT_TRANSFER_ENCODING
85        );
86        if (value == null) {
87            throw new IllegalArgumentException("Value may not be null");
88        }
89        if (value.indexOf(0) != -1) {
90            // See RFC 2048, 2.8. "8bit Data"
91            throw new IllegalArgumentException("NULs may not be present in string parts");
92        }
93        this.value = value;
94    }
95
96    /**
97     * Constructor.
98     *
99     * @param name The name of the part
100     * @param value the string to post
101     */
102    public StringPart(String name, String value) {
103        this(name, value, null);
104    }
105
106    /**
107     * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
108     * after the part is created.
109     *
110     * @return the content in bytes
111     */
112    private byte[] getContent() {
113        if (content == null) {
114            content = EncodingUtils.getBytes(value, getCharSet());
115        }
116        return content;
117    }
118
119    /**
120     * Writes the data to the given OutputStream.
121     * @param out the OutputStream to write to
122     * @throws IOException if there is a write error
123     */
124    @Override
125    protected void sendData(OutputStream out) throws IOException {
126        LOG.trace("enter sendData(OutputStream)");
127        out.write(getContent());
128    }
129
130    /**
131     * Return the length of the data.
132     * @return The length of the data.
133     * @see Part#lengthOfData()
134     */
135    @Override
136    protected long lengthOfData() {
137        LOG.trace("enter lengthOfData()");
138        return getContent().length;
139    }
140
141    /* (non-Javadoc)
142     * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
143     */
144    @Override
145    public void setCharSet(String charSet) {
146        super.setCharSet(charSet);
147        this.content = null;
148    }
149
150}
151