Author: remy.maucherat(a)jboss.com
Date: 2012-01-12 06:27:47 -0500 (Thu, 12 Jan 2012)
New Revision: 1919
Modified:
trunk/java/org/apache/tomcat/util/buf/C2BConverter.java
trunk/webapps/docs/changelog.xml
Log:
JBWEB-221: Add leftovers to the char encoder.
Modified: trunk/java/org/apache/tomcat/util/buf/C2BConverter.java
===================================================================
--- trunk/java/org/apache/tomcat/util/buf/C2BConverter.java 2012-01-10 08:46:41 UTC (rev
1918)
+++ trunk/java/org/apache/tomcat/util/buf/C2BConverter.java 2012-01-12 11:27:47 UTC (rev
1919)
@@ -41,6 +41,11 @@
protected CharBuffer cb = null;
/**
+ * Leftover buffer used for multi-characters characters.
+ */
+ protected CharBuffer leftovers = null;
+
+ /**
* Create an encoder for the specified charset.
*/
public C2BConverter(String charset)
@@ -51,6 +56,8 @@
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(charset);
}
+ char[] left = new char[4];
+ leftovers = CharBuffer.wrap(left);
}
/**
@@ -58,8 +65,13 @@
*/
public void recycle() {
encoder.reset();
+ leftovers.position(0);
}
+ public boolean isUndeflow() {
+ return (leftovers.position() > 0);
+ }
+
/**
* Convert the given characters to bytes.
*
@@ -86,8 +98,26 @@
cb.limit(cc.getEnd());
cb.position(cc.getStart());
}
+ CoderResult result = null;
+ // Parse leftover if any are present
+ if (leftovers.position() > 0) {
+ int pos = bb.position();
+ // Loop until one char is encoded or there is a encoder error
+ do {
+ leftovers.put((char) cc.substract());
+ leftovers.flip();
+ result = encoder.encode(leftovers, bb, false);
+ leftovers.position(leftovers.limit());
+ leftovers.limit(leftovers.array().length);
+ } while (result.isUnderflow() && (bb.position() == pos));
+ if (result.isError() || result.isMalformed()) {
+ result.throwException();
+ }
+ cb.position(cc.getStart());
+ leftovers.position(0);
+ }
// Do the decoding and get the results into the byte chunk and the char chunk
- CoderResult result = encoder.encode(cb, bb, false);
+ result = encoder.encode(cb, bb, false);
if (result.isError() || result.isMalformed()) {
result.throwException();
} else if (result.isOverflow()) {
@@ -98,6 +128,12 @@
// Propagate current positions to the byte chunk and char chunk
bc.setEnd(bb.position());
cc.setOffset(cb.position());
+ // Put leftovers in the leftovers char buffer
+ if (cc.getLength() > 0) {
+ leftovers.limit(leftovers.array().length);
+ leftovers.position(cc.getLength());
+ cc.substract(leftovers.array(), 0, cc.getLength());
+ }
}
}
Modified: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2012-01-10 08:46:41 UTC (rev 1918)
+++ trunk/webapps/docs/changelog.xml 2012-01-12 11:27:47 UTC (rev 1919)
@@ -16,6 +16,17 @@
<body>
+<section name="JBoss Web 7.0.9.Final (remm)">
+ <subsection name="Coyote">
+ <changelog>
+ <fix>
+ <jira>221</jira>: Add leftovers to the character encoder to
accomodate
+ multi-chars characters. (remm)
+ </fix>
+ </changelog>
+ </subsection>
+</section>
+
<section name="JBoss Web 7.0.8.Final (remm)">
<subsection name="Catalina">
<changelog>
Show replies by date