Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 62085 invoked from network); 28 Apr 2005 19:34:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Apr 2005 19:34:11 -0000 Received: (qmail 23896 invoked by uid 500); 28 Apr 2005 19:35:20 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 23871 invoked by uid 500); 28 Apr 2005 19:35:20 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 23856 invoked by uid 500); 28 Apr 2005 19:35:20 -0000 Delivered-To: apmail-incubator-derby-cvs@incubator.apache.org Received: (qmail 23850 invoked by uid 99); 28 Apr 2005 19:35:20 -0000 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 28 Apr 2005 12:35:19 -0700 Received: (qmail 39219 invoked by uid 65534); 28 Apr 2005 19:06:07 -0000 Message-ID: <20050428190607.39216.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: svn commit: r165178 [23/26] - in /incubator/derby/code/trunk: ./ java/client/ java/client/org/ java/client/org/apache/ java/client/org/apache/derby/ java/client/org/apache/derby/client/ java/client/org/apache/derby/client/am/ java/client/org/apache/derby/client/net/ java/client/org/apache/derby/client/resources/ java/client/org/apache/derby/jdbc/ tools/ant/properties/ Date: Thu, 28 Apr 2005 19:05:45 -0000 To: derby-cvs@incubator.apache.org From: bandaram@apache.org X-Mailer: svnmailer-1.0.0-dev X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/derby/code/trunk/java/client/org/apache/derby/client/net/R= eply.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/o= rg/apache/derby/client/net/Reply.java?rev=3D165178&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/derby/code/trunk/java/client/org/apache/derby/client/net/Repl= y=2Ejava (added) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/net/Repl= y=2Ejava Thu Apr 28 12:05:42 2005 @@ -0,0 +1,1501 @@ +/* + + Derby - Class org.apache.derby.client.net.Reply + + Copyright (c) 2001, 2005 The Apache Software Foundation or its licensor= s, where 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 org.apache.derby.client.am.SqlState; +import org.apache.derby.client.am.SqlException; +import org.apache.derby.client.am.SignedBinary; +import java.io.ByteArrayOutputStream; + +public class Reply +{ + protected org.apache.derby.client.am.Agent agent_; + protected NetAgent netAgent_; //cheat-link to (NetAgent) agent_ + + private CcsidManager ccsidManager_; + protected final static int DEFAULT_BUFFER_SIZE =3D 32767; + protected byte[] buffer_; + protected int pos_; + protected int count_; + + private int topDdmCollectionStack_; + private final static int MAX_MARKS_NESTING =3D 10; + private int[] ddmCollectionLenStack_; + private int ddmScalarLen_; // a value of -1 -> streamed ddm -> length un= known + private final static int EMPTY_STACK =3D -1; + + protected boolean ensuredLengthForDecryption_ =3D false; // A layer leng= ths have already been ensured in decrypt method. + protected byte[] longBufferForDecryption_ =3D null; + protected int longPosForDecryption_ =3D 0; + protected byte[] longValueForDecryption_ =3D null; + protected int longCountForDecryption_ =3D 0; + + protected int dssLength_; + protected boolean dssIsContinued_; + private boolean dssIsChainedWithSameID_; + private boolean dssIsChainedWithDiffID_; + protected int dssCorrelationID_; + + protected int peekedLength_ =3D 0; + protected int peekedCodePoint_ =3D END_OF_COLLECTION; // saves the pe= eked codept + private int peekedNumOfExtendedLenBytes_ =3D 0; + private int currentPos_ =3D 0; + + public final static int END_OF_COLLECTION =3D -1; + public final static int END_OF_SAME_ID_CHAIN =3D -2; + Reply (NetAgent netAgent, int bufferSize) + { + buffer_ =3D new byte[bufferSize]; + agent_ =3D netAgent_ =3D netAgent; + ccsidManager_ =3D netAgent.targetCcsidManager_; + ddmCollectionLenStack_ =3D new int[Reply.MAX_MARKS_NESTING]; + initialize (); + } + + final void initialize () + { + pos_ =3D 0; + count_ =3D 0; + topDdmCollectionStack_ =3D Reply.EMPTY_STACK; + for (int i =3D 0; i < Reply.MAX_MARKS_NESTING; i++) { + ddmCollectionLenStack_[i] =3D 0; + } + ddmScalarLen_ =3D 0; + dssLength_ =3D 0; + dssIsContinued_ =3D false; + dssIsChainedWithSameID_ =3D false; + dssIsChainedWithDiffID_ =3D false; + dssCorrelationID_ =3D 1; + } + + final int getDdmLength() + { + return ddmScalarLen_; + } + + // This is a helper method which shifts the buffered bytes from + // wherever they are in the current buffer to the beginning of + // different buffer (note these buffers could be the same). + // State information is updated as needed after the shift. + private final void shiftBuffer (byte[] destinationBuffer) + { + // calculate the size of the data in the current buffer. + int sz =3D count_ - pos_; + + // copy this data to the new buffer starting at position 0. + System.arraycopy (buffer_, pos_, destinationBuffer, 0, sz); + + // update the state information for data in the new buffer. + pos_ =3D 0; + count_ =3D sz; + + // replace the old buffer with the new buffer. + buffer_ =3D destinationBuffer; + } + + // This method makes sure there is enough room in the buffer + // for a certain number of bytes. This method will allocate + // a new buffer if needed and shift the bytes in the current buffer + // to make ensure space is available for a fill. Right now + // this method will shift bytes as needed to make sure there is + // as much room as possible in the buffer before trying to + // do the read. The idea is to try to have space to get as much data as= possible + // if we need to do a read on the socket's stream. + protected final void ensureSpaceInBufferForFill (int desiredSpace) + { + // calculate the total unused space in the buffer. + // this includes any space at the end of the buffer and any free + // space at the beginning resulting from bytes already read. + int currentAvailableSpace =3D (buffer_.length - count_) + pos_; + + // check to see if there is enough free space. + if (currentAvailableSpace < desiredSpace) { + + // there is not enough free space so we need more storage. + // we are going to double the buffer unless that happens to still be= too small. + // if more than double the buffer is needed, use the smallest amount= over this as possible. + int doubleBufferSize =3D (2 * buffer_.length); + + int minumNewBufferSize =3D (desiredSpace - currentAvailableSpace) + = buffer_.length; + int newsz =3D minumNewBufferSize <=3D doubleBufferSize ? doubleBuffe= rSize : minumNewBufferSize; + + byte[] newBuffer =3D new byte[newsz]; + + // shift everything from the old buffer to the new buffer + shiftBuffer (newBuffer); + } + else { + + // there is enough free space in the buffer but let's make sure it i= s all at the end. + // this is also important because if we are going to do a read, it w= ould be nice + // to get as much data as possible and making room at the end if the= buffer helps to + // ensure this. + if (pos_ !=3D 0) { + shiftBuffer (buffer_); + } + } + } + + // This method will attempt to read a minimum number of bytes + // from the underlying stream. This method will keep trying to + // read bytes until it has obtained at least the minimum number. + // Now returns the total bytes read for decryption, use to return void. + protected int fill (int minimumBytesNeeded) throws org.apache.derby.clie= nt.am.DisconnectException + { + // make sure that there is enough space in the buffer to hold + // the minimum number of bytes needed. + ensureSpaceInBufferForFill (minimumBytesNeeded); + + // read until the minimum number of bytes needed is now in the buffer. + // hopefully the read method will return as many bytes as it can. + int totalBytesRead =3D 0; + int actualBytesRead =3D 0; + do { + try { + // oops, we shouldn't expose the agent's input stream here, collaps= e this into a read method on the agent + actualBytesRead =3D netAgent_.getInputStream().read (buffer_, coun= t_, buffer_.length - count_); + } + catch (java.io.IOException ioe) { + netAgent_.throwCommunicationsFailure ("Reply.fill()", + "InputStream.read()", + ioe.getMessage(), + "*"); + } + finally { + if (agent_.loggingEnabled()) + ((NetLogWriter) netAgent_.logWriter_).traceProtocolFlow (buffer_, + count_, + actualBytesRead, + NetLogWriter.TYPE_TRACE_RECEIVE, + "Reply", + "fill", + 2); // tracepoint + } + count_ +=3D actualBytesRead; + totalBytesRead +=3D actualBytesRead; + + } + while ((totalBytesRead < minimumBytesNeeded) && (actualBytesRead !=3D = -1)); + + if (actualBytesRead =3D=3D -1) { + if (totalBytesRead < minimumBytesNeeded) { + netAgent_.throwCommunicationsFailure ("Reply.fill()", + "InputStream.read()", + "insufficient data", + "*"); + } + } + return totalBytesRead; + } + + // Make sure a certain amount of Layer A data is in the buffer. + // The data will be in the buffer after this method is called. + // Now returns the total bytes read for decryption, use to return void. + protected final int ensureALayerDataInBuffer (int desiredDataSize) throw= s org.apache.derby.client.am.DisconnectException + { + int totalBytesRead =3D 0; + // calulate the the number of bytes in the buffer. + int avail =3D count_ - pos_; + + // read more bytes off the network if the data is not in the buffer al= ready. + if (avail < desiredDataSize) { + totalBytesRead =3D fill (desiredDataSize - avail); + } + return totalBytesRead; + } + + protected final void ensureBLayerDataInBuffer (int desiredDataSize) thro= ws org.apache.derby.client.am.DisconnectException + { + if (dssIsContinued_ && (desiredDataSize > dssLength_)) { + int continueDssHeaderCount =3D + (((desiredDataSize - dssLength_) / 32767) + 1); + ensureALayerDataInBuffer (desiredDataSize + (continueDssHeaderCount = * 2)); + compressBLayerData (continueDssHeaderCount); + return; + } + ensureALayerDataInBuffer (desiredDataSize); + } + + // this will probably never be called. + // it is included here in the highly unlikely event that a reply object + // exceeds 32K. for opimization purposes, we should consider + // removing this. removing this should be ok since we handle most + // big stuff returned from the server (qrydta's for example) by + // copying out the data into some other storage. any extended dss header + // info will be removed in the copying process. + private final void compressBLayerData (int continueDssHeaderCount) throw= s org.apache.derby.client.am.DisconnectException + { + int tempPos =3D 0; + + // jump to the last continuation header. + for (int i =3D 0; i < continueDssHeaderCount; i++) { + // the first may be less than the size of a full dss + if (i =3D=3D 0) { + // only jump by the number of bytes remaining in the current dss + tempPos =3D pos_ + dssLength_; + } + else { + // all other jumps are for a full continued dss + tempPos +=3D 32767; + } + } + + // for each of the dss headers to remove, + // read out the continuation header and increment the dss length by the + // size of the conitnation bytes, then shift the continuation data as= needed. + int shiftSize =3D 0; + int bytesToShift =3D 0; + int continueHeaderLength =3D 0; + int newDssLength =3D 0; + for (int i =3D 0; i < continueDssHeaderCount; i++) { + + continueHeaderLength =3D ((buffer_[tempPos]&0xFF) << 8) + + ((buffer_[tempPos + 1]&0xFF) << 0); + + if (i =3D=3D 0) { + // if this is the last one (farthest down stream and first to stri= p out) + + if ((continueHeaderLength & 0x8000) =3D=3D 0x8000) { + // the last dss header is again continued + continueHeaderLength =3D 32767; + dssIsContinued_ =3D true; + } + else { + // the last dss header was not contiued so update continue state= flag + dssIsContinued_ =3D false; + } + // the very first shift size is 2 + shiftSize =3D 2; + } + else { + // already removed the last header so make sure the chaining flag = is on + if ((continueHeaderLength & 0x8000) =3D=3D 0x8000) { + continueHeaderLength =3D 32767; + } + else { + // this is a syntax error but not really certain which one. + // for now pick 0x02 which is Dss header Length does not match t= he number + // of bytes of data found. + doSyntaxrmSemantics (CodePoint.SYNERRCD_DSS_LENGTH_BYTE_NUMBER_M= ISMATCH); + } + // increase the shift size by 2 + shiftSize +=3D 2; + } + + // it is a syntax error if the dss continuation is less than or equa= l to two + if (continueHeaderLength <=3D 2) { + doSyntaxrmSemantics (CodePoint.SYNERRCD_DSS_CONT_LESS_OR_EQUAL_2); + } + + newDssLength +=3D (continueHeaderLength - 2); + + // calculate the number of bytes to shift + if (i !=3D (continueDssHeaderCount - 1)) + bytesToShift =3D 32767; + else + bytesToShift =3D dssLength_; + + tempPos -=3D (shiftSize - 1); + // perform the compress + for (int j =3D 0; j < bytesToShift; j++) { + buffer_[tempPos + shiftSize] =3D buffer_[tempPos]; + tempPos--; + } + tempPos +=3D (shiftSize + 1); + } + // reposition the start of the data after the final dss shift. + pos_ =3D tempPos; + dssLength_ =3D dssLength_ + newDssLength; + } + + protected final void readDssHeader () throws org.apache.derby.client.am.= DisconnectException + { + int correlationID =3D 0; + int nextCorrelationID =3D 0; + ensureALayerDataInBuffer (6); + + // read out the dss length + dssLength_ =3D + ((buffer_[pos_++]&0xFF) << 8) + + ((buffer_[pos_++]&0xFF) << 0); + + // Remember the old dss length for decryption only. + int oldDssLength =3D dssLength_; + + // check for the continuation bit and update length as needed. + if ((dssLength_ & 0x8000) =3D=3D 0x8000) { + dssLength_ =3D 32767; + dssIsContinued_ =3D true; + } + else { + dssIsContinued_ =3D false; + } + + if (dssLength_ < 6) + doSyntaxrmSemantics (CodePoint.SYNERRCD_DSS_LESS_THAN_6); + + // If the GDS id is not valid, or + // if the reply is not an RPYDSS nor + // a OBJDSS, then throw an exception. + if ((buffer_[pos_++]&0xFF) !=3D 0xd0) + doSyntaxrmSemantics (CodePoint.SYNERRCD_CBYTE_NOT_D0); + + int gdsFormatter =3D buffer_[pos_++]&0xFF; + if (((gdsFormatter & 0x02) !=3D 0x02) + &&((gdsFormatter & 0x03) !=3D 0x03) + &&((gdsFormatter & 0x04) !=3D 0x04)) { + doSyntaxrmSemantics (CodePoint.SYNERRCD_FBYTE_NOT_SUPPORTED); + } + + // Determine if the current DSS is chained with the + // next DSS, with the same or different request ID. + if ((gdsFormatter & 0x40) =3D=3D 0x40) { // on indicates structure = chained to next structure + if ((gdsFormatter & 0x10) =3D=3D 0x10) { + dssIsChainedWithSameID_ =3D true; + dssIsChainedWithDiffID_ =3D false; + nextCorrelationID =3D dssCorrelationID_; + } + else { + dssIsChainedWithSameID_ =3D false; + dssIsChainedWithDiffID_ =3D true; + nextCorrelationID =3D dssCorrelationID_ + 1; + } + } + else { + // chaining bit not b'1', make sure DSSFMT bit3 not b'1' + if ((gdsFormatter & 0x10) =3D=3D 0x10) { // Next DSS can not have = same correlator + doSyntaxrmSemantics(CodePoint.SYNERRCD_CHAIN_OFF_SAME_NEXT_CORRELA= TOR); + } + + // chaining bit not b'1', make sure no error continuation + if ((gdsFormatter & 0x20) =3D=3D 0x20) { // must be 'do not continue= on error' + doSyntaxrmSemantics(CodePoint.SYNERRCD_CHAIN_OFF_ERROR_CONTINUE); + } + + dssIsChainedWithSameID_ =3D false; + dssIsChainedWithDiffID_ =3D false; + nextCorrelationID =3D 1; + } + + correlationID =3D + ((buffer_[pos_++]&0xFF) << 8) + + ((buffer_[pos_++]&0xFF) << 0); + + // corrid must be the one expected or a -1 which gets returned in some= error cases. + if ((correlationID !=3D dssCorrelationID_) && (correlationID !=3D 0xFF= FF)){ + doSyntaxrmSemantics (CodePoint.SYNERRCD_INVALID_CORRELATOR); + } + else { + dssCorrelationID_ =3D nextCorrelationID; + } + dssLength_ -=3D 6; + if ((gdsFormatter & 0x04) =3D=3D 0x04 ) + decryptData (gdsFormatter, oldDssLength); //we have to decrypt dat= a here because + //we need the decrypted = codepoint. If + //Data is very long > 32= 767, we have to + //get all the data first= because decrypt + //piece by piece doesn't= work. + } + + + + private final void decryptData(int gdsFormatter, int oldDssLength) throw= s org.apache.derby.client.am.DisconnectException + { + boolean readHeader; + + if(dssLength_ =3D=3D 32761) { + ByteArrayOutputStream baos; + int copySize =3D 0; + + baos =3D new ByteArrayOutputStream (); + + // set the amount to read for the first segment + copySize =3D dssLength_; // note: has already been adjusted for = headers + + do { + // determine if a continuation header needs to be read after t= he data + if (dssIsContinued_) + readHeader =3D true; + else + readHeader =3D false; + + // read the segment + ensureALayerDataInBuffer (copySize); + adjustLengths (copySize); + baos.write (buffer_, pos_, copySize); + pos_ +=3D copySize; + + // read the continuation header, if necessary + if (readHeader) + readDSSContinuationHeader (); + + copySize =3D dssLength_; + } + while (readHeader =3D=3D true); + byte[] cipherBytes =3D baos.toByteArray(); + byte[] clearedByte =3D null; + try{ + clearedByte =3D netAgent_.netConnection_.getEncryptionManager(= )=2EdecryptData( + cipherBytes, + NetConfiguration.SECMEC_EUSRIDPWD, + netAgent_.netConnection_.getTargetPublicKey(), + netAgent_.netConnection_.getTargetPublicKey()); + } + catch (SqlException e) + { + //throw new SqlException (agent_.logWriter_, "error in decrypt= ing data"); + } + + //The decrypted data is for one codepoint only. We need to save = the data follows this codepoint + longBufferForDecryption_ =3D new byte[buffer_.length - pos_]; + longPosForDecryption_ =3D 0; + count_ =3D count_ - pos_; + longCountForDecryption_ =3D count_; + System.arraycopy(buffer_,pos_,longBufferForDecryption_,0,buffer_= .length - pos_); + + //copy the clear data to buffer_ + if (clearedByte.length >=3D32767) + System.arraycopy(clearedByte,0,buffer_,0,32767); + else + System.arraycopy(clearedByte,0,buffer_,0,clearedByte.length); + + pos_ =3D 0; + dssLength_ =3D buffer_.length; + + int lobLength =3D 0; + if(clearedByte.length > 32767) { //for extended length, length = is the 4 bytes that follow codepoint + lobLength =3D ( (clearedByte[4] & 0xFF) << 24) + + ( (clearedByte[5] & 0xFF) << 16) + + ( (clearedByte[6] & 0xFF) << 8) + + ( (clearedByte[7] & 0xFF) << 0); + longValueForDecryption_ =3D new byte[lobLength]; + System.arraycopy (clearedByte, 8, longValueForDecryption_, 0, = clearedByte.length-8); + } + else { + lobLength =3D ( (clearedByte[0] & 0xFF) << 8) + + ( (clearedByte[1] & 0xFF) << 0); + longValueForDecryption_ =3D new byte[lobLength-4]; + System.arraycopy (clearedByte, 4, longValueForDecryption_, 0, = clearedByte.length-4); + } + } + + else{ + int bytesRead =3D ensureALayerDataInBuffer (dssLength_); //we nee= d to get back all the data here, and then decrypt + if (bytesRead > 0) //we ensuredALayerDAtaInBuffer here and set the= flag to true, so we don't need do this again later + ensuredLengthForDecryption_ =3D true; + byte[] encryptedByte =3D new byte[dssLength_]; + System.arraycopy(buffer_,pos_,encryptedByte,0,dssLength_); + byte[] array1 =3D new byte[pos_]; + System.arraycopy(buffer_,0,array1,0,pos_); //save the data before= encrypted data in array1 + byte[] array3 =3D new byte[buffer_.length - dssLength_ - pos_]; + System.arraycopy(buffer_,pos_+dssLength_,array3,0,buffer_.length -= dssLength_ - pos_); //save the data follows encrypted data in array3 + byte[] clearedByte =3D null; + try{ + clearedByte =3D netAgent_.netConnection_.getEncryptionManager().= decryptData( + encryptedByte, + NetConfiguration.SECMEC_EUSRIDPWD, + netAgent_.netConnection_.getTargetPublicKey(), + netAgent_.netConnection_.getTargetPublicKey()); + } + catch (SqlException e) + { + //throw new SqlException (agent_.logWriter_, "error in decryptin= g data"); + } + dssLength_ -=3D (encryptedByte.length - clearedByte.length); + byte[] buffer =3D new byte[array1.length + clearedByte.length + ar= ray3.length]; + System.arraycopy(array1,0,buffer,0,array1.length); + System.arraycopy(clearedByte,0,buffer,array1.length,clearedByte.le= ngth); + System.arraycopy(array3,0,buffer,array1.length+clearedByte.length,= array3.length); + buffer_ =3D buffer; + int oldCount =3D count_; + count_ =3D count_ - (encryptedByte.length - clearedByte.length); + if (((clearedByte[2]& 0xff) << 8) + ((clearedByte[3] & 0xff) << 0)= =3D=3D 0x146c) { + int firstLobLength =3D ( (clearedByte[0] & 0xFF) << 8) + + ( (clearedByte[1] & 0xFF) << 0); + + boolean flag =3D false; + if (gdsFormatter =3D=3D 0x54) + flag =3D true; + if(flag){ + if (oldCount - oldDssLength < 6) { + int totalBytesRead =3D fill(6); //sometimes the 2nd EXTDTA = doesn't come back, need to fetch again to get it + if (totalBytesRead > 0) { + longBufferForDecryption_ =3D new byte[totalBytesRead]; + longPosForDecryption_ =3D 0; + System.arraycopy(buffer_, pos_ + firstLobLength, longBuff= erForDecryption_, 0, + totalBytesRead); + } + + } + else { + longBufferForDecryption_ =3D new byte[count_ - pos_ - first= LobLength]; + longPosForDecryption_ =3D 0; + System.arraycopy(buffer_, pos_ + firstLobLength, longBuffer= ForDecryption_, 0, + longBufferForDecryption_.length); + + } + } //end if(flag) + int lobLength =3D ( (clearedByte[0] & 0xFF) << 8) + + ( (clearedByte[1] & 0xFF) << 0) - 4; + + longValueForDecryption_ =3D new byte[lobLength]; + + System.arraycopy (clearedByte, 4, longValueForDecryption_, 0, cle= aredByte.length - 4); //copy the decrypted lob value (excluded length an d= codepoint) to longValue_ + } + else if (((clearedByte[2]& 0xff) << 8) + ((clearedByte[3] & 0xff) = << 0) =3D=3D 0x241B){ + int length =3D ( (clearedByte[0] & 0xFF) << 8) + + ( (clearedByte[1] & 0xFF) << 0); + boolean noData =3D false; + if (clearedByte[4] =3D=3D -1 && clearedByte[5] =3D=3D -1) + noData =3D true; //there is no data, no need to do the copy + if (!noData) { + if (length =3D=3D 32776) { + length =3D ( (clearedByte[4] & 0xFF) << 24) + + ( (clearedByte[5] & 0xFF) << 16) + + ( (clearedByte[6] & 0xFF) << 8) + + ( (clearedByte[7] & 0xFF) << 0); + longValueForDecryption_ =3D new byte[length]; + System.arraycopy (clearedByte, 8, longValueForDecryption_,= 0, + clearedByte.length - 8); + longCountForDecryption_ =3D count_ - (pos_ + length + 8); + longBufferForDecryption_ =3D new byte[buffer_.length - pos= _ - length - 8]; + System.arraycopy(buffer_, pos_ + length + 8, longBufferFor= Decryption_, 0, + longBufferForDecryption_.length); + + } + else { + longPosForDecryption_ =3D 0; + longCountForDecryption_ =3D count_ - (pos_ + length); + longBufferForDecryption_ =3D new byte[buffer_.length - pos= _ - length]; + System.arraycopy (buffer_, pos_ + length, longBufferForDec= ryption_, 0, + longBufferForDecryption_.length); + + longValueForDecryption_ =3D new byte[length - 4]; + + System.arraycopy (clearedByte, 4, longValueForDecryption_,= 0, + clearedByte.length - 4); + } + } + } + } + } + + + final int readUnsignedShort () throws org.apache.derby.client.am.Disconn= ectException + { + // should we be checking dss lengths and ddmScalarLengths here=20 + // if yes, i am not sure this is the correct place if we should be che= cking=20 + ensureBLayerDataInBuffer (2); + adjustLengths (2); + return ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + } + + final short readShort () throws org.apache.derby.client.am.DisconnectExc= eption + { + // should we be checking dss lengths and ddmScalarLengths here=20 + ensureBLayerDataInBuffer (2); + adjustLengths (2); + short s =3D SignedBinary.getShort (buffer_, pos_); + + pos_ +=3D 2; + + return s; + } + + final int readInt () throws org.apache.derby.client.am.DisconnectExcepti= on + { + // should we be checking dss lengths and ddmScalarLengths here=20 + ensureBLayerDataInBuffer (4); + adjustLengths (4); + int i =3D SignedBinary.getInt (buffer_, pos_); + pos_ +=3D 4; + + return i; + } + + final void readIntArray (int[] array) throws org.apache.derby.client.am.= DisconnectException + { + ensureBLayerDataInBuffer (array.length * 4); + adjustLengths (array.length * 4); + + for (int i =3D 0; i < array.length; i++) { + array[i] =3D SignedBinary.getInt (buffer_, pos_); + pos_ +=3D 4; + } + } + + + final long readLong () throws org.apache.derby.client.am.DisconnectExcep= tion + { + // should we be checking dss lengths and ddmScalarLengths here + ensureBLayerDataInBuffer (8); + adjustLengths (8); + long l =3D SignedBinary.getLong (buffer_, pos_); + + pos_ +=3D 8; + + return l; + } + + + final int [] readUnsignedShortList() throws org.apache.derby.client.am.= DisconnectException + { + int len =3D ddmScalarLen_; + ensureBLayerDataInBuffer (len); + adjustLengths (len); + + int count =3D len/2; + int[] list =3D new int[count]; + + for (int i=3D0; i < count; i++) { + list[i] =3D ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + } + + return list; + } + + final int readUnsignedByte () throws org.apache.derby.client.am.Disconne= ctException + { + ensureBLayerDataInBuffer (1); + adjustLengths (1); + return (buffer_[pos_++] & 0xff); + } + + final byte readByte () throws org.apache.derby.client.am.DisconnectExcep= tion + { + ensureBLayerDataInBuffer (1); + adjustLengths (1); + return (byte) (buffer_[pos_++] & 0xff); + } + + final boolean readBoolean () throws org.apache.derby.client.am.Disconnec= tException + { + ensureBLayerDataInBuffer (1); + adjustLengths (1); + return buffer_[pos_++] !=3D 0; + } + + final String readString (int length) throws org.apache.derby.client.am.D= isconnectException + { + ensureBLayerDataInBuffer (length); + adjustLengths (length); + + String result =3D ccsidManager_.convertToUCS2 (buffer_, pos_, length); + pos_ +=3D length; + return result; + } + + final String readString (int length, String encoding) throws org.apache.= derby.client.am.DisconnectException + { + ensureBLayerDataInBuffer (length); + adjustLengths (length); + String s =3D null; + + try { + s =3D new String (buffer_, pos_, length, encoding); + } + catch (java.io.UnsupportedEncodingException e) { + agent_.accumulateChainBreakingReadExceptionAndThrow ( + new org.apache.derby.client.am.DisconnectException ( + e, + agent_, + "encoding not supported!!")); + } + + pos_ +=3D length; + return s; + } + + final String readString () throws org.apache.derby.client.am.DisconnectE= xception + { + int len =3D ddmScalarLen_; + ensureBLayerDataInBuffer (len); + adjustLengths (len); + String result =3D ccsidManager_.convertToUCS2 (buffer_, pos_, len); + pos_ +=3D len; + return result; + } + + final byte[] readBytes (int length) throws org.apache.derby.client.am.Di= sconnectException + { + ensureBLayerDataInBuffer (length); + adjustLengths (length); + + byte[] b =3D new byte[length]; + System.arraycopy (buffer_, pos_, b, 0, length); + pos_ +=3D length; + return b; + } + + final byte[] readBytes () throws org.apache.derby.client.am.DisconnectEx= ception + { + int len =3D ddmScalarLen_; + ensureBLayerDataInBuffer (len); + adjustLengths (len); + + byte[] b =3D new byte[len]; + System.arraycopy (buffer_, pos_, b, 0, len); + pos_ +=3D len; + return b; + } + + final byte[] readLDBytes () throws org.apache.derby.client.am.Disconnect= Exception + { + ensureBLayerDataInBuffer (2); + int len =3D ((buffer_[pos_++] & 0xff) << 8) + ((buffer_[pos_++] & 0xff= ) << 0); + + if (len =3D=3D 0) { + adjustLengths (2); + return null; + } + + ensureBLayerDataInBuffer (len); + adjustLengths (len + 2); + + byte [] b =3D new byte[len]; + System.arraycopy (buffer_, pos_, b, 0, len); + pos_ +=3D len; + return b; + } + + final void skipBytes (int length) throws org.apache.derby.client.am.Disc= onnectException + { + ensureBLayerDataInBuffer (length); + adjustLengths (length); + pos_ +=3D length; + } + + final void skipBytes () throws org.apache.derby.client.am.DisconnectExce= ption + { + int len =3D ddmScalarLen_; + ensureBLayerDataInBuffer (len); + adjustLengths (len); + pos_ +=3D len; + } + + // This will be the new and improved getData that handles all QRYDTA/EXT= DTA + // Returns the stream so that the caller can cache it + final ByteArrayOutputStream getData (ByteArrayOutputStream existingBuffe= r) throws org.apache.derby.client.am.DisconnectException + { + boolean readHeader; + int copySize; + ByteArrayOutputStream baos; + + // note: an empty baos can yield an allocated and empty byte[] + if (existingBuffer !=3D null) + baos =3D existingBuffer; + else { + if (ddmScalarLen_ !=3D -1) { + // allocate a stream based on a known amount of data + baos =3D new ByteArrayOutputStream (ddmScalarLen_); + } + else { + // allocate a stream to hold an unknown amount of data + baos =3D new ByteArrayOutputStream (); + //isLengthAndNullabilityUnknown =3D true; + } + } + + // set the amount to read for the first segment + copySize =3D dssLength_; // note: has already been adjusted for headers + + do { + // determine if a continuation header needs to be read after the data + if (dssIsContinued_) + readHeader =3D true; + else + readHeader =3D false; + + // read the segment + ensureALayerDataInBuffer (copySize); + adjustLengths (copySize); + baos.write (buffer_, pos_, copySize); + pos_ +=3D copySize; + + // read the continuation header, if necessary + if (readHeader) + readDSSContinuationHeader (); + + copySize =3D dssLength_; + } + while (readHeader =3D=3D true); + + return baos; + } + + // reads a DSS continuation header + // prereq: pos_ is positioned on the first byte of the two-byte header + // post: dssIsContinued_ is set to true if the continuation bit is on,= false otherwise + // dssLength_ is set to DssConstants.MAX_DSS_LEN - 2 (don't coun= t the header for the next read) + // helper method for getEXTDTAData + protected final void readDSSContinuationHeader () throws org.apache.derb= y=2Eclient.am.DisconnectException + { + ensureALayerDataInBuffer(2); + + dssLength_ =3D + ((buffer_[pos_++]&0xFF) << 8) + + ((buffer_[pos_++]&0xFF) << 0); + + if ((dssLength_ & 0x8000) =3D=3D 0x8000) { + dssLength_ =3D DssConstants.MAX_DSS_LEN; + dssIsContinued_ =3D true; + } + else { + dssIsContinued_ =3D false; + } + // it is a syntax error if the dss continuation header length + // is less than or equal to two + if (dssLength_ <=3D 2) { + doSyntaxrmSemantics (CodePoint.SYNERRCD_DSS_CONT_LESS_OR_EQUAL_2); + } + + dssLength_ -=3D 2; // avoid consuming the DSS cont header + } + + + // As part of parsing the reply, the client can detect that the + // data sent from the target agent does not structurally + // conform to the requirements of the DDM architecture. These are + // the same checks performed by the target server on the messages + // it receives from the protocolj code. Server side detected errors + // result in a SYNTAXRM being returned from the AS. According to the + // DDM manual, parsing of the DSS is terminated when the error is + // detected. The Syntax Error Code, SYNERRCD, describes the various err= ors. + // + // Note: Not all of these may be valid at the client. See descriptions = for + // which ones make sense for client side errors/checks. + // Syntax Error Code Description of Error + // ----------------- -------------------- + // 0x01 Dss header Length is less than 6. + // 0x02 Dss header Length does not match t= he + // number of bytes of data found. + // 0x03 Dss header C-byte not D0. + // 0x04 Dss header f-bytes either not + // recognized or not supported. + // 0x05 DSS continuation specified but not= found. + // For example, DSS continuation is s= pecified + // on the last DSS, and the SNA LU 6.= 2 communication + // facility returned the SEND indicat= or. + // 0x06 DSS chaining specified but no DSS = found. + // For example, DSS chaining is speci= fied + // on the last DSS, and the SNA LU 6.= 2 communication + // facility returned the SEND indicat= or. + // 0x07 Object length less than four. For= example, + // a command parameter's length is sp= ecified + // as two, or a command's length is s= pecified as three. + // 0x08 Object length does not match the n= umber of bytes of data + // found. For example, a RQSDSS with= a length of 150 + // contains a command whose length is= 125 or a SRVDGN parameter + // specifies a length of 200 but ther= e are only 50 + // bytes left in the DSS. + // 0x09 Object length greater than maximum= allowed. + // For example, a RECCNT parameter sp= ecifies a + // length of ten, but the parameter i= s defined + // to have a maximum length of eight. + // 0x0A Object length less than the minimu= m required. + // For example, a SVRCOD parameter sp= ecifies a + // length of five, but the parameter = is defined + // to have a fixed length of six. + // 0x0B Object length not allowed. For ex= ample, + // a FILEXDPT parameter is specified = with a length of + // 11, but this would indicate that o= nly half of the hours + // field is present instead of the co= mplete hours field. + // 0x0C Incorrect large object extended le= ngth field (see + // description of DSS). For example,= an extended + // length field is present, but it is= only three bytes + // long when it is defined to be a mu= ltiple of two bytes. + // 0x0D Object code point index not suppor= ted. + // For example, a code point of 8032 = is encountered + // but x'8' is a reserved code point = index. + // 0x0E Required object not found. For ex= ample, a CLEAR + // command does not have a filnam par= ameter present, + // or a MODREC command is not followe= d by a RECORD + // command data object. + // 0x0F Too many command data objects sent= . For example, + // a MODREC command is followed by tw= o RECORD command + // command data objects, or a DECREC = command is followed + // by RECORD object. + // 0x10 Mutually exclusive objects present. + // For example, a CRTDIRF command spe= cifies both + // a DCLNAM and FILNAM parameters. + // 0x11 Too few command data objects sent. + // For example, an INSRECEF command t= hat + // specified RECCNT95) is followed by= only + // 4 RECORD command data objects. + // 0x12 Duplicate object present. + // For example, a LSTFAT command has = tow FILNAM + // parameters specified. + // 0x13 Invalid request correlator specifi= ed. + // Use PRCCNVRM with PRCCNVDC of 04 o= r 05 instead + // of this error code. This error co= de is being retained + // for compatibility with Level 1 of = the architecture. + // 0x14 Required value not found. + // 0x15 Reserved value not allowed. For e= xample, + // a INSRECEF command specified a REC= CNT(0) parameter. + // 0x16 DSS continuation less than or equa= l to two. + // For example, the length bytes of t= he DSS continuation + // have the value of one. + // 0x17 Objects not in required order. Fo= r example, a RECAL + // object contains a RECORD object fo= llowed by a RECNBR + // object with is not in the defined = order. + // 0x18 DSS chaining byt not b'1', but DSS= FMT bit3 set to b'1'. + // 0x19 Previous DSS indicated current DSS= has the same + // request correlator, but the reques= t correlators are + // not the same. + // 0x1A DSS cahining bit not b'1', but err= or continuation requested. + // 0x1B Mutually exclusive parameter value= s not specified. + // For example, an OPEN command speci= fied PRPSHD(TRUE) + // and FILSHR(READER). + // 0x1D Code point not valid command. For= example, the first + // code point in RQSDSS either is not= in the dictionary + // or is not a code point for a comma= nd. + // + // When the client detects these errors, it will be handled as if a SYNT= AXRM is returned + // from the server. In this SYNTAXRM case, PROTOCOL architects an SQLST= ATE of 58008 or 58009. + // + // Messages + // SQLSTATE : 58009 + // Execution failed due to a distribution protocol error that caused= deallocation of the conversation. + // SQLCODE : -30020 + // Execution failed because of a Distributed Protocol + // Error that will affect the successful execution of subsequent + // commands and SQL statements: Reason Code . + // Some possible reason codes include: + // 121C Indicates that the user is not authorized to perform the re= quested command. + // 1232 The command could not be completed because of a permanent e= rror. + // In most cases, the server will be in the process of an abend. + // 220A The target server has received an invalid data description. + // If a user SQLDA is specified, ensure that the fields are + // initialized correctly. Also, ensure that the length does not + // exceed the maximum allowed length for the data type being us= ed. + // + // The command or statement cannot be processed. The current + // transaction is rolled back and the application is disconnect= ed + // from the remote database. + final void doSyntaxrmSemantics (int syntaxErrorCode) throws org.apache.d= erby.client.am.DisconnectException + { + agent_.accumulateChainBreakingReadExceptionAndThrow ( + new org.apache.derby.client.am.DisconnectException ( + agent_, + "Execution failed due to a distribution protocol error " + + "that caused deallocation of the conversation. " + + "A PROTOCOL Data Stream Syntax Error was detected. Reason: " + + "0x" + Integer.toHexString (syntaxErrorCode), + SqlState._58009)); + } + + +// the names of these methods start with a letter z. +// the z will be removed when they are finalized... + + protected final void pushLengthOnCollectionStack() + { + ddmCollectionLenStack_[++topDdmCollectionStack_] =3D ddmScalarLen_; + ddmScalarLen_ =3D 0; + } + + protected final void adjustLengths(int length) + { + ddmScalarLen_ -=3D length; + adjustCollectionAndDssLengths (length); + /* + for (int i =3D 0; i <=3D topDdmCollectionStack_; i++) { + ddmCollectionLenStack_[i] -=3D length; + } + dssLength_ -=3D length; + */ + } + + protected int adjustDdmLength (int ddmLength, int length) + { + ddmLength -=3D length; + if (ddmLength =3D=3D 0) + adjustLengths (getDdmLength()); + return ddmLength; + } + + // Pop the collection Length stack. + // pre: The collection length stack must not be empty and the top value + // on the stack must be 0. + // post: The top 0 value on the stack will be popped. + protected final void popCollectionStack() + { + topDdmCollectionStack_--; + } + + protected final int peekCodePoint() throws org.apache.derby.client.am.Di= sconnectException + { + if (topDdmCollectionStack_ !=3D EMPTY_STACK) { + if (ddmCollectionLenStack_[topDdmCollectionStack_] =3D=3D 0) + return END_OF_COLLECTION; + else if (ddmCollectionLenStack_[topDdmCollectionStack_] < 4) { + // error + } + } + + // if there is no more data in the current dss, and the dss is not + // continued, indicate the end of the same Id chain or read the next d= ss header. + if ((dssLength_ =3D=3D 0) && (!dssIsContinued_)) { + if (!dssIsChainedWithSameID_) { + return END_OF_SAME_ID_CHAIN; + } + readDssHeader(); + } + + if (longBufferForDecryption_ =3D=3D null) //we don't need to do this = if it's data stream encryption + ensureBLayerDataInBuffer (4); + peekedLength_ =3D ((buffer_[pos_] & 0xff) << 8) + ((buffer_[pos_+1] & = 0xff) << 0); + peekedCodePoint_ =3D ((buffer_[pos_+2] & 0xff) << 8) + ((buffer_[pos_+= 3] & 0xff) << 0); + + // check for extended length + if ((peekedLength_ & 0x8000) =3D=3D 0x8000) + peekExtendedLength(); + else + peekedNumOfExtendedLenBytes_ =3D 0; + return peekedCodePoint_; + } + + // Read out the 2-byte length without moving the pos_ pointer. + protected final int peekLength() throws org.apache.derby.client.am.Disco= nnectException + { + ensureBLayerDataInBuffer (2); + return (((buffer_[pos_] & 0xff) << 8) + + ((buffer_[pos_ + 1] & 0xff) << 0)); + } + + // Read "length" number of bytes from the buffer into the byte array b s= tarting from offset + // "offset". The current offset in the buffer does not change. + protected final int peekFastBytes (byte[] b, int offset, int length) thr= ows org.apache.derby.client.am.DisconnectException + { + for (int i =3D 0; i < length; i++) { + b[offset+i] =3D buffer_[pos_+i]; + } + return offset+length; + } + + protected final void parseLengthAndMatchCodePoint (int expectedCodePoint= ) throws org.apache.derby.client.am.DisconnectException + { + int actualCodePoint =3D 0; + if (peekedCodePoint_ =3D=3D END_OF_COLLECTION) + actualCodePoint =3D readLengthAndCodePoint(); + else { + actualCodePoint =3D peekedCodePoint_; + pos_ +=3D (4 + peekedNumOfExtendedLenBytes_); + ddmScalarLen_ =3D peekedLength_; + if (peekedNumOfExtendedLenBytes_ =3D=3D 0 && ddmScalarLen_ !=3D -1) { + adjustLengths (4); + } + else { + adjustCollectionAndDssLengths (4+peekedNumOfExtendedLenBytes_); + } + peekedLength_ =3D 0; + peekedCodePoint_ =3D END_OF_COLLECTION; + peekedNumOfExtendedLenBytes_ =3D 0; + } + + if (actualCodePoint !=3D expectedCodePoint) { + zThrowSyntaxError ("actual code point, " + actualCodePoint + + " does not match expected code point, " + expecte= dCodePoint); + } + } + protected final int readLengthAndCodePoint () throws org.apache.derby.cl= ient.am.DisconnectException + { + if (topDdmCollectionStack_ !=3D EMPTY_STACK) { + if (ddmCollectionLenStack_[topDdmCollectionStack_] =3D=3D 0) + return END_OF_COLLECTION; + else if (ddmCollectionLenStack_[topDdmCollectionStack_] < 4) { + zThrowSyntaxError ("ddm collection contains less than 4 bytes of d= ata"); + } + } + + // if there is no more data in the current dss, and the dss is not + // continued, indicate the end of the same Id chain or read the next d= ss header. + if ((dssLength_ =3D=3D 0) && (!dssIsContinued_)) { + if (!dssIsChainedWithSameID_) { + return END_OF_SAME_ID_CHAIN; + } + readDssHeader(); + } + + ensureBLayerDataInBuffer (4); + ddmScalarLen_ =3D + ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + int codePoint =3D ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + adjustLengths (4); + + // check for extended length + if ((ddmScalarLen_ & 0x8000) =3D=3D 0x8000) { + readExtendedLength(); + } + return codePoint; + } + + private final void readExtendedLength() throws org.apache.derby.client.a= m=2EDisconnectException + { + int numberOfExtendedLenBytes =3D (ddmScalarLen_ - 0x8000); // fix scro= ll problem was - 4 + int adjustSize =3D 0; + switch (numberOfExtendedLenBytes) { + case 4: + ensureBLayerDataInBuffer (4); + ddmScalarLen_ =3D + ((buffer_[pos_++] & 0xff) << 32) + + ((buffer_[pos_++] & 0xff) << 16) + + ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + adjustSize =3D 4; =20 + break; + case 0: + ddmScalarLen_ =3D -1;=20 + adjustSize =3D 0; =20 + break; + default: + doSyntaxrmSemantics(CodePoint.SYNERRCD_INCORRECT_EXTENDED_LEN); + } + + adjustCollectionAndDssLengths (adjustSize); + /* + // adjust the lengths here. this is a special case since the + // extended length bytes do not include their own length. + for (int i =3D 0; i <=3D topDdmCollectionStack_; i++) { + ddmCollectionLenStack_[i] -=3D adjustSize; + } + dssLength_ -=3D adjustSize; + */ + } + + private final void adjustCollectionAndDssLengths (int length) + { + // adjust the lengths here. this is a special case since the + // extended length bytes do not include their own length. + for (int i =3D 0; i <=3D topDdmCollectionStack_; i++) { + ddmCollectionLenStack_[i] -=3D length; + } + dssLength_ -=3D length; + } + + protected final void startSameIdChainParse() throws org.apache.derby.cli= ent.am.DisconnectException + { + readDssHeader (); + netAgent_.clearSvrcod(); + } + + protected final void endOfSameIdChainData() throws org.apache.derby.clie= nt.am.DisconnectException + { + netAgent_.targetTypdef_ =3D netAgent_.originalTargetTypdef_; + netAgent_.targetSqlam_ =3D netAgent_.orignalTargetSqlam_; + + if (this.topDdmCollectionStack_ !=3D Reply.EMPTY_STACK) { + zThrowSyntaxError ("collection stack not empty at end of same id cha= in parse"); + } + if (this.dssLength_ !=3D 0) { + zThrowSyntaxError ("dss length not 0 at end of same id chain parse"); + } + if (dssIsChainedWithSameID_ =3D=3D true) { + zThrowSyntaxError ("dss chained with same id at end of same id chain= parse"); + } + } + + private final void zThrowSyntaxError (String error) throws org.apache.de= rby.client.am.DisconnectException + { + agent_.accumulateChainBreakingReadExceptionAndThrow ( + new org.apache.derby.client.am.DisconnectException (agent_, error)); + } + + protected final int peekTotalColumnCount (int tripletLength) throws org.= apache.derby.client.am.DisconnectException + { + int columnCount =3D 0; + int offset =3D 0; + int tripletType =3D FdocaConstants.CPT_TRIPLET_TYPE; + while (tripletType =3D=3D FdocaConstants.CPT_TRIPLET_TYPE) { + columnCount +=3D ((tripletLength-3)/3); + // Peek ahead for the next triplet's tripletLength and tripletType. + // The number of bytes to skip before the next tripletType is triple= tLength - 3. + ensureBLayerDataInBuffer (tripletLength-3); + offset +=3D (tripletLength-3); + tripletLength =3D (buffer_[pos_+offset++] & 0xff); + tripletType =3D (buffer_[pos_+offset++] & 0xff); + // Skip the 1-byte tripletId. + offset++; + } + return columnCount; + } + + private final void peekExtendedLength () throws org.apache.derby.client.= am.DisconnectException + { + peekedNumOfExtendedLenBytes_ =3D (peekedLength_ - 0x8004); + switch (peekedNumOfExtendedLenBytes_) { + case 4: + // L L C P Extended Length + // -->2-bytes<-- --->4-bytes<--- + // We are only peeking the length here, the actual pos_ is still bef= ore LLCP. We ensured + // 4-bytes in peedCodePoint() for the LLCP, and we need to ensure 4-= bytes(of LLCP) + the + // extended length bytes here. + if (longBufferForDecryption_ =3D=3D null ) //we ddon't need to do th= is if it's data stream encryption + ensureBLayerDataInBuffer (4 + 4); + // The ddmScalarLen_ we peek here does not include the LLCP and the = extended length bytes + // themselves. So we will add those back to the ddmScalarLen_ so it= can be adjusted + // correctly in parseLengthAndMatchCodePoint(). (since the adjustLen= gths() method will + // subtract the length from ddmScalarLen_) + peekedLength_ =3D + ((buffer_[pos_+4] & 0xff) << 32) + + ((buffer_[pos_+5] & 0xff) << 16) + + ((buffer_[pos_+6] & 0xff) << 8) + + ((buffer_[pos_+7] & 0xff) << 0); + break; + case 0: + peekedLength_ =3D -1; // this ddm is streamed, so set -1 -> length u= nknown + break; + default: + doSyntaxrmSemantics(CodePoint.SYNERRCD_INCORRECT_EXTENDED_LEN); + } + } + + final int readFastUnsignedByte () throws org.apache.derby.client.am.Disc= onnectException + { + return (buffer_[pos_++] & 0xff); + } + + final short readFastShort () throws org.apache.derby.client.am.Disconnec= tException + { + short s =3D SignedBinary.getShort (buffer_, pos_); + pos_ +=3D 2; + return s; + } + + final int readFastUnsignedShort () throws org.apache.derby.client.am.Dis= connectException + { + return ((buffer_[pos_++] & 0xff) << 8) + + ((buffer_[pos_++] & 0xff) << 0); + } + + final int readFastInt () throws org.apache.derby.client.am.DisconnectExc= eption + { + int i =3D SignedBinary.getInt (buffer_, pos_); + pos_ +=3D 4; + return i; + } + + final String readFastString (int length) throws org.apache.derby.client.= am.DisconnectException + { + String result =3D ccsidManager_.convertToUCS2 (buffer_, pos_, length); + pos_ +=3D length; + return result; + } + + final byte[] readFastBytes (int length) throws org.apache.derby.client.a= m=2EDisconnectException + { + byte[] b =3D new byte[length]; + System.arraycopy (buffer_, pos_, b, 0, length); + pos_ +=3D length; + return b; + } + + protected final int peekFastLength() throws org.apache.derby.client.am.D= isconnectException + { + return (((buffer_[pos_] & 0xff) << 8) + + ((buffer_[pos_ + 1] & 0xff) << 0)); + } + + final void skipFastBytes (int length) throws org.apache.derby.client.am.= DisconnectException + { + pos_ +=3D length; + } + + final void readFastIntArray (int[] array) throws org.apache.derby.client= .am.DisconnectException + { + for (int i =3D 0; i < array.length; i++) { + array[i] =3D SignedBinary.getInt (buffer_, pos_); + pos_ +=3D 4; + } + } + + final String readFastString (int length, String encoding) throws org.apa= che.derby.client.am.DisconnectException + { + String s =3D null; + + try { + s =3D new String (buffer_, pos_, length, encoding); + } + catch (java.io.UnsupportedEncodingException e) { + agent_.accumulateChainBreakingReadExceptionAndThrow ( + new org.apache.derby.client.am.DisconnectException ( + e, + agent_, + "encoding not supported!!")); + } + pos_ +=3D length; + return s; + } + + final byte[] readFastLDBytes () throws org.apache.derby.client.am.Discon= nectException + { + int len =3D ((buffer_[pos_++] & 0xff) << 8) + ((buffer_[pos_++] & 0xff= ) << 0); + if (len =3D=3D 0) return null; + + byte [] b =3D new byte[len]; + System.arraycopy (buffer_, pos_, b, 0, len); + pos_ +=3D len; + return b; + } + final long readFastLong () throws org.apache.derby.client.am.DisconnectE= xception + { + long l =3D SignedBinary.getLong (buffer_, pos_ ); + pos_ +=3D 8; + return l; + } + + final byte readFastByte () throws org.apache.derby.client.am.DisconnectE= xception + { + return (byte) (buffer_[pos_++] & 0xff); +} + + final void mark() + { + currentPos_ =3D pos_; + } + + // remove and return the top offset value from mark stack. + final int popMark() + { + return currentPos_; + } + + final int getFastSkipSQLCARDrowLength () + { + return pos_ - popMark(); + } + + // The only difference between this method and the original getData() me= thod is this method + // is not doing an ensureALayerDataInBuffer + final ByteArrayOutputStream getFastData (ByteArrayOutputStream existingB= uffer) throws org.apache.derby.client.am.DisconnectException + { + boolean readHeader; + int copySize; + ByteArrayOutputStream baos; + + // note: an empty baos can yield an allocated and empty byte[] + if (existingBuffer !=3D null) + baos =3D existingBuffer; + else { + if (ddmScalarLen_ !=3D -1) { + // allocate a stream based on a known amount of data + baos =3D new ByteArrayOutputStream (ddmScalarLen_); + } + else { + // allocate a stream to hold an unknown amount of data + baos =3D new ByteArrayOutputStream (); + //isLengthAndNullabilityUnknown =3D true; + } + } + + // set the amount to read for the first segment + copySize =3D dssLength_; // note: has already been adjusted for headers + + do { + // determine if a continuation header needs to be read after the data + if (dssIsContinued_) + readHeader =3D true; + else + readHeader =3D false; + + // read the segment + //ensureALayerDataInBuffer (copySize); + adjustLengths (copySize); + baos.write (buffer_, pos_, copySize); + pos_ +=3D copySize; + + // read the continuation header, if necessary + if (readHeader) + readDSSContinuationHeader (); + + copySize =3D dssLength_; + } + while (readHeader =3D=3D true); + + return baos; + } + + // This method is only used to match the codePoint for those class insta= nce variables + // that are embedded in other reply messages. + final protected void matchCodePoint (int expectedCodePoint) throws org.a= pache.derby.client.am.DisconnectException + { + int actualCodePoint =3D 0; + actualCodePoint =3D peekedCodePoint_; + pos_ +=3D 4; + if (actualCodePoint !=3D expectedCodePoint) { + zThrowSyntaxError ("actual code point, " + actualCodePoint + + " does not match expected code point, " + expecte= dCodePoint); + } + } + + + protected final int peekNumOfColumns () throws org.apache.derby.client.a= m=2EDisconnectException + { + // skip the 4-byte LLCP and any extended length bytes + 1-byte null sq= lcagrp null indicator + int offset =3D (4 + peekedNumOfExtendedLenBytes_ + 1); + + offset =3D skipSQLDHROW (offset); + + return SignedBinary.getShort (buffer_, pos_+offset); + } + + protected final boolean peekForNullSqlcagrp () + { + // skip the 4-byte LLCP and any extended length bytes + int offset =3D (4 + peekedNumOfExtendedLenBytes_); + int nullInd =3D buffer_[pos_+offset] & 0xff; + return (nullInd =3D=3D CodePoint.NULLDATA); + } + + private final int skipSQLDHROW (int offset) throws org.apache.derby.clie= nt.am.DisconnectException + { + int sqldhrowgrpNullInd =3D buffer_[pos_+offset++] & 0xff; + if (sqldhrowgrpNullInd =3D=3D CodePoint.NULLDATA) + return offset; + + offset +=3D 12; + + // skip sqldrdbnam + int stringLength =3D ((buffer_[pos_+offset++] & 0xff) << 8) + + ((buffer_[pos_+offset++] & 0xff) << 0); + offset +=3D stringLength; + + // skip sqldschema + stringLength =3D ((buffer_[pos_+offset++] & 0xff) << 8) + + ((buffer_[pos_+offset++] & 0xff) << 0); + offset +=3D stringLength; + + stringLength =3D ((buffer_[pos_+offset++] & 0xff) << 8) + + ((buffer_[pos_+offset++] & 0xff) << 0); + offset +=3D stringLength; + + return offset; + } +} + + + + Propchange: incubator/derby/code/trunk/java/client/org/apache/derby/client/= net/Reply.java ---------------------------------------------------------------------------= --- svn:eol-style =3D native