You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/04/27 00:00:25 UTC

svn commit: r397316 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/services/io/LimitReader.java iapi/types/ReaderToUTF8Stream.java impl/jdbc/EmbedPreparedStatement.java impl/jdbc/EmbedResultSet.java

Author: djd
Date: Wed Apr 26 15:00:22 2006
New Revision: 397316

URL: http://svn.apache.org/viewcvs?rev=397316&view=rev
Log:
DERBY-438 (partial) Consolidate code around converting application character streams (Readers)
to the internal stored form (modified UTF8). Pushed handling of LimitReader into ReaderToUTF8Stream
to avoid duplicated code and ReaderToUTF8Stream having to make assumptions about the caller setting
up the LimitReader correctly. Added comments and clarifications to LimitReader and ReaderToUTF8Stream.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/LimitReader.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/ReaderToUTF8Stream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/LimitReader.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/LimitReader.java?rev=397316&r1=397315&r2=397316&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/LimitReader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/LimitReader.java Wed Apr 26 15:00:22 2006
@@ -27,10 +27,10 @@
 	A  Reader that provides methods to limit the range that
 	can be read from the reader.
 */
-public class LimitReader extends Reader implements Limit 
+public final class LimitReader extends Reader implements Limit 
 {
-	protected int remainingBytes;
-	protected boolean limitInPlace;
+	private int remainingCharacters;
+	private boolean limitInPlace;
 	private	Reader	reader;
 
 	/**
@@ -49,13 +49,13 @@
 		if (!limitInPlace)
 			return reader.read();
 		
-		if (remainingBytes == 0)
+		if (remainingCharacters == 0)
 			return -1; // end of file
 
 		
 		int value = reader.read();
 		if (value >= 0)
-			remainingBytes--;
+			remainingCharacters--;
 		return value;
 
 	}
@@ -65,17 +65,17 @@
 		if (!limitInPlace)
 			return reader.read(c, off, len);
 
-		if (remainingBytes == 0)
+		if (remainingCharacters == 0)
 			return -1;
 
-		if (remainingBytes < len) 
+		if (remainingCharacters < len) 
 		{
-			len = remainingBytes; // end of file
+			len = remainingCharacters; // end of file
 		}
 
 		len = reader.read(c, off, len);
 		if (len >= 0)
-			remainingBytes -= len;
+			remainingCharacters -= len;
 		return len;
 	}
 
@@ -85,14 +85,14 @@
 		if (!limitInPlace)
 			return reader.skip(count);
 
-		if (remainingBytes == 0)
+		if (remainingCharacters == 0)
 			return 0; // end of file
 
-		if (remainingBytes < count)
-			count = remainingBytes;
+		if (remainingCharacters < count)
+			count = remainingCharacters;
 
 		count = reader.skip(count);
-		remainingBytes -= count;
+		remainingCharacters -= count;
 		return count;
 	}
 
@@ -104,8 +104,9 @@
 
 	/**
 		Set the limit of the stream that can be read. After this
-		call up to and including length bytes can be read from or skipped in
-		the stream. Any attempt to read more than length bytes will
+		call up to and including length characters can be read from
+        or skipped in the stream.
+        Any attempt to read more than length characters will
 		result in an EOFException
 
 		@exception IOException IOException from some underlying stream
@@ -114,7 +115,7 @@
 	*/
 	public void setLimit(int length) 
 	{
-		remainingBytes = length;
+		remainingCharacters = length;
 		limitInPlace = true;
 		return;
 	}
@@ -122,11 +123,11 @@
     /**
      * return limit of the stream that can be read without throwing
      * EOFException
-     * @return the remaining bytes left to be read from the stream
+     * @return the remaining characters left to be read from the stream
      */
     public final int getLimit()
     {
-        return remainingBytes;
+        return remainingCharacters;
     }
 
 	/**
@@ -138,9 +139,9 @@
 	*/
 	public int clearLimit() 
 	{
-		int leftOver = remainingBytes;
+		int leftOver = remainingCharacters;
 		limitInPlace = false;
-		remainingBytes = -1;
+		remainingCharacters = -1;
 		return leftOver;
 	}
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/ReaderToUTF8Stream.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/ReaderToUTF8Stream.java?rev=397316&r1=397315&r2=397316&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/ReaderToUTF8Stream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/ReaderToUTF8Stream.java Wed Apr 26 15:00:22 2006
@@ -36,7 +36,9 @@
 public final class ReaderToUTF8Stream
 	extends InputStream
 {
-
+    /**
+     * Application's reader wrapped in a LimitReader.
+     */
 	private LimitReader reader;
 
 	private byte[] buffer;
@@ -48,25 +50,34 @@
     // and converted to UTF8 format
     private final static int BUFSIZE = 32768;
     
-    // Number of characters to truncate from this stream
-    // The SQL standard allows for truncation of trailing spaces 
-    // for clobs,varchar,char.
+    /** Number of characters to truncate from this stream
+     The SQL standard allows for truncation of trailing spaces 
+     for clobs,varchar,char.
+     If zero, no characters are truncated.
+     */
     private int charsToTruncate;
-    private final char SPACE =' ';
+    private static final char SPACE =' ';
     
-    // this stream needs to fit into a column of colWidth
-    // if truncation error happens ,then the error message includes 
-    // information about the column width which is why this variable
-    // is needed.
-    private int colWidth;  
+    /**
+     * Length of the final value, after truncation if any,
+     * in characters.
+     this stream needs to fit into a column of colWidth
+     if truncation error happens ,then the error message includes 
+     information about the column width.
+    */
+    private final int valueLength; 
     
-	public ReaderToUTF8Stream(LimitReader reader,int length,int numCharsToTruncate)
+    /**
+     * Create a stream with truncation.
+     */
+ 	public ReaderToUTF8Stream(Reader appReader, int valueLength,int numCharsToTruncate)
 	{
-		this.reader = reader;
-		buffer = new byte[BUFSIZE];
-		blen = -1;
+        this.reader = new LimitReader(appReader);
+        reader.setLimit(valueLength);
+        buffer = new byte[BUFSIZE];
+        blen = -1;        
         this.charsToTruncate = numCharsToTruncate;
-        this.colWidth = length;
+        this.valueLength = valueLength;
 	}
 
     /**
@@ -218,7 +229,7 @@
         {
             reader.setLimit(charsToTruncate);
             int c = 0;
-            do
+            for (;;)
             {
                 c = reader.read();
                 
@@ -237,10 +248,9 @@
                             SQLState.LANG_STRING_TRUNCATION,
                             TypeId.CLOB_NAME, 
                             "XXXX", 
-                            String.valueOf(colWidth)));
+                            String.valueOf(valueLength)));
                 }
             }
-            while (c == SPACE);
         }
         
         int remainingBytes = reader.clearLimit();

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java?rev=397316&r1=397315&r2=397316&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java Wed Apr 26 15:00:22 2006
@@ -681,11 +681,10 @@
         int intLength = (int)length;		    
 
 	    try {
-           
-            LimitReader limitIn = new LimitReader(reader);
+
 			ParameterValueSet pvs = getParms();
             int usableLength = intLength;
-            ReaderToUTF8Stream utfIn = null;
+            int truncationLength = 0;
 
             // Currently long varchar does not allow for truncation of trailing
             // blanks.  
@@ -711,26 +710,18 @@
                 // length - colWidth has trailing blanks only
                 // we have used intLength into which the length variable had
                 // been cast to an int and stored  
-                if (colWidth < intLength)
+                if (intLength > colWidth) {                 
                     usableLength = colWidth;
-                
-                // keep information with the stream about how much data needs 
-                // to be truncated, and colWidth info to give proper truncation
-                // message
-                utfIn = new ReaderToUTF8Stream(
-                            limitIn, colWidth, intLength - usableLength);
-            }
-            else
-            {
-                utfIn = new ReaderToUTF8Stream(
-                            limitIn,usableLength,intLength - usableLength);
+                    truncationLength = intLength - usableLength;
+                }
             }
 
-            limitIn.setLimit(usableLength);
+            ReaderToUTF8Stream utfIn = new ReaderToUTF8Stream(
+                    reader, usableLength, truncationLength);
 
             /* JDBC is one-based, DBMS is zero-based */
             pvs.getParameterForSet(
-                parameterIndex - 1).setValue(utfIn,usableLength);
+                parameterIndex - 1).setValue(utfIn, usableLength);
 
 		} catch (StandardException t) {
 			throw EmbedResultSet.noStateChangeException(t);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java?rev=397316&r1=397315&r2=397316&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java Wed Apr 26 15:00:22 2006
@@ -2753,13 +2753,11 @@
                 throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE,
                         getColumnSQLType(columnIndex));
             } 
-
-            LimitReader limitIn = new LimitReader(reader);
-            
+           
             // length is +ve. at this point, all checks for negative
             // length has already been done
             int usableLength = (int) length;
-            ReaderToUTF8Stream utfIn = null;
+            int truncationLength = 0;
 
             // Currently long varchar does not allow for truncation of
             // trailing blanks.  For char and varchar types, current mechanism 
@@ -2781,20 +2779,15 @@
                 // usableLength is the length of the data from stream
                 // that can be used which is min(colWidth,length) provided
                 // length - colWidth has trailing blanks only
-                if (colWidth < length)
+                if (usableLength > colWidth) {                   
+                    truncationLength = usableLength - colWidth;
                     usableLength = colWidth;
-
-                // keep information with the stream about how much data
-                // needs to be truncated, and colWidth info to give proper
-                // truncation message
-                utfIn = new ReaderToUTF8Stream(
-                            limitIn, colWidth,     ((int) length) - usableLength);
-            } else {
-                utfIn = new ReaderToUTF8Stream(
-                            limitIn, usableLength, ((int)length) - usableLength);
+                }
             }
 
-            limitIn.setLimit(usableLength);
+            ReaderToUTF8Stream utfIn = new ReaderToUTF8Stream(
+                    reader, usableLength, truncationLength);
+            
             getDVDforColumnToBeUpdated(columnIndex, updateMethodName).setValue(
                     utfIn, (int) usableLength);
         } catch (StandardException t) {