KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > methods > multipart > StringPart


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v 1.7.2.2 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.7.2.2 $
4  * $Date: 2004/02/22 18:21:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.methods.multipart;
33
34 import java.io.OutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import org.apache.commons.httpclient.HttpConstants;
37 import org.apache.commons.logging.Log;
38 import 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  */

50 public 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 JavaDoc DEFAULT_CONTENT_TYPE = "text/plain";
57
58     /** Default charset of string parameters*/
59     public static final String JavaDoc DEFAULT_CHARSET = "US-ASCII";
60
61     /** Default transfer encoding of string parameters*/
62     public static final String JavaDoc 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 JavaDoc 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 JavaDoc name, String JavaDoc value, String JavaDoc 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 JavaDoc("Value may not be null");
88         }
89         if (value.indexOf(0) != -1) {
90             // See RFC 2048, 2.8. "8bit Data"
91
throw new IllegalArgumentException JavaDoc("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 JavaDoc name, String JavaDoc 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 = HttpConstants.getContentBytes(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     protected void sendData(OutputStream JavaDoc out) throws IOException JavaDoc {
125         LOG.trace("enter sendData(OutputStream)");
126         out.write(getContent());
127     }
128     
129     /**
130      * Return the length of the data.
131      * @return The length of the data.
132      * @throws IOException If an IO problem occurs
133      * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData()
134      */

135     protected long lengthOfData() throws IOException JavaDoc {
136         LOG.trace("enter lengthOfData()");
137         return getContent().length;
138     }
139     
140     /* (non-Javadoc)
141      * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
142      */

143     public void setCharSet(String JavaDoc charSet) {
144         super.setCharSet(charSet);
145         this.content = null;
146     }
147
148 }
149
Popular Tags