Index: java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v retrieving revision 1.38 diff -u -r1.38 EntityEnclosingMethod.java --- java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 13 Jun 2004 20:22:19 -0000 1.38 +++ java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 2 Jul 2004 21:51:00 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v 1.38 2004/06/13 20:22:19 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v 1.38 2004/06/13 20:22:19 olegk Exp $ * $Revision: 1.38 $ * $Date: 2004/06/13 20:22:19 $ * @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.ChunkedOutputStream; import org.apache.commons.httpclient.Header; @@ -176,10 +177,15 @@ requestContentLength); this.requestStream = null; } else if (this.requestString != null) { - this.requestEntity = new StringRequestEntity( - requestString, - null, - getRequestCharSet()); + try { + this.requestEntity = new StringRequestEntity( + requestString, + null, + getRequestCharSet()); + } catch (UnsupportedEncodingException e) { + this.requestEntity = new StringRequestEntity( + requestString); + } } return this.requestEntity; Index: java/org/apache/commons/httpclient/methods/StringRequestEntity.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java,v retrieving revision 1.2 diff -u -r1.2 StringRequestEntity.java --- java/org/apache/commons/httpclient/methods/StringRequestEntity.java 13 May 2004 02:26:08 -0000 1.2 +++ java/org/apache/commons/httpclient/methods/StringRequestEntity.java 2 Jul 2004 21:51:01 -0000 @@ -32,8 +32,7 @@ import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; +import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.HeaderElement; import org.apache.commons.httpclient.NameValuePair; @@ -46,7 +45,7 @@ public class StringRequestEntity implements RequestEntity { /** The content */ - private String content; + private byte[] content; /** The charset */ private String charset; @@ -61,7 +60,13 @@ * @param content The content to set. */ public StringRequestEntity(String content) { - this(content, null, null); + super(); + if (content == null) { + throw new IllegalArgumentException("The content cannot be null"); + } + this.contentType = null; + this.charset = null; + this.content = content.getBytes(); } /** @@ -75,13 +80,13 @@ * content to bytes. If the content type does not contain a charset and charset is not null, * then the charset will be appended to the content type. */ - public StringRequestEntity(String content, String contentType, String charset) { + public StringRequestEntity(String content, String contentType, String charset) + throws UnsupportedEncodingException { super(); if (content == null) { throw new IllegalArgumentException("The content cannot be null"); } - this.content = content; this.contentType = contentType; this.charset = charset; @@ -103,6 +108,11 @@ this.contentType = contentType + "; charset=" + charset; } } + if (this.charset != null) { + this.content = content.getBytes(this.charset); + } else { + this.content = content.getBytes(); + } } /* (non-Javadoc) @@ -123,28 +133,33 @@ * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream) */ public void writeRequest(OutputStream out) throws IOException { - Writer writer = null; - if (this.charset != null) { - writer = new OutputStreamWriter(out, this.charset); - } else { - writer = new OutputStreamWriter(out); + if (out == null) { + throw new IllegalArgumentException("Output stream may not be null"); } - writer.write(content); - writer.flush(); + out.write(this.content); + out.flush(); } /** * @return The length of the content. */ public long getContentLength() { - return content.length(); + return this.content.length; } /** * @return Returns the content. */ public String getContent() { - return this.content; + if (this.charset != null) { + try { + return new String(this.content, this.charset); + } catch (UnsupportedEncodingException e) { + return new String(this.content); + } + } else { + return new String(this.content); + } } /** Index: test/org/apache/commons/httpclient/TestMethodCharEncoding.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v retrieving revision 1.9 diff -u -r1.9 TestMethodCharEncoding.java --- test/org/apache/commons/httpclient/TestMethodCharEncoding.java 12 May 2004 20:43:54 -0000 1.9 +++ test/org/apache/commons/httpclient/TestMethodCharEncoding.java 2 Jul 2004 21:51:02 -0000 @@ -277,4 +277,13 @@ assertEquals(ch_msg, params.get("ch")); } + public void testRequestEntityLength() throws IOException { + String s = constructString(SWISS_GERMAN_STUFF_UNICODE); + RequestEntity requestentity = + new StringRequestEntity(s, "text/plain", CHARSET_DEFAULT); + assertEquals( + s.getBytes(CHARSET_DEFAULT).length, + requestentity.getContentLength()); + } + }