garyp       01/07/25 23:28:55

  Modified:    java/src/org/apache/xalan/serialize
                        WriterToUTF8Buffered.java
  Log:
  Fix bugzilla bug 2639 
(http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2639) where buffer was 
overflowing because the expansion of the Unicode characters into multiple UTF8 
characters was not being properly taken into account.  Many thanks to 
Gunnlaugur Thor Briem for his "multiply by three" trick.
  
  Revision  Changes    Path
  1.3       +25 -9     
xml-xalan/java/src/org/apache/xalan/serialize/WriterToUTF8Buffered.java
  
  Index: WriterToUTF8Buffered.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/serialize/WriterToUTF8Buffered.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- WriterToUTF8Buffered.java 2001/06/12 19:14:56     1.2
  +++ WriterToUTF8Buffered.java 2001/07/26 06:28:55     1.3
  @@ -135,20 +135,24 @@
     public void write(final int c) throws IOException
     {
   
  -    if (count >= buf.length)
  -    {
  -      flushBuffer();
  -    }
  -
  +  
       if (c < 0x80)
  +    {
  +      if (count >= buf.length)
  +        flushBuffer();
         buf[count++] = (byte) (c);
  +    }
       else if (c < 0x800)
       {
  +      if (count+1 >= buf.length)
  +        flushBuffer();
         buf[count++] = (byte) (0xc0 + (c >> 6));
         buf[count++] = (byte) (0x80 + (c & 0x3f));
       }
       else
       {
  +      if (count+2 >= buf.length)
  +        flushBuffer();
         buf[count++] = (byte) (0xe0 + (c >> 12));
         buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
         buf[count++] = (byte) (0x80 + (c & 0x3f));
  @@ -241,8 +245,14 @@
     public void write(final char chars[], final int start, final int length)
             throws java.io.IOException
     {
  +
  +    // We multiply the length by three since this is the maximum length
  +    // of the characters that we can put into the buffer.  It is possible
  +    // for each Unicode character to expand to three bytes.
  +
  +    int lengthx3 = (length << 1) + length;
   
  -    if (length >= buf.length)
  +    if (lengthx3 >= buf.length)
       {
   
         /* If the request length exceeds the size of the output buffer,
  @@ -254,7 +264,7 @@
         return;
       }
   
  -    if (length > buf.length - count)
  +    if (lengthx3 > buf.length - count)
       {
         flushBuffer();
       }
  @@ -293,8 +303,14 @@
     {
   
       final int length = s.length();
  +
  +    // We multiply the length by three since this is the maximum length
  +    // of the characters that we can put into the buffer.  It is possible
  +    // for each Unicode character to expand to three bytes.
  +
  +    int lengthx3 = (length << 1) + length;
   
  -    if (length >= buf.length)
  +    if (lengthx3 >= buf.length)
       {
   
         /* If the request length exceeds the size of the output buffer,
  @@ -306,7 +322,7 @@
         return;
       }
   
  -    if (length > buf.length - count)
  +    if (lengthx3 > buf.length - count)
       {
         flushBuffer();
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to