db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r538311 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc: ClobAsciiStream.java ClobStreamControl.java
Date Tue, 15 May 2007 20:33:58 GMT
Author: djd
Date: Tue May 15 13:33:57 2007
New Revision: 538311

URL: http://svn.apache.org/viewvc?view=rev&rev=538311
Log:
DERBY-2618 (partial) Make ClobAsciiStream honour the contract for OutputStream.write(int)
by ignoring
top 24 bits. Optimize ClobAsciiStream by re-using an array rather than recreating one for
each write(byte[]) call.
Fix ClobStreamControl to correctly process a Derby UTF8 encoded stream.
Does not fix the issue, fixes are being worked on by others under related bugs.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobStreamControl.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java?view=diff&rev=538311&r1=538310&r2=538311
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java Tue May
15 13:33:57 2007
@@ -26,9 +26,17 @@
 import java.io.OutputStream;
 import java.io.Writer;
 
+/**
+ * Wrap a Writer as an OutputStream to support Clob.setAsciiStream().
+ * Any value written to the OutputStream is a valid ASCII value
+ * (0-255 from JDBC 4 spec appendix C2) thus this class simply
+ * passes the written values onto the Writer.
+ *
+ */
 final class ClobAsciiStream extends OutputStream {
 
-    private Writer writer;
+    private final Writer writer;
+    private final char[] buffer = new char[1024];
     
     ClobAsciiStream (Writer writer){
         this.writer = writer;
@@ -41,9 +49,6 @@
      * to the output stream. The byte to be written is the eight low-order bits
      * of the argument <code>b</code>. The 24 high-order bits of <code>b</code>
      * are ignored.
-     * <p>
-     * Subclasses of <code>OutputStream</code> must provide an 
-     * implementation for this method. 
      * 
      * @param b   the <code>byte</code>.
      * @exception IOException  if an I/O error occurs. In particular, 
@@ -51,7 +56,7 @@
      *             output stream has been closed.
      */
     public void write(int b) throws IOException {
-        writer.write (b);
+        writer.write(b & 0xff);
     }
 
     /**
@@ -84,10 +89,16 @@
      *             stream is closed.
      */
     public void write(byte[] b, int off, int len) throws IOException {
-        char [] c = new char [len];
-        for (int i = off; i < len; i++) {
-            c [i - off] = (char) b [i];
+        
+        while (len > 0)
+        {
+            int clen = Math.min(len, buffer.length);
+            for (int i = 0; i < clen; i++) {
+                buffer[i] = (char) b[off + i];
+            }
+            writer.write(buffer, 0, clen);
+            off += clen;
+            len -= clen;
         }
-        writer.write(c);
     }    
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobStreamControl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobStreamControl.java?view=diff&rev=538311&r1=538310&r2=538311
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobStreamControl.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobStreamControl.java Tue
May 15 13:33:57 2007
@@ -53,6 +53,8 @@
     /**
      * Finds the corresponding byte position for the given UTF-8 character
      * position, starting from the byte position <code>startPos</code>.
+     * See comments in SQLChar.readExternal for more notes on
+     * processing the UTF8 format.
      *
      * @param startPos start position in number of bytes
      * @param charPos character position
@@ -69,26 +71,31 @@
             if (c < 0)
                 return -1;
             charLength ++;
-             if ((c >= 0x0001) && (c <= 0x007F)) {
+            if ((c & 0x80) == 0x00) {
                 //found char of one byte width
                 streamLength++;
             }
-            else if (c > 0x07FF) {
-                //found char of three byte width
-                if (in.skip (2) < 2) {
+            else if ((c & 0x60) == 0x40) // we know the top bit is set here
+            {
+                //found char of two byte width
+                if (in.skip (1) != 1) {
                     //no second and third byte present
                     throw new UTFDataFormatException();
                 }
-                streamLength += 3;
-                break;
+                streamLength += 2;                
             }
-            else {
+            else if ((c & 0x70) == 0x60) // we know the top bit is set here
+            {
                 //found char of three byte width
-                if (in.skip (1) != 1) {
+                if (in.skip (2) != 2) {
                     //no second and third byte present
                     throw new UTFDataFormatException();
                 }
-                streamLength += 2;
+                streamLength += 3;
+            }
+            else
+            {
+                throw new UTFDataFormatException();
             }
         }
         



Mime
View raw message