From 1b26b47caaafe89cb141322ccfb7b7a3f8f27911 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 9 May 2011 07:55:48 +0000 Subject: [PATCH] JETTY-1324 Tested not using CESU-8 instead of UTF-8 git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@3099 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + .../eclipse/jetty/server/HttpWriterTest.java | 17 +++ .../eclipse/jetty/util/Utf8Appendable.java | 142 ++++++++++++++++++ .../eclipse/jetty/util/Utf8StringBuffer.java | 102 +------------ .../eclipse/jetty/util/Utf8StringBuilder.java | 122 +-------------- 5 files changed, 176 insertions(+), 208 deletions(-) create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/Utf8Appendable.java diff --git a/VERSION.txt b/VERSION.txt index 3208728f609..d62e191d45e 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -16,6 +16,7 @@ jetty-7.4.1-SNAPSHOT + JETTY-1326 Invoker names not hashCode based + JETTY-1343 IllegalArgumentException for bad % encodings + JETTY-1347 Updated ServletHander javadoc + + JETTY-1324 Tested not using CESU-8 instead of UTF-8 jetty-7.4.0.v20110414 + 342504 Scanner Listener diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java index fb21cb536a3..fa86c3dcba0 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java @@ -15,6 +15,7 @@ import org.eclipse.jetty.io.ByteArrayEndPoint; import org.eclipse.jetty.io.SimpleBuffers; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.TypeUtil; +import org.eclipse.jetty.util.Utf8StringBuilder; import org.junit.Before; import org.junit.Test; @@ -93,6 +94,22 @@ public class HttpWriterTest _writer.write("How now \uFF22rown cow"); assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),_bytes.asArray()); } + + @Test + public void testNotCESU8() throws Exception + { + _writer.setCharacterEncoding(StringUtil.__UTF8); + String data="xxx\uD801\uDC00xxx"; + _writer.write(data); + assertEquals("787878F0909080787878",TypeUtil.toHexString(_bytes.asArray())); + assertArrayEquals(data.getBytes(StringUtil.__UTF8),_bytes.asArray()); + assertEquals(3+4+3,_bytes.length()); + + Utf8StringBuilder buf = new Utf8StringBuilder(); + buf.append(_bytes.asArray(),0,_bytes.length()); + assertEquals(data,buf.toString()); + + } @Test public void testMultiByteOverflowUTF8() throws Exception diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8Appendable.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8Appendable.java new file mode 100644 index 00000000000..92bdb5671bb --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8Appendable.java @@ -0,0 +1,142 @@ +package org.eclipse.jetty.util; + +import java.io.IOException; + +public abstract class Utf8Appendable +{ + protected final Appendable _appendable; + protected int _more; + protected int _bits; + + public Utf8Appendable(Appendable appendable) + { + _appendable=appendable; + } + + public abstract int length(); + + public void append(byte b) + { + try + { + appendByte(b); + } + catch(IOException e) + { + throw new RuntimeException(e); + } + } + + public void append(byte[] b,int offset, int length) + { + try + { + int end=offset+length; + for (int i=offset; imaxChars) + return false; + appendByte(b[i]); + } + return true; + } + catch(IOException e) + { + throw new RuntimeException(e); + } + } + + protected void appendByte(byte b) throws IOException + { + if (b>=0) + { + if (_more>0) + { + _appendable.append('?'); + _more=0; + _bits=0; + } + else + _appendable.append((char)(0x7f&b)); + } + else if (_more==0) + { + if ((b&0xc0)!=0xc0) + { + // 10xxxxxx + _appendable.append('?'); + _more=0; + _bits=0; + } + else + { + if ((b & 0xe0) == 0xc0) + { + //110xxxxx + _more=1; + _bits=b&0x1f; + } + else if ((b & 0xf0) == 0xe0) + { + //1110xxxx + _more=2; + _bits=b&0x0f; + } + else if ((b & 0xf8) == 0xf0) + { + //11110xxx + _more=3; + _bits=b&0x07; + } + else if ((b & 0xfc) == 0xf8) + { + //111110xx + _more=4; + _bits=b&0x03; + } + else if ((b & 0xfe) == 0xfc) + { + //1111110x + _more=5; + _bits=b&0x01; + } + else + { + throw new IllegalArgumentException("!utf8"); + } + } + } + else + { + if ((b&0xc0)==0xc0) + { // 11?????? + _appendable.append('?'); + _more=0; + _bits=0; + throw new IllegalArgumentException("!utf8"); + } + else + { + // 10xxxxxx + _bits=(_bits<<6)|(b&0x3f); + if (--_more==0) + _appendable.append(new String(Character.toChars(_bits))); + } + } + } + +} \ No newline at end of file diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuffer.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuffer.java index 19996078245..b6051d59435 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuffer.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuffer.java @@ -13,6 +13,8 @@ package org.eclipse.jetty.util; +import java.io.IOException; + /* ------------------------------------------------------------ */ /** UTF-8 StringBuffer. * @@ -27,110 +29,22 @@ package org.eclipse.jetty.util; * * This class is not synchronised and should probably be called Utf8StringBuilder */ -public class Utf8StringBuffer +public class Utf8StringBuffer extends Utf8Appendable { - StringBuffer _buffer; - int _more; - int _bits; + final StringBuffer _buffer; public Utf8StringBuffer() { - _buffer=new StringBuffer(); + super(new StringBuffer()); + _buffer=(StringBuffer)_appendable; } public Utf8StringBuffer(int capacity) { - _buffer=new StringBuffer(capacity); + super(new StringBuffer(capacity)); + _buffer=(StringBuffer)_appendable; } - public void append(byte[] b,int offset, int length) - { - int end=offset+length; - for (int i=offset; i=0) - { - if (_more>0) - { - _buffer.append('?'); - _more=0; - _bits=0; - } - else - _buffer.append((char)(0x7f&b)); - } - else if (_more==0) - { - if ((b&0xc0)!=0xc0) - { - // 10xxxxxx - _buffer.append('?'); - _more=0; - _bits=0; - } - else - - { - if ((b & 0xe0) == 0xc0) - { - //110xxxxx - _more=1; - _bits=b&0x1f; - } - else if ((b & 0xf0) == 0xe0) - { - //1110xxxx - _more=2; - _bits=b&0x0f; - } - else if ((b & 0xf8) == 0xf0) - { - //11110xxx - _more=3; - _bits=b&0x07; - } - else if ((b & 0xfc) == 0xf8) - { - //111110xx - _more=4; - _bits=b&0x03; - } - else if ((b & 0xfe) == 0xfc) - { - //1111110x - _more=5; - _bits=b&0x01; - } - else - { - throw new IllegalArgumentException("!utf8"); - } - - } - } - else - { - if ((b&0xc0)==0xc0) - { // 11?????? - _buffer.append('?'); - _more=0; - _bits=0; - throw new IllegalArgumentException("!utf8"); - } - else - { - // 10xxxxxx - _bits=(_bits<<6)|(b&0x3f); - if (--_more==0) - _buffer.append(Character.toChars(_bits)); - } - } - } - public int length() { return _buffer.length(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuilder.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuilder.java index da9086420e6..f270021d957 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuilder.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Utf8StringBuilder.java @@ -13,6 +13,8 @@ package org.eclipse.jetty.util; +import java.io.IOException; + /* ------------------------------------------------------------ */ /** UTF-8 StringBuilder. * @@ -26,128 +28,20 @@ package org.eclipse.jetty.util; * The UTF-8 code was inspired by http://javolution.org * */ -public class Utf8StringBuilder +public class Utf8StringBuilder extends Utf8Appendable { - StringBuilder _buffer; - int _more; - int _bits; + final StringBuilder _buffer; public Utf8StringBuilder() { - _buffer=new StringBuilder(); + super(new StringBuilder()); + _buffer=(StringBuilder)_appendable; } public Utf8StringBuilder(int capacity) { - _buffer=new StringBuilder(capacity); - } - - public void append(byte[] b,int offset, int length) - { - int end=offset+length; - for (int i=offset; imaxChars) - return false; - append(b[i]); - } - return true; - } - - public void append(byte b) - { - if (b>=0) - { - if (_more>0) - { - _buffer.append('?'); - _more=0; - _bits=0; - } - else - _buffer.append((char)(0x7f&b)); - } - else if (_more==0) - { - if ((b&0xc0)!=0xc0) - { - // 10xxxxxx - _buffer.append('?'); - _more=0; - _bits=0; - } - else - { - if ((b & 0xe0) == 0xc0) - { - //110xxxxx - _more=1; - _bits=b&0x1f; - } - else if ((b & 0xf0) == 0xe0) - { - //1110xxxx - _more=2; - _bits=b&0x0f; - } - else if ((b & 0xf8) == 0xf0) - { - //11110xxx - _more=3; - _bits=b&0x07; - } - else if ((b & 0xfc) == 0xf8) - { - //111110xx - _more=4; - _bits=b&0x03; - } - else if ((b & 0xfe) == 0xfc) - { - //1111110x - _more=5; - _bits=b&0x01; - } - else - { - throw new IllegalArgumentException("!utf8"); - } - } - } - else - { - if ((b&0xc0)==0xc0) - { // 11?????? - _buffer.append('?'); - _more=0; - _bits=0; - throw new IllegalArgumentException("!utf8"); - } - else - { - // 10xxxxxx - _bits=(_bits<<6)|(b&0x3f); - if (--_more==0) - { - // _buffer.append((char)_bits); - _buffer.append(Character.toChars(_bits)); - } - } - } + super(new StringBuilder(capacity)); + _buffer=(StringBuilder)_appendable; } public int length()