From e66729308d589f8bdb935f735e827077263707ea Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 22 Feb 2011 03:41:39 +0000 Subject: [PATCH] 333481 Handle UCS-4 codepoints in decode and encode git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2811 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../org/eclipse/jetty/server/HttpWriter.java | 4 +- .../eclipse/jetty/server/HttpWriterTest.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpWriter.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpWriter.java index 47ba4357a09..5da2dd5ab2c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpWriter.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpWriter.java @@ -173,7 +173,6 @@ public class HttpWriter extends Writer else if(Character.isLowSurrogate((char)code)) { code = Character.toCodePoint((char)_surrogate, (char)code); // UCS-4 - _surrogate=0; // USED } // else UCS-2 else @@ -264,7 +263,8 @@ public class HttpWriter extends Writer { buffer[bytes++]=(byte)('?'); } - + + _surrogate=0; // USED if (bytes==buffer.length) { 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 c47897bf796..3dea7c9814e 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 @@ -216,6 +216,46 @@ public class HttpWriterTest assertArrayEquals(bytes,_bytes.asArray()); assertArrayEquals(baos.toByteArray(),_bytes.asArray()); } + + @Test + public void testMultiByteOverflowUTF16x2_2() throws Exception + { + _writer.setCharacterEncoding(StringUtil.__UTF8); + + final String singleByteStr = "a"; + int remainSize = 1; + final String multiByteDuplicateStr = "\uD842\uDF9F"; + int adjustSize = -2; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < HttpWriter.MAX_OUTPUT_CHARS + adjustSize; i++) + { + sb.append(singleByteStr); + } + sb.append(multiByteDuplicateStr); + for (int i = 0; i < remainSize; i++) + { + sb.append(singleByteStr); + } + String source = sb.toString(); + + byte[] bytes = source.getBytes("UTF-8"/* StringUtil.__UTF81 */); + _writer.write(source.toCharArray(),0,source.toCharArray().length); + + java.io.ByteArrayOutputStream baos = new +java.io.ByteArrayOutputStream(); + java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos/* +,StringUtil.__UTF8 */); + osw.write(source.toCharArray(),0,source.toCharArray().length); + osw.flush(); + + myReportBytes(bytes); + myReportBytes(baos.toByteArray()); + myReportBytes(_bytes.asArray()); + + assertArrayEquals(bytes,_bytes.asArray()); + assertArrayEquals(baos.toByteArray(),_bytes.asArray()); + } private void myReportBytes(byte[] bytes) throws Exception {