db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r537291 - in /db/derby/code/trunk/java/client/org/apache/derby/client/am: ClobLocatorInputStream.java ClobLocatorOutputStream.java ClobLocatorReader.java ClobLocatorWriter.java
Date Fri, 11 May 2007 21:11:02 GMT
Author: kahatlen
Date: Fri May 11 14:11:01 2007
New Revision: 537291

URL: http://svn.apache.org/viewvc?view=rev&rev=537291
Log:
DERBY-2604: Implement Clob support for locators

Follow-up patch which fixed problems with string encoding and boundary
checking. Contributed by V. Narayanan.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorOutputStream.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java?view=diff&rev=537291&r1=537290&r2=537291
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
(original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
Fri May 11 14:11:01 2007
@@ -49,12 +49,12 @@
     /**
      * Connection used to read Clob from server.
      */
-    private Connection connection;
+    private final Connection connection;
     
     /**
      * The Clob to be accessed.
      */
-    private Clob clob;
+    private final Clob clob;
     
     /**
      * Current position in the underlying Clob.
@@ -100,7 +100,7 @@
      */
     public int read(byte[] b, int off, int len) throws IOException {
         if (len == 0) return 0;
-        if ((off < 0) || (len < 0) || (off+len > b.length)) {
+        if ((off < 0) || (len < 0) || (len > b.length - off)) {
             throw new IndexOutOfBoundsException();
         }
         
@@ -129,7 +129,7 @@
             String resultStr = connection.locatorProcedureCall().
                     clobGetSubString(clob.getLocator(),
                     currentPos, actualLength);
-            byte[] result = resultStr.getBytes();
+            byte[] result = getBytesFromString(resultStr);
             currentPos += result.length;
             return result;
         } catch (SqlException ex) {
@@ -137,5 +137,48 @@
             ioEx.initCause(ex);
             throw ioEx;
         }
+    }
+
+    /**
+     * Returns a <code>Byte</code> array from the
+     * <code>String</code> passed as Input.
+     *
+     * @param str the input <code>String</code>.
+     * @return The <code>Byte</code> corresponding
+     *         to the <code>String</code> that was
+     *         input.
+     */
+    private byte[] getBytesFromString(String str) {
+        //The Byte array that will hold the final
+        //converted Byte array that will be returned
+        //to the user
+        byte[] result = new byte[str.length()];
+
+        //Iterate through the String to
+        //Convert each character in the
+        //String
+        for (int i = 1; i <= str.length(); i++) {
+            //charAt function accpets a index that
+            //starts from 0 and ranges to length()-1
+            char oneChar = str.charAt(i-1);
+
+            if (oneChar <= 0xff) {
+                //Check if the value is lesser
+                //than maximum value that can
+                //be stored in a byte. If it is
+                //lesser store it directly in the
+                //byte array
+                result[i-1] = (byte)oneChar;
+            }
+            else {
+                //The value is greater than the
+                //maximum value that can be
+                //stored. Use the value 0x003f
+                //which corresponds to '?'
+                //signifying an unknown character
+                result[i-1] = 0x3f;
+            }
+        }
+        return result;
     }
 }

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorOutputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorOutputStream.java?view=diff&rev=537291&r1=537290&r2=537291
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorOutputStream.java
(original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorOutputStream.java
Fri May 11 14:11:01 2007
@@ -40,12 +40,12 @@
     /**
      * Connection used to read Clob from server.
      */
-    private Connection connection;
+    private final Connection connection;
     
     /**
      * The Clob to be accessed.
      */
-    private Clob clob;
+    private final Clob clob;
     
     /**
      * Current position in the underlying Clob.
@@ -101,7 +101,7 @@
     public void write(byte[] b, int off, int len) throws IOException {
         if (len == 0) return;
         if ((off < 0) || (off > b.length) || (len < 0) ||
-                (off+len > b.length) || (off+len < 0)) {
+                (len > b.length - off)) {
             throw new IndexOutOfBoundsException();
         }
         
@@ -132,7 +132,7 @@
      */
     private void writeBytes(byte[] b) throws IOException {
         try {
-            String clobStr = new String(b);
+            String clobStr = new String(b, "ISO-8859-1");
             connection.locatorProcedureCall().clobSetString
                     (clob.locator_, currentPos, b.length, clobStr);
             currentPos += b.length;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java?view=diff&rev=537291&r1=537290&r2=537291
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java Fri
May 11 14:11:01 2007
@@ -40,12 +40,12 @@
     /**
      * Connection used to read Clob from server.
      */
-    private Connection connection;
+    private final Connection connection;
     
     /**
      * The Clob to be accessed.
      */
-    private Clob clob;
+    private final Clob clob;
     
     /**
      * Current position in the underlying Clob.
@@ -124,7 +124,7 @@
     public int read(char[] c, int off, int len) throws IOException {
         checkClosed();
         if (len == 0) return 0;
-        if ((off < 0) || (len < 0) || (off+len > c.length)) {
+        if ((off < 0) || (len < 0) || (len > c.length - off)) {
             throw new IndexOutOfBoundsException();
         }
         
@@ -145,8 +145,6 @@
             return;
         }
         isClosed = true;
-        connection = null;
-        clob = null;
     }
     
     /**

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java?view=diff&rev=537291&r1=537290&r2=537291
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java Fri
May 11 14:11:01 2007
@@ -35,12 +35,12 @@
     /**
      * Connection used to read Clob from server.
      */
-    private Connection connection;
+    private final Connection connection;
     
     /**
      * The Clob to be accessed.
      */
-    private Clob clob;
+    private final Clob clob;
     
     /**
      * Current position in the underlying Clob.
@@ -86,8 +86,6 @@
             return;
         }
         isClosed = true;
-        connection = null;
-        clob = null;
     }
     
     /**
@@ -147,7 +145,7 @@
         checkClosed();
         if (len == 0) return;
         if ((off < 0) || (off > c.length) || (len < 0) ||
-                (off+len > c.length) || (off+len < 0)) {
+                (len > c.length - off)) {
             throw new IndexOutOfBoundsException();
         }
         writeCharacters(c, off, len);



Mime
View raw message