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 tm...@apache.org on 2006/06/15 13:38:27 UTC

svn commit: r414562 - in /db/derby/code/trunk/java/client/org/apache/derby/client/net: Request.java UTF32BEEncodedInputStream.java

Author: tmnk
Date: Thu Jun 15 04:38:26 2006
New Revision: 414562

URL: http://svn.apache.org/viewvc?rev=414562&view=rev
Log:
- DERBY-1388 Integrate processing code of two writeScalarStream methods. - Patch by Tomohito Nakayama (tomonaka@basil.ocn.ne.jp)

Added:
    db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java   (with props)
Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/net/Request.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/Request.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/net/Request.java?rev=414562&r1=414561&r2=414562&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/Request.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/Request.java Thu Jun 15 04:38:26 2006
@@ -452,276 +452,16 @@
                                  int length,
                                  java.io.Reader r,
                                  boolean writeNullByte,
-                                 int parameterIndex) throws DisconnectException {
-        int leftToRead = length * 2; // the bytes to read
-        int extendedLengthByteCount = prepScalarStream(chained,
-                chainedWithSameCorrelator,
-                writeNullByte,
-                leftToRead);
-        int bytesToRead;
-
-        if (writeNullByte) {
-            bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - 1 - extendedLengthByteCount);
-        } else {
-            bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - extendedLengthByteCount);
-        }
-
-
-        if (netAgent_.netConnection_.getSecurityMechanism() != NetConfiguration.SECMEC_EUSRIDDTA &&
-                netAgent_.netConnection_.getSecurityMechanism() != NetConfiguration.SECMEC_EUSRPWDDTA) {
-            buildLengthAndCodePointForLob(codePoint,
-                    leftToRead,
-                    writeNullByte,
-                    extendedLengthByteCount);
-
-
-            // write the data
-            int charsRead = 0;
-            boolean haveHalfChar = false;
-            byte halfChar = (byte) 0x0;
-            char[] buf = new char[1 + 32765 / 2]; // enough for one DSS segment
-
-            do {
-                do {
-                    // fill in a half-character if we have one from a previous segment
-                    if (haveHalfChar) {
-                        bytes_[offset_++] = halfChar;
-                        bytesToRead--;
-                        leftToRead--;
-                        haveHalfChar = false;
-                    }
-
-                    if (bytesToRead == 1) {
-                        try {
-                            charsRead = r.read(buf, 0, 1);
-                        } catch (java.io.IOException e) {
-                            padScalarStreamForError(leftToRead, bytesToRead);
-                            // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                            netAgent_.accumulateReadException(new SqlException(
-                                netAgent_.logWriter_,
-                                new ClientMessageId(
-                                    SQLState.NET_IOEXCEPTION_ON_READ),
-                                new Integer(parameterIndex),
-                                e.getMessage(),
-                                e));
-
-                            return;
-                        }
-                        if (charsRead == -1) {
-                            padScalarStreamForError(leftToRead, bytesToRead);
-                            // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                            netAgent_.accumulateReadException(new SqlException(
-                                netAgent_.logWriter_,
-                                new ClientMessageId(SQLState.NET_PREMATURE_EOS),
-                                new Integer(parameterIndex)));
-
-                            return;
-                        }
-                        // set first half-char in buffer and save the other half for later
-                        bytes_[offset_++] = (byte) (buf[0] >>> 8);
-                        halfChar = (byte) buf[0];
-                        haveHalfChar = true;
-                        bytesToRead--;
-                        leftToRead--;
-                    } else if (bytesToRead != 0) {
-                        try {
-                            // read as many whole characters as needed to fill the buffer
-                            // half characters are handled above
-                            charsRead = r.read(buf, 0, bytesToRead / 2);
-                        } catch (java.io.IOException e) {
-                            padScalarStreamForError(leftToRead, bytesToRead);
-                            // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                            netAgent_.accumulateReadException(new SqlException(
-                                netAgent_.logWriter_,
-                                new ClientMessageId(
-                                    SQLState.NET_IOEXCEPTION_ON_READ),
-                                new Integer(parameterIndex),
-                                e.getMessage(),
-                                e));
-
-                            return;
-                        }
-
-                        if (charsRead == -1) {
-                            padScalarStreamForError(leftToRead, bytesToRead);
-                            // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                            netAgent_.accumulateReadException(new SqlException(
-                                netAgent_.logWriter_,
-                                new ClientMessageId(SQLState.NET_PREMATURE_EOS),
-                                new Integer(parameterIndex)));
-
-                            return;
-                        }
-                        for (int i = 0; i < charsRead; i++) {
-                            bytes_[offset_++] = (byte) (buf[i] >>> 8);
-                            bytes_[offset_++] = (byte) (buf[i]);
-                        }
-
-                        bytesToRead -= 2 * charsRead;
-                        leftToRead -= 2 * charsRead;
-                    }
-                } while (bytesToRead > 0);
-
-                bytesToRead = flushScalarStreamSegment(leftToRead, bytesToRead);
-            } while (leftToRead > 0);
-
-            // check to make sure that the specified length wasn't too small
-            try {
-                if (r.read() != -1) {
-                    netAgent_.accumulateReadException(new SqlException(
-                        netAgent_.logWriter_,
-                        new ClientMessageId(SQLState.NET_READER_LENGTH_TOO_SMALL),
-                        new Integer(parameterIndex)));
-                }
-            } catch (java.io.IOException e) {
-                netAgent_.accumulateReadException(new SqlException(
-                    netAgent_.logWriter_,
-                    new ClientMessageId(
-                        SQLState.NET_IOEXCEPTION_ON_STREAMLEN_VERIFICATION),
-                    new Integer(parameterIndex),
-                    e.getMessage(),
-                    e));
-            }
-        } else {  //data stream encryption
-
-            byte[] lengthAndCodepoint;
-            lengthAndCodepoint = buildLengthAndCodePointForEncryptedLob(codePoint,
-                    leftToRead,
-                    writeNullByte,
-                    extendedLengthByteCount);
-
-            // write the data
-            int charsRead = 0;
-            char[] buf = new char[leftToRead / 2];
-            byte[] clearedBytes = new byte[leftToRead];
-            int pos = 0;
-
-
-            do {
-                // fill in a half-character if we have one from a previous segment
-
-                try {
-                    // read as many whole characters as needed to fill the buffer
-                    // half characters are handled above
-                    charsRead = r.read(buf, 0, leftToRead / 2);
-                } catch (java.io.IOException e) {
-                    padScalarStreamForError(leftToRead, bytesToRead);
-                    // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                    netAgent_.accumulateReadException(new SqlException(
-                        netAgent_.logWriter_, 
-                        new ClientMessageId(SQLState.NET_IOEXCEPTION_ON_READ),
-                        new Integer(parameterIndex),
-                        e.getMessage(),
-                        e));
-
-                    return;
-                }
-
-                if (charsRead == -1) {
-                    padScalarStreamForError(leftToRead, bytesToRead);
-                    // set with SQLSTATE 01004: The value of a string was truncated when assigned to a host variable.
-                    netAgent_.accumulateReadException(new SqlException(
-                        netAgent_.logWriter_,
-                        new ClientMessageId(SQLState.NET_PREMATURE_EOS),
-                        new Integer(parameterIndex)));
-
-                    return;
-                }
-                for (int i = 0; i < charsRead; i++) {
-                    clearedBytes[pos++] = (byte) (buf[i] >>> 8);
-                    clearedBytes[pos++] = (byte) (buf[i]);
-                }
-
-                bytesToRead -= 2 * charsRead;
-                leftToRead -= 2 * charsRead;
-            } while (leftToRead > 0);
-
-            // check to make sure that the specified length wasn't too small
-            try {
-                if (r.read() != -1) {
-                    netAgent_.accumulateReadException(new SqlException(
-                        netAgent_.logWriter_,
-                        new ClientMessageId(SQLState.NET_READER_LENGTH_TOO_SMALL),
-                        new Integer(parameterIndex)));
-                }
-            } catch (java.io.IOException e) {
-                netAgent_.accumulateReadException(new SqlException(
-                    netAgent_.logWriter_, 
-                    new ClientMessageId(
-                        SQLState.NET_IOEXCEPTION_ON_STREAMLEN_VERIFICATION),
-                    new Integer(parameterIndex),
-                    e.getMessage(),
-                    e));
-            }
-
-            byte[] newClearedBytes = new byte[clearedBytes.length +
-                    lengthAndCodepoint.length];
-            System.arraycopy(lengthAndCodepoint, 0, newClearedBytes, 0,
-                    lengthAndCodepoint.length);
-            System.arraycopy(clearedBytes, 0, newClearedBytes, lengthAndCodepoint.length, clearedBytes.length);
-            int encryptedBytesLength = 0;
-            byte[] encryptedBytes = null;
-            try {
-                EncryptionManager encryptionMgr = netAgent_.netConnection_.getEncryptionManager();
-                byte[] publicKey = netAgent_.netConnection_.getTargetPublicKey();
-                encryptedBytes = encryptionMgr.encryptData(newClearedBytes,
-                        NetConfiguration.SECMEC_EUSRIDPWD,
-                        publicKey,
-                        publicKey);
-                encryptedBytesLength = encryptedBytes.length;
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
-
-            int sendingLength = bytes_.length - offset_;
-            if (encryptedBytesLength > bytes_.length - offset_) {
-
-
-                System.arraycopy(encryptedBytes, 0, bytes_, offset_, (bytes_.length - offset_));
-                offset_ = 32767;
-                try {
-                    sendBytes(netAgent_.getOutputStream());
-                } catch (java.io.IOException ioe) {
-                    netAgent_.throwCommunicationsFailure(ioe);
-                }
-            } else {
-                System.arraycopy(encryptedBytes, 0, bytes_, offset_, encryptedBytesLength);
-                offset_ = offset_ + encryptedBytes.length;
-            }
-
-            encryptedBytesLength = encryptedBytesLength - sendingLength;
-            while (encryptedBytesLength > 0) {
-                offset_ = 0;
-
-                if ((encryptedBytesLength - 32765) > 0) {
-                    bytes_[offset_++] = (byte) (0xff);
-                    bytes_[offset_++] = (byte) (0xff);
-                    System.arraycopy(encryptedBytes, sendingLength, bytes_, offset_, 32765);
-                    encryptedBytesLength -= 32765;
-                    sendingLength += 32765;
-                    offset_ = 32767;
-                    try {
-                        sendBytes(netAgent_.getOutputStream());
-                    } catch (java.io.IOException ioe) {
-                        netAgent_.throwCommunicationsFailure(ioe);
-                    }
-                } else {
-                    int leftlength = encryptedBytesLength + 2;
-                    bytes_[offset_++] = (byte) ((leftlength >>> 8) & 0xff);
-                    bytes_[offset_++] = (byte) (leftlength & 0xff);
-
-                    System.arraycopy(encryptedBytes, sendingLength, bytes_, offset_, encryptedBytesLength);
-
-                    offset_ += encryptedBytesLength;
-                    dssLengthLocation_ = offset_;
-                    encryptedBytesLength = 0;
-                }
-
-            }
-
-
-        }
+                                 int parameterIndex) throws DisconnectException, 
+															SqlException{
+        
+		writeScalarStream(chained,
+						  chainedWithSameCorrelator,
+						  codePoint,
+						  length * 2,
+						  new UTF32BEEncodedInputStream( r ),
+						  writeNullByte,
+						  parameterIndex);
     }
 
 

Added: db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java?rev=414562&view=auto
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java (added)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java Thu Jun 15 04:38:26 2006
@@ -0,0 +1,186 @@
+/*
+ 
+Derby - Class org.apache.derby.client.net.UTF32BEEncodedInputStream
+
+Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+package org.apache.derby.client.net;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.OutputStreamWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.derby.iapi.services.sanity.SanityManager;
+
+/**
+ *
+ * UTF32BEEncodedInputStream passes
+ * stream from Reader, which is stream of decoded style, 
+ * to user of this subclass of InputStream, which is stream of encoded style.
+ *
+ * The encoding of stream passed to user is limited to UTF32BE.
+ *
+ * This class will be used to pass stream, which is served as a Reader,
+ * as a InputStream of a UTF32BE encoding.
+ *
+ */
+public class UTF32BEEncodedInputStream extends InputStream {
+
+    private static final int BUFFERED_CHAR_LEN = 1024;
+	private static final ByteArrayInputStream suspendMarker = new ByteArrayInputStream( new byte[ 0 ] );
+
+    private Reader reader_;
+    private char[] decodedBuffer_;
+    
+    private OutputStreamWriter encodedStreamWriter_;
+    private PublicBufferOutputStream encodedOutputStream_;
+    
+    private ByteArrayInputStream encodedInputStream_;
+    
+    public UTF32BEEncodedInputStream(Reader reader) {
+	
+		reader_ = reader;
+		decodedBuffer_ = new char[BUFFERED_CHAR_LEN];
+
+		encodedOutputStream_ = new PublicBufferOutputStream( BUFFERED_CHAR_LEN * 2 );
+		
+		try{
+			encodedStreamWriter_ = new OutputStreamWriter(encodedOutputStream_,"UnicodeBigUnmarked");
+			
+		}catch(UnsupportedEncodingException e){
+			//never happens ...
+		}
+	
+		encodedInputStream_ = suspendMarker;
+	
+    }
+
+
+    private ByteArrayInputStream reEncode(Reader reader) 
+		throws IOException
+    {
+	
+		int count;
+		do{
+			count = reader.read(decodedBuffer_, 0, BUFFERED_CHAR_LEN);
+			
+		}while(count == 0);
+			
+		if(count < 0)
+			return null;
+	
+		encodedOutputStream_.reset();
+		encodedStreamWriter_.write(decodedBuffer_,0,count);
+		encodedStreamWriter_.flush();
+
+		int encodedLength = encodedOutputStream_.size();
+	
+		return new ByteArrayInputStream(encodedOutputStream_.getBuffer(),
+										0,
+										encodedLength);
+    }
+    
+    
+    public int available() 
+		throws IOException {
+		
+		if(encodedInputStream_ == suspendMarker)
+			encodedInputStream_ = reEncode(reader_);
+
+		if(encodedInputStream_ == null){
+			return 0;
+		}
+
+		return encodedInputStream_.available();
+	
+    }
+    
+
+    public void close() 
+		throws IOException {
+	
+		if(encodedInputStream_ != null ){
+			encodedInputStream_.close();
+			encodedInputStream_ = null;
+		}
+
+		if(reader_ != null ){
+			reader_.close();
+			reader_ = null;
+		}
+
+		if(encodedStreamWriter_ != null){
+			encodedStreamWriter_.close();
+			encodedStreamWriter_ = null;
+		}
+	
+    }
+    
+    
+    public int read() 
+		throws IOException {
+		
+		if(encodedInputStream_ == suspendMarker)
+			encodedInputStream_ = reEncode(reader_);
+
+		if(encodedInputStream_ == null){
+			return -1;
+		}
+	
+		int c = encodedInputStream_.read();
+
+		if(c > -1){
+			return c;
+	    
+		}else{
+			encodedInputStream_ = reEncode(reader_);
+	    
+			if(encodedInputStream_ == null){
+				return -1;
+			}
+	    
+			return encodedInputStream_.read();
+
+		}
+	
+    }
+    
+    
+    protected void finalize() throws IOException {
+		close();
+    }
+    
+    
+    static class PublicBufferOutputStream extends ByteArrayOutputStream{
+	
+		PublicBufferOutputStream(int size){
+			super(size);
+		}
+
+		public byte[] getBuffer(){
+			return buf;
+		}
+	
+    }
+
+}
+
+

Propchange: db/derby/code/trunk/java/client/org/apache/derby/client/net/UTF32BEEncodedInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native