1 31 32 package org.apache.commons.httpclient.methods.multipart; 33 34 import java.io.OutputStream ; 35 import java.io.IOException ; 36 import org.apache.commons.httpclient.HttpConstants; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 50 public class StringPart extends PartBase { 51 52 53 private static final Log LOG = LogFactory.getLog(StringPart.class); 54 55 56 public static final String DEFAULT_CONTENT_TYPE = "text/plain"; 57 58 59 public static final String DEFAULT_CHARSET = "US-ASCII"; 60 61 62 public static final String DEFAULT_TRANSFER_ENCODING = "8bit"; 63 64 65 private byte[] content; 66 67 68 private String value; 69 70 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 throw new IllegalArgumentException ("NULs may not be present in string parts"); 92 } 93 this.value = value; 94 } 95 96 102 public StringPart(String name, String value) { 103 this(name, value, null); 104 } 105 106 112 private byte[] getContent() { 113 if (content == null) { 114 content = HttpConstants.getContentBytes(value, getCharSet()); 115 } 116 return content; 117 } 118 119 124 protected void sendData(OutputStream out) throws IOException { 125 LOG.trace("enter sendData(OutputStream)"); 126 out.write(getContent()); 127 } 128 129 135 protected long lengthOfData() throws IOException { 136 LOG.trace("enter lengthOfData()"); 137 return getContent().length; 138 } 139 140 143 public void setCharSet(String charSet) { 144 super.setCharSet(charSet); 145 this.content = null; 146 } 147 148 } 149 | Popular Tags |