1 21 package org.apache.derby.client.net; 22 23 import org.apache.derby.client.am.DisconnectException; 24 import org.apache.derby.client.am.EncryptionManager; 25 import org.apache.derby.client.am.ClientMessageId; 26 import org.apache.derby.client.am.SqlException; 27 import org.apache.derby.client.am.Utils; 28 import org.apache.derby.shared.common.reference.SQLState; 29 30 import java.io.UnsupportedEncodingException ; 31 32 public class Request { 33 34 protected byte[] bytes_; 37 38 protected int offset_; 41 42 private final static int MAX_MARKS_NESTING = 10; 48 private int[] markStack_ = new int[MAX_MARKS_NESTING]; 49 private int top_ = 0; 50 51 protected CcsidManager ccsidManager_; 55 56 private int dssLengthLocation_ = 0; 61 62 private int correlationID_ = 0; 65 66 private boolean simpleDssFinalize = false; 67 68 protected boolean passwordIncluded_ = false; 70 protected int passwordStart_ = 0; 71 protected int passwordLength_ = 0; 72 73 protected NetAgent netAgent_; 74 75 76 Request(NetAgent netAgent, int minSize, CcsidManager ccsidManager) { 80 netAgent_ = netAgent; 81 bytes_ = new byte[minSize]; 82 ccsidManager_ = ccsidManager; 83 clearBuffer(); 84 } 85 86 Request(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) { 90 this(netAgent, bufferSize, ccsidManager); 92 } 93 94 protected final void clearBuffer() { 95 offset_ = 0; 96 top_ = 0; 97 for (int i = 0; i < markStack_.length; i++) { 98 if (markStack_[i] != 0) { 99 markStack_[i] = 0; 100 } else { 101 break; 102 } 103 } 104 dssLengthLocation_ = 0; 105 } 106 107 final void initialize() { 108 clearBuffer(); 109 correlationID_ = 0; 110 } 111 112 final void setCcsidMgr(CcsidManager ccsidManager) { 116 ccsidManager_ = ccsidManager; 117 } 118 119 protected final void ensureLength(int length) { 124 if (length > bytes_.length) { 125 byte newBytes[] = new byte[Math.max(bytes_.length << 1, length)]; 126 System.arraycopy(bytes_, 0, newBytes, 0, offset_); 127 bytes_ = newBytes; 128 } 129 } 130 131 protected final void createCommand() { 136 buildDss(false, false, false, DssConstants.GDSFMT_RQSDSS, ++correlationID_, false); 137 } 138 139 protected void createXACommand() { 144 buildDss(false, false, false, DssConstants.GDSFMT_RQSDSS_NOREPLY, ++correlationID_, false); 145 } 146 147 final void createCommandData() { 152 buildDss(true, 153 false, 154 false, 155 DssConstants.GDSFMT_OBJDSS, 156 correlationID_, 157 false); 158 } 159 160 final void createEncryptedCommandData() { 161 if (netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRIDDTA || 162 netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRPWDDTA) { 163 buildDss(true, false, false, DssConstants.GDSFMT_ENCOBJDSS, correlationID_, false); 164 } else { 165 buildDss(true, 166 false, 167 false, 168 DssConstants.GDSFMT_OBJDSS, 169 correlationID_, 170 false); 171 } 172 } 173 174 175 177 private final void buildDss(boolean dssHasSameCorrelator, 178 boolean chainedToNextStructure, 179 boolean nextHasSameCorrelator, 180 int dssType, 181 int corrId, 182 boolean simpleFinalizeBuildingNextDss) { 183 if (doesRequestContainData()) { 184 if (simpleDssFinalize) { 185 finalizeDssLength(); 186 } else { 187 finalizePreviousChainedDss(dssHasSameCorrelator); 188 } 189 } 190 191 ensureLength(offset_ + 6); 192 193 dssLengthLocation_ = offset_; 197 bytes_[offset_++] = (byte) 0xFF; 200 bytes_[offset_++] = (byte) 0xFF; 201 202 bytes_[offset_++] = (byte) 0xD0; 204 if (chainedToNextStructure) { 205 dssType |= DssConstants.GDSCHAIN; 206 if (nextHasSameCorrelator) { 207 dssType |= DssConstants.GDSCHAIN_SAME_ID; 208 } 209 } 210 bytes_[offset_++] = (byte) (dssType & 0xff); 211 212 bytes_[offset_++] = (byte) ((corrId >>> 8) & 0xff); 215 bytes_[offset_++] = (byte) (corrId & 0xff); 216 217 simpleDssFinalize = simpleFinalizeBuildingNextDss; 218 } 219 220 221 final void writeScalarStream(boolean chained, 222 boolean chainedWithSameCorrelator, 223 int codePoint, 224 int length, 225 java.io.InputStream in, 226 boolean writeNullByte, 227 int parameterIndex) throws DisconnectException, SqlException { 228 229 if (netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRIDDTA || 230 netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRPWDDTA) { 231 232 writeEncryptedScalarStream(chained, 233 chainedWithSameCorrelator, 234 codePoint, 235 length, 236 in, 237 writeNullByte, 238 parameterIndex); 239 240 }else{ 241 242 writePlainScalarStream(chained, 243 chainedWithSameCorrelator, 244 codePoint, 245 length, 246 in, 247 writeNullByte, 248 parameterIndex); 249 250 } 251 252 } 253 254 final void writeEncryptedScalarStream(boolean chained, 257 boolean chainedWithSameCorrelator, 258 int codePoint, 259 int length, 260 java.io.InputStream in, 261 boolean writeNullByte, 262 int parameterIndex) throws DisconnectException, SqlException { 263 264 265 266 int leftToRead = length; 267 int extendedLengthByteCount = prepScalarStream(chained, 268 chainedWithSameCorrelator, 269 writeNullByte, 270 leftToRead); 271 int bytesToRead; 272 273 if (writeNullByte) { 274 bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - 1 - extendedLengthByteCount); 275 } else { 276 bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - extendedLengthByteCount); 277 } 278 279 byte[] lengthAndCodepoint; 280 lengthAndCodepoint = buildLengthAndCodePointForEncryptedLob(codePoint, 281 leftToRead, 282 writeNullByte, 283 extendedLengthByteCount); 284 285 286 287 290 byte[] clearedBytes = new byte[leftToRead]; 291 int bytesRead = 0; 292 int totalBytesRead = 0; 293 int pos = 0; 294 do { 295 try { 296 bytesRead = in.read(clearedBytes, pos, leftToRead); 297 totalBytesRead += bytesRead; 298 } catch (java.io.IOException e) { 299 padScalarStreamForError(leftToRead, bytesToRead); 300 netAgent_.accumulateReadException(new SqlException(netAgent_.logWriter_, 302 new ClientMessageId(SQLState.NET_IOEXCEPTION_ON_READ), 303 new Integer (parameterIndex), e.getMessage(), e)); 304 return; 305 } 306 if (bytesRead == -1) { 307 313 netAgent_.accumulateChainBreakingReadExceptionAndThrow( 318 new DisconnectException(netAgent_, 319 new ClientMessageId(SQLState.NET_PREMATURE_EOS_DISCONNECT), 320 new Integer (parameterIndex))); 321 return; 322 323 329 } else { 330 pos += bytesRead; 331 leftToRead -= bytesRead; 333 } 334 335 } while (leftToRead > 0); 336 337 try { 339 if (in.read() != -1) { 340 netAgent_.accumulateReadException(new SqlException( 342 netAgent_.logWriter_, 343 new ClientMessageId(SQLState.NET_INPUTSTREAM_LENGTH_TOO_SMALL), 344 new Integer (parameterIndex))); 345 } 346 } catch (java.io.IOException e) { 347 netAgent_.accumulateReadException(new SqlException( 348 netAgent_.logWriter_, 349 new ClientMessageId( 350 SQLState.NET_IOEXCEPTION_ON_STREAMLEN_VERIFICATION), 351 new Integer (parameterIndex), 352 e.getMessage(), 353 e)); 354 } 355 356 byte[] newClearedBytes = new byte[clearedBytes.length + 357 lengthAndCodepoint.length]; 358 System.arraycopy(lengthAndCodepoint, 0, newClearedBytes, 0, 359 lengthAndCodepoint.length); 360 System.arraycopy(clearedBytes, 0, newClearedBytes, lengthAndCodepoint.length, clearedBytes.length); 361 byte[] encryptedBytes; 363 encryptedBytes = netAgent_.netConnection_.getEncryptionManager(). 364 encryptData(newClearedBytes, 365 NetConfiguration.SECMEC_EUSRIDPWD, 366 netAgent_.netConnection_.getTargetPublicKey(), 367 netAgent_.netConnection_.getTargetPublicKey()); 368 369 int encryptedBytesLength = encryptedBytes.length; 370 int sendingLength = bytes_.length - offset_; 371 if (encryptedBytesLength > (bytes_.length - offset_)) { 372 373 System.arraycopy(encryptedBytes, 0, bytes_, offset_, (bytes_.length - offset_)); 374 offset_ = 32767; 375 try { 376 sendBytes(netAgent_.getOutputStream()); 377 } catch (java.io.IOException ioe) { 378 netAgent_.throwCommunicationsFailure(ioe); 379 } 380 } else { 381 System.arraycopy(encryptedBytes, 0, bytes_, offset_, encryptedBytesLength); 382 offset_ = offset_ + encryptedBytes.length; 383 } 384 385 encryptedBytesLength = encryptedBytesLength - sendingLength; 386 while (encryptedBytesLength > 0) { 387 offset_ = 0; 389 390 if ((encryptedBytesLength - 32765) > 0) { 391 bytes_[offset_++] = (byte) (0xff); 392 bytes_[offset_++] = (byte) (0xff); 393 System.arraycopy(encryptedBytes, sendingLength, bytes_, offset_, 32765); 394 encryptedBytesLength -= 32765; 395 sendingLength += 32765; 396 offset_ = 32767; 397 try { 398 sendBytes(netAgent_.getOutputStream()); 399 } catch (java.io.IOException ioe) { 400 netAgent_.throwCommunicationsFailure(ioe); 401 } 402 } else { 403 int leftlength = encryptedBytesLength + 2; 404 bytes_[offset_++] = (byte) ((leftlength >>> 8) & 0xff); 405 bytes_[offset_++] = (byte) (leftlength & 0xff); 406 407 System.arraycopy(encryptedBytes, sendingLength, bytes_, offset_, encryptedBytesLength); 408 409 offset_ += encryptedBytesLength; 410 dssLengthLocation_ = offset_; 411 encryptedBytesLength = 0; 412 } 413 414 } 415 } 416 417 418 final void writePlainScalarStream(boolean chained, 421 boolean chainedWithSameCorrelator, 422 int codePoint, 423 int length, 424 java.io.InputStream in, 425 boolean writeNullByte, 426 int parameterIndex) throws DisconnectException, SqlException { 427 int leftToRead = length; 428 int extendedLengthByteCount = prepScalarStream(chained, 429 chainedWithSameCorrelator, 430 writeNullByte, 431 leftToRead); 432 int bytesToRead; 433 434 if (writeNullByte) { 435 bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - 1 - extendedLengthByteCount); 436 } else { 437 bytesToRead = Utils.min(leftToRead, DssConstants.MAX_DSS_LEN - 6 - 4 - extendedLengthByteCount); 438 } 439 440 buildLengthAndCodePointForLob(codePoint, 441 leftToRead, 442 writeNullByte, 443 extendedLengthByteCount); 444 445 int bytesRead = 0; 446 int totalBytesRead = 0; 447 do { 448 do { 449 try { 450 bytesRead = in.read(bytes_, offset_, bytesToRead); 451 totalBytesRead += bytesRead; 452 } catch (java.io.IOException e) { 453 padScalarStreamForError(leftToRead, bytesToRead); 454 netAgent_.accumulateReadException(new SqlException( 456 netAgent_.logWriter_, 457 new ClientMessageId(SQLState.NET_IOEXCEPTION_ON_READ), 458 new Integer (parameterIndex), 459 e.getMessage(), 460 e)); 461 462 return; 463 } 464 if (bytesRead == -1) { 465 padScalarStreamForError(leftToRead, bytesToRead); 466 netAgent_.accumulateReadException(new SqlException(netAgent_.logWriter_, 468 new ClientMessageId(SQLState.NET_PREMATURE_EOS), 469 new Integer (parameterIndex))); 470 return; 471 } else { 472 bytesToRead -= bytesRead; 473 offset_ += bytesRead; 474 leftToRead -= bytesRead; 475 } 476 } while (bytesToRead > 0); 477 478 bytesToRead = flushScalarStreamSegment(leftToRead, bytesToRead); 479 } while (leftToRead > 0); 480 481 try { 483 if (in.read() != -1) { 484 netAgent_.accumulateReadException(new SqlException(netAgent_.logWriter_, 486 new ClientMessageId(SQLState.NET_INPUTSTREAM_LENGTH_TOO_SMALL), 487 new Integer (parameterIndex))); 488 } 489 } catch (java.io.IOException e) { 490 netAgent_.accumulateReadException(new SqlException( 491 netAgent_.logWriter_, 492 new ClientMessageId( 493 SQLState.NET_IOEXCEPTION_ON_STREAMLEN_VERIFICATION), 494 new Integer (parameterIndex), 495 e.getMessage(), 496 e)); 497 } 498 } 499 500 501 final void writeScalarStream(boolean chained, 505 boolean chainedWithSameCorrelator, 506 int codePoint, 507 int length, 508 java.io.Reader r, 509 boolean writeNullByte, 510 int parameterIndex) throws DisconnectException, 511 SqlException{ 512 513 writeScalarStream(chained, 514 chainedWithSameCorrelator, 515 codePoint, 516 length * 2, 517 EncodedInputStream.createUTF16BEStream(r), 518 writeNullByte, 519 parameterIndex); 520 } 521 522 523 protected final int prepScalarStream(boolean chained, 528 boolean chainedWithSameCorrelator, 529 boolean writeNullByte, 530 int leftToRead) throws DisconnectException { 531 int extendedLengthByteCount; 532 533 int nullIndicatorSize = 0; 534 if (writeNullByte) { 535 extendedLengthByteCount = calculateExtendedLengthByteCount((long) leftToRead + 4 + 1); 537 nullIndicatorSize = 1; 538 } else { 539 extendedLengthByteCount = calculateExtendedLengthByteCount(leftToRead + 4); 540 } 541 542 if (10 + extendedLengthByteCount + nullIndicatorSize + (long) leftToRead + offset_ > DssConstants.MAX_DSS_LEN) { 545 try { 546 if (simpleDssFinalize) { 547 finalizeDssLength(); 548 } else { 549 finalizePreviousChainedDss(true); 550 } 551 sendBytes(netAgent_.getOutputStream()); 552 } catch (java.io.IOException e) { 553 netAgent_.throwCommunicationsFailure(e); 554 } 555 } 556 557 if (netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRIDDTA || 558 netAgent_.netConnection_.getSecurityMechanism() == NetConfiguration.SECMEC_EUSRPWDDTA) { 559 buildDss(true, 560 chained, 561 chainedWithSameCorrelator, 562 DssConstants.GDSFMT_ENCOBJDSS, 563 correlationID_, 564 true); 565 } else 566 { 568 buildDss(true, 569 chained, 570 chainedWithSameCorrelator, 571 DssConstants.GDSFMT_OBJDSS, 572 correlationID_, 573 true); 574 } 575 576 return extendedLengthByteCount; 577 } 578 579 580 protected final int flushScalarStreamSegment(int leftToRead, 583 int bytesToRead) throws DisconnectException { 584 int newBytesToRead = bytesToRead; 585 586 if (leftToRead != 0) { 588 if ((Utils.min(2 + leftToRead, 32767)) > (bytes_.length - offset_)) { 590 try { 591 sendBytes(netAgent_.getOutputStream()); 592 } catch (java.io.IOException ioe) { 593 netAgent_.throwCommunicationsFailure(ioe); 594 } 595 } 596 dssLengthLocation_ = offset_; 597 bytes_[offset_++] = (byte) (0xff); 598 bytes_[offset_++] = (byte) (0xff); 599 newBytesToRead = Utils.min(leftToRead, 32765); 600 } 601 602 return newBytesToRead; 603 } 604 605 protected final void padScalarStreamForError(int leftToRead, int bytesToRead) throws DisconnectException { 608 do { 609 do { 610 bytes_[offset_++] = (byte) (0x0); bytesToRead--; 612 leftToRead--; 613 } while (bytesToRead > 0); 614 615 bytesToRead = flushScalarStreamSegment(leftToRead, bytesToRead); 616 } while (leftToRead > 0); 617 } 618 619 private final void writeExtendedLengthBytes(int extendedLengthByteCount, long length) { 620 int shiftSize = (extendedLengthByteCount - 1) * 8; 621 for (int i = 0; i < extendedLengthByteCount; i++) { 622 bytes_[offset_++] = (byte) ((length >>> shiftSize) & 0xff); 623 shiftSize -= 8; 624 } 625 } 626 627 private final byte[] writeExtendedLengthBytesForEncryption(int extendedLengthByteCount, long length) { 628 int shiftSize = (extendedLengthByteCount - 1) * 8; 629 byte[] extendedLengthBytes = new byte[extendedLengthByteCount]; 630 for (int i = 0; i < extendedLengthByteCount; i++) { 631 extendedLengthBytes[i] = (byte) ((length >>> shiftSize) & 0xff); 632 shiftSize -= 8; 633 } 634 return extendedLengthBytes; 635 } 636 637 639 protected final void finalizePreviousChainedDss(boolean dssHasSameCorrelator) { 643 finalizeDssLength(); 644 bytes_[dssLengthLocation_ + 3] |= 0x40; 645 if (dssHasSameCorrelator) { 647 bytes_[dssLengthLocation_ + 3] |= 0x10; 648 } 649 } 650 651 protected final boolean doesRequestContainData() { 654 return offset_ != 0; 655 } 656 657 protected final void finalizeDssLength() { 667 int totalSize = offset_ - dssLengthLocation_; 672 int bytesRequiringContDssHeader = totalSize - 32767; 673 674 if (bytesRequiringContDssHeader > 0) { 676 677 int contDssHeaderCount = bytesRequiringContDssHeader / 32765; 682 if (bytesRequiringContDssHeader % 32765 != 0) { 683 contDssHeaderCount++; 684 } 685 686 int dataByte = offset_ - 1; 697 int shiftOffset = contDssHeaderCount * 2; 698 ensureLength(offset_ + shiftOffset); 699 offset_ += shiftOffset; 700 701 boolean passOne = true; 704 do { 705 int dataToShift = bytesRequiringContDssHeader % 32765; 707 if (dataToShift == 0) { 708 dataToShift = 32765; 709 } 710 711 dataByte -= dataToShift; 713 System.arraycopy(bytes_, dataByte + 1,bytes_, dataByte + shiftOffset + 1, dataToShift); 714 715 int twoByteContDssHeader = dataToShift + 2; 719 if (passOne) { 720 passOne = false; 721 } else { 722 if (twoByteContDssHeader == 32767) { 723 twoByteContDssHeader = 0xFFFF; 724 } 725 } 726 727 bytes_[dataByte + shiftOffset - 1] = (byte) ((twoByteContDssHeader >>> 8) & 0xff); 729 bytes_[dataByte + shiftOffset] = (byte) (twoByteContDssHeader & 0xff); 730 731 bytesRequiringContDssHeader -= dataToShift; 734 shiftOffset -= 2; 735 736 } while (bytesRequiringContDssHeader > 0); 738 739 totalSize = 0xFFFF; 741 742 } 743 744 bytes_[dssLengthLocation_] = (byte) ((totalSize >>> 8) & 0xff); 746 bytes_[dssLengthLocation_ + 1] = (byte) (totalSize & 0xff); 747 } 748 749 protected final void markLengthBytes(int codePoint) { 756 ensureLength(offset_ + 4); 757 758 mark(); 760 761 offset_ += 2; 763 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 764 bytes_[offset_++] = (byte) (codePoint & 0xff); 765 } 766 767 private final void mark() { 770 markStack_[top_++] = offset_; 771 } 772 773 private final int popMark() { 775 return markStack_[--top_]; 776 } 777 778 protected final void markForCachingPKGNAMCSN() { 779 mark(); 780 } 781 782 protected final int popMarkForCachingPKGNAMCSN() { 783 return popMark(); 784 } 785 786 protected final void updateLengthBytes() throws SqlException { 793 int lengthLocation = popMark(); 796 int length = offset_ - lengthLocation; 797 798 int extendedLengthByteCount = calculateExtendedLengthByteCount(length); 802 if (extendedLengthByteCount != 0) { 803 804 ensureLength(offset_ + extendedLengthByteCount); 806 807 int extendedLength = length - 4; 810 811 int extendedLengthLocation = lengthLocation + 4; 813 System.arraycopy(bytes_, 814 extendedLengthLocation, 815 bytes_, 816 extendedLengthLocation + extendedLengthByteCount, 817 extendedLength); 818 819 int shiftSize = (extendedLengthByteCount - 1) * 8; 821 for (int i = 0; i < extendedLengthByteCount; i++) { 822 bytes_[extendedLengthLocation++] = (byte) ((extendedLength >>> shiftSize) & 0xff); 823 shiftSize -= 8; 824 } 825 offset_ += extendedLengthByteCount; 827 828 length = extendedLengthByteCount + 4; 833 length |= 0x8000; 834 } 835 836 bytes_[lengthLocation] = (byte) ((length >>> 8) & 0xff); 838 bytes_[lengthLocation + 1] = (byte) (length & 0xff); 839 } 840 841 private final int calculateExtendedLengthByteCount(long ddmSize) { 845 if (ddmSize <= 0x7FFF) { 850 return 0; 851 } else if (ddmSize <= 0x7FFFFFFFL) { 852 return 4; 853 } else if (ddmSize <= 0x7FFFFFFFFFFFL) { 854 return 6; 855 } else { 856 return 8; 857 } 858 } 859 860 final void padBytes(byte padByte, int length) { 862 ensureLength(offset_ + length); 863 for (int i = 0; i < length; i++) { 864 bytes_[offset_++] = padByte; 865 } 866 } 867 868 final void write1Byte(int value) { 870 ensureLength(offset_ + 1); 871 bytes_[offset_++] = (byte) (value & 0xff); 872 } 873 874 final void buildTripletHeader(int tripletLength, 877 int tripletType, 878 int tripletId) { 879 ensureLength(offset_ + 3); 880 bytes_[offset_++] = (byte) (tripletLength & 0xff); 881 bytes_[offset_++] = (byte) (tripletType & 0xff); 882 bytes_[offset_++] = (byte) (tripletId & 0xff); 883 } 884 885 final void writeLidAndLengths(int[][] lidAndLengthOverrides, int count, int offset) { 886 ensureLength(offset_ + (count * 3)); 887 for (int i = 0; i < count; i++, offset++) { 888 bytes_[offset_++] = (byte) (lidAndLengthOverrides[offset][0] & 0xff); 889 bytes_[offset_++] = (byte) ((lidAndLengthOverrides[offset][1] >>> 8) & 0xff); 890 bytes_[offset_++] = (byte) (lidAndLengthOverrides[offset][1] & 0xff); 891 } 892 } 893 894 final void writeLidAndLengths(int[][] lidAndLengthOverrides, 899 int count, 900 int offset, 901 boolean mddRequired, 902 java.util.Hashtable map) { 903 if (!mddRequired) { 904 writeLidAndLengths(lidAndLengthOverrides, count, offset); 905 } 906 else { 909 ensureLength(offset_ + (count * 3)); 910 int protocolType, overrideLid; 911 Object entry; 912 for (int i = 0; i < count; i++, offset++) { 913 protocolType = lidAndLengthOverrides[offset][0]; 914 entry = map.get(new Integer (protocolType)); 917 overrideLid = (entry == null) ? protocolType : ((Integer ) entry).intValue(); 918 bytes_[offset_++] = (byte) (overrideLid & 0xff); 919 bytes_[offset_++] = (byte) ((lidAndLengthOverrides[offset][1] >>> 8) & 0xff); 920 bytes_[offset_++] = (byte) (lidAndLengthOverrides[offset][1] & 0xff); 921 } 922 } 923 } 924 925 927 final void write2Bytes(int value) { 929 ensureLength(offset_ + 2); 930 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 931 bytes_[offset_++] = (byte) (value & 0xff); 932 } 933 934 final void write4Bytes(long value) { 936 ensureLength(offset_ + 4); 937 bytes_[offset_++] = (byte) ((value >>> 24) & 0xff); 938 bytes_[offset_++] = (byte) ((value >>> 16) & 0xff); 939 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 940 bytes_[offset_++] = (byte) (value & 0xff); 941 } 942 943 final void writeBytes(byte[] buf, int length) { 947 ensureLength(offset_ + length); 948 System.arraycopy(buf, 0, bytes_, offset_, length); 949 offset_ += length; 950 } 951 952 final void writeBytes(byte[] buf) { 953 ensureLength(offset_ + buf.length); 954 System.arraycopy(buf, 0, bytes_, offset_, buf.length); 955 offset_ += buf.length; 956 } 957 958 final void writeCodePoint4Bytes(int codePoint, int value) { ensureLength(offset_ + 4); 961 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 962 bytes_[offset_++] = (byte) (codePoint & 0xff); 963 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 964 bytes_[offset_++] = (byte) (value & 0xff); 965 } 966 967 protected final void writeScalar1Byte(int codePoint, int value) { 970 ensureLength(offset_ + 5); 971 bytes_[offset_++] = 0x00; 972 bytes_[offset_++] = 0x05; 973 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 974 bytes_[offset_++] = (byte) (codePoint & 0xff); 975 bytes_[offset_++] = (byte) (value & 0xff); 976 } 977 978 final void writeScalar2Bytes(int codePoint, int value) { 981 ensureLength(offset_ + 6); 982 bytes_[offset_++] = 0x00; 983 bytes_[offset_++] = 0x06; 984 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 985 bytes_[offset_++] = (byte) (codePoint & 0xff); 986 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 987 bytes_[offset_++] = (byte) (value & 0xff); 988 } 989 990 protected final void writeScalar4Bytes(int codePoint, long value) { 993 ensureLength(offset_ + 8); 994 bytes_[offset_++] = 0x00; 995 bytes_[offset_++] = 0x08; 996 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 997 bytes_[offset_++] = (byte) (codePoint & 0xff); 998 bytes_[offset_++] = (byte) ((value >>> 24) & 0xff); 999 bytes_[offset_++] = (byte) ((value >>> 16) & 0xff); 1000 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 1001 bytes_[offset_++] = (byte) (value & 0xff); 1002 } 1003 1004 final void writeScalar8Bytes(int codePoint, long value) { 1007 ensureLength(offset_ + 12); 1008 bytes_[offset_++] = 0x00; 1009 bytes_[offset_++] = 0x0C; 1010 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1011 bytes_[offset_++] = (byte) (codePoint & 0xff); 1012 bytes_[offset_++] = (byte) ((value >>> 56) & 0xff); 1013 bytes_[offset_++] = (byte) ((value >>> 48) & 0xff); 1014 bytes_[offset_++] = (byte) ((value >>> 40) & 0xff); 1015 bytes_[offset_++] = (byte) ((value >>> 32) & 0xff); 1016 bytes_[offset_++] = (byte) ((value >>> 24) & 0xff); 1017 bytes_[offset_++] = (byte) ((value >>> 16) & 0xff); 1018 bytes_[offset_++] = (byte) ((value >>> 8) & 0xff); 1019 bytes_[offset_++] = (byte) (value & 0xff); 1020 } 1021 1022 final void writeLengthCodePoint(int length, int codePoint) { 1028 ensureLength(offset_ + 4); 1029 bytes_[offset_++] = (byte) ((length >>> 8) & 0xff); 1030 bytes_[offset_++] = (byte) (length & 0xff); 1031 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1032 bytes_[offset_++] = (byte) (codePoint & 0xff); 1033 } 1034 1035 final byte[] writeEXTDTALengthCodePointForEncryption(int length, int codePoint) { 1036 byte[] clearedBytes = new byte[4]; 1038 clearedBytes[0] = (byte) ((length >>> 8) & 0xff); 1039 clearedBytes[1] = (byte) (length & 0xff); 1040 clearedBytes[2] = (byte) ((codePoint >>> 8) & 0xff); 1041 clearedBytes[3] = (byte) (codePoint & 0xff); 1042 return clearedBytes; 1043 } 1044 1045 final void writeScalarBytes(int codePoint, byte[] buf, int length) { 1053 ensureLength(offset_ + length + 4); 1054 bytes_[offset_++] = (byte) (((length + 4) >>> 8) & 0xff); 1055 bytes_[offset_++] = (byte) ((length + 4) & 0xff); 1056 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1057 bytes_[offset_++] = (byte) (codePoint & 0xff); 1058 for (int i = 0; i < length; i++) { 1059 bytes_[offset_++] = buf[i]; 1060 } 1061 } 1062 1063 final void writeScalarHeader(int codePoint, int dataLength) { 1068 ensureLength(offset_ + dataLength + 4); 1069 bytes_[offset_++] = (byte) (((dataLength + 4) >>> 8) & 0xff); 1070 bytes_[offset_++] = (byte) ((dataLength + 4) & 0xff); 1071 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1072 bytes_[offset_++] = (byte) (codePoint & 0xff); 1073 } 1074 1075 final void writeScalarString(int codePoint, String string) throws SqlException { 1085 int stringLength = string.length(); 1086 ensureLength(offset_ + stringLength + 4); 1087 bytes_[offset_++] = (byte) (((stringLength + 4) >>> 8) & 0xff); 1088 bytes_[offset_++] = (byte) ((stringLength + 4) & 0xff); 1089 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1090 bytes_[offset_++] = (byte) (codePoint & 0xff); 1091 offset_ = ccsidManager_.convertFromUCS2(string, bytes_, offset_, netAgent_); 1092 } 1093 1094 final void writeScalarPaddedString(int codePoint, String string, int paddedLength) throws SqlException { 1108 int stringLength = string.length(); 1109 ensureLength(offset_ + paddedLength + 4); 1110 bytes_[offset_++] = (byte) (((paddedLength + 4) >>> 8) & 0xff); 1111 bytes_[offset_++] = (byte) ((paddedLength + 4) & 0xff); 1112 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1113 bytes_[offset_++] = (byte) (codePoint & 0xff); 1114 offset_ = ccsidManager_.convertFromUCS2(string, bytes_, offset_, netAgent_); 1115 for (int i = 0; i < paddedLength - stringLength; i++) { 1116 bytes_[offset_++] = ccsidManager_.space_; 1117 } 1118 } 1119 1120 final void writeScalarPaddedString(String string, int paddedLength) throws SqlException { 1131 int stringLength = string.length(); 1132 ensureLength(offset_ + paddedLength); 1133 offset_ = ccsidManager_.convertFromUCS2(string, bytes_, offset_, netAgent_); 1134 for (int i = 0; i < paddedLength - stringLength; i++) { 1135 bytes_[offset_++] = ccsidManager_.space_; 1136 } 1137 } 1138 1139 final void writeScalarBytes(int codePoint, byte[] buff) { 1145 int buffLength = buff.length; 1146 ensureLength(offset_ + buffLength + 4); 1147 bytes_[offset_++] = (byte) (((buffLength + 4) >>> 8) & 0xff); 1148 bytes_[offset_++] = (byte) ((buffLength + 4) & 0xff); 1149 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1150 bytes_[offset_++] = (byte) (codePoint & 0xff); 1151 System.arraycopy(buff, 0, bytes_, offset_, buffLength); 1152 offset_ += buffLength; 1153 } 1154 1155 final void writeScalarBytes(int codePoint, byte[] buff, int start, int length) { 1163 ensureLength(offset_ + length + 4); 1164 bytes_[offset_++] = (byte) (((length + 4) >>> 8) & 0xff); 1165 bytes_[offset_++] = (byte) ((length + 4) & 0xff); 1166 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1167 bytes_[offset_++] = (byte) (codePoint & 0xff); 1168 System.arraycopy(buff, start, bytes_, offset_, length); 1169 offset_ += length; 1170 } 1171 1172 final void writeScalarPaddedBytes(int codePoint, byte[] buff, int paddedLength, byte padByte) { 1181 int buffLength = buff.length; 1182 ensureLength(offset_ + paddedLength + 4); 1183 bytes_[offset_++] = (byte) (((paddedLength + 4) >>> 8) & 0xff); 1184 bytes_[offset_++] = (byte) ((paddedLength + 4) & 0xff); 1185 bytes_[offset_++] = (byte) ((codePoint >>> 8) & 0xff); 1186 bytes_[offset_++] = (byte) (codePoint & 0xff); 1187 System.arraycopy(buff, 0, bytes_, offset_, buffLength); 1188 offset_ += buffLength; 1189 1190 for (int i = 0; i < paddedLength - buffLength; i++) { 1191 bytes_[offset_++] = padByte; 1192 } 1193 } 1194 1195 final void writeScalarPaddedBytes(byte[] buff, int paddedLength, byte padByte) { 1200 int buffLength = buff.length; 1201 ensureLength(offset_ + paddedLength); 1202 System.arraycopy(buff, 0, bytes_, offset_, buffLength); 1203 offset_ += buffLength; 1204 1205 for (int i = 0; i < paddedLength - buffLength; i++) { 1206 bytes_[offset_++] = padByte; 1207 } 1208 } 1209 1210 protected void flush(java.io.OutputStream socketOutputStream) throws java.io.IOException { 1213 if (doesRequestContainData()) { 1214 finalizeDssLength(); 1215 sendBytes(socketOutputStream); 1216 } 1217 } 1218 1219 protected void sendBytes(java.io.OutputStream socketOutputStream) throws java.io.IOException { 1220 try { 1221 socketOutputStream.write(bytes_, 0, offset_); 1222 socketOutputStream.flush(); 1223 } finally { 1224 if (netAgent_.logWriter_ != null && passwordIncluded_) { 1225 maskOutPassword(); 1227 passwordIncluded_ = false; 1228 } 1229 if (netAgent_.loggingEnabled()) { 1230 ((NetLogWriter) netAgent_.logWriter_).traceProtocolFlow(bytes_, 1231 0, 1232 offset_, 1233 NetLogWriter.TYPE_TRACE_SEND, 1234 "Request", 1235 "flush", 1236 1); } 1238 clearBuffer(); 1239 } 1240 } 1241 1242 final void maskOutPassword() { 1243 try { 1244 String maskChar = "*"; 1245 StringBuffer mask = new StringBuffer (); 1247 for (int i = 0; i < passwordLength_; i++) { 1248 mask.append(maskChar); 1249 } 1250 ccsidManager_.convertFromUCS2(mask.toString(), bytes_, passwordStart_, netAgent_); 1252 } catch (SqlException sqle) { 1253 for (int i = 0; i < passwordLength_; i++) { 1256 bytes_[passwordStart_ + i] = (byte) 0xFF; 1257 } 1258 } 1259 } 1260 1261 final void writeByte(byte v) { 1263 ensureLength(offset_ + 1); 1264 bytes_[offset_++] = v; 1265 } 1266 1267 final void writeShort(short v) { 1269 ensureLength(offset_ + 2); 1270 org.apache.derby.client.am.SignedBinary.shortToBigEndianBytes(bytes_, offset_, v); 1271 offset_ += 2; 1272 } 1273 1274 void writeInt(int v) { 1276 ensureLength(offset_ + 4); 1277 org.apache.derby.client.am.SignedBinary.intToBigEndianBytes(bytes_, offset_, v); 1278 offset_ += 4; 1279 } 1280 1281 final void writeLong(long v) { 1283 ensureLength(offset_ + 8); 1284 org.apache.derby.client.am.SignedBinary.longToBigEndianBytes(bytes_, offset_, v); 1285 offset_ += 8; 1286 } 1287 1288 1290 protected void writeShortFdocaData(short v) { 1292 ensureLength(offset_ + 2); 1293 org.apache.derby.client.am.SignedBinary.shortToBigEndianBytes(bytes_, offset_, v); 1294 offset_ += 2; 1295 } 1296 1297 protected void writeIntFdocaData(int v) { 1299 ensureLength(offset_ + 4); 1300 org.apache.derby.client.am.SignedBinary.intToBigEndianBytes(bytes_, offset_, v); 1301 offset_ += 4; 1302 } 1303 1304 protected void writeLongFdocaData(long v) { 1306 ensureLength(offset_ + 8); 1307 org.apache.derby.client.am.SignedBinary.longToBigEndianBytes(bytes_, offset_, v); 1308 offset_ += 8; 1309 } 1310 1311 protected void writeFloat(float v) { 1313 ensureLength(offset_ + 4); 1314 org.apache.derby.client.am.FloatingPoint.floatToIeee754Bytes(bytes_, offset_, v); 1315 offset_ += 4; 1316 } 1317 1318 protected void writeDouble(double v) { 1320 ensureLength(offset_ + 8); 1321 org.apache.derby.client.am.FloatingPoint.doubleToIeee754Bytes(bytes_, offset_, v); 1322 offset_ += 8; 1323 } 1324 1325 final void writeBigDecimal(java.math.BigDecimal v, 1327 int declaredPrecision, 1328 int declaredScale) throws SqlException { 1329 ensureLength(offset_ + 16); 1330 int length = org.apache.derby.client.am.Decimal.bigDecimalToPackedDecimalBytes(bytes_, offset_, v, declaredPrecision, declaredScale); 1331 offset_ += length; 1332 } 1333 1334 final void writeDate(java.sql.Date date) throws SqlException { 1335 try 1336 { 1337 ensureLength(offset_ + 10); 1338 org.apache.derby.client.am.DateTime.dateToDateBytes(bytes_, offset_, date); 1339 offset_ += 10; 1340 } catch (java.io.UnsupportedEncodingException e) { 1341 throw new SqlException(netAgent_.logWriter_, 1342 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1343 "java.sql.Date", "DATE", e); 1344 } 1345 } 1346 1347 final void writeTime(java.sql.Time time) throws SqlException { 1348 try{ 1349 ensureLength(offset_ + 8); 1350 org.apache.derby.client.am.DateTime.timeToTimeBytes(bytes_, offset_, time); 1351 offset_ += 8; 1352 } catch(UnsupportedEncodingException e) { 1353 throw new SqlException(netAgent_.logWriter_, 1354 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1355 "java.sql.Time", "TIME", e); 1356 } 1357 } 1358 1359 final void writeTimestamp(java.sql.Timestamp timestamp) throws SqlException { 1360 try{ 1361 ensureLength(offset_ + 26); 1362 org.apache.derby.client.am.DateTime.timestampToTimestampBytes(bytes_, offset_, timestamp); 1363 offset_ += 26; 1364 }catch(UnsupportedEncodingException e) { 1365 throw new SqlException(netAgent_.logWriter_, 1366 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1367 "java.sql.Timestamp", "TIMESTAMP", e); 1368 } 1369 } 1370 1371 final void writeBoolean(boolean v) { 1374 ensureLength(offset_ + 1); 1375 bytes_[offset_++] = (byte) ((v ? 1 : 0) & 0xff); 1376 } 1377 1378 final void writeSingleorMixedCcsidLDString(String s, String encoding) throws SqlException { 1384 byte[] b; 1385 try { 1386 b = s.getBytes(encoding); 1387 } catch (UnsupportedEncodingException e) { 1388 throw new SqlException(netAgent_.logWriter_, 1389 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1390 "String", "byte", e); 1391 } 1392 if (b.length > 0x7FFF) { 1393 throw new SqlException(netAgent_.logWriter_, 1394 new ClientMessageId(SQLState.LANG_STRING_TOO_LONG), 1395 "32767"); 1396 } 1397 ensureLength(offset_ + b.length + 2); 1398 writeLDBytesX(b.length, b); 1399 } 1400 1401 1402 final void writeLDBytes(byte[] bytes) { 1403 ensureLength(offset_ + bytes.length + 2); 1404 writeLDBytesX(bytes.length, bytes); 1405 } 1406 1407 private final void writeLDBytesX(int ldSize, byte[] bytes) { 1413 bytes_[offset_++] = (byte) ((ldSize >>> 8) & 0xff); 1414 bytes_[offset_++] = (byte) (ldSize & 0xff); 1415 System.arraycopy(bytes, 0, bytes_, offset_, bytes.length); 1416 offset_ += bytes.length; 1417 } 1418 1419 final void writeDDMString(String s) throws SqlException { 1423 ensureLength(offset_ + s.length()); 1424 offset_ = ccsidManager_.convertFromUCS2(s, bytes_, offset_, netAgent_); 1425 } 1426 1427 1428 private byte[] buildLengthAndCodePointForEncryptedLob(int codePoint, 1429 int leftToRead, 1430 boolean writeNullByte, 1431 int extendedLengthByteCount) throws DisconnectException { 1432 byte[] lengthAndCodepoint = new byte[4]; 1433 byte[] extendedLengthBytes = new byte[extendedLengthByteCount]; 1434 1435 if (extendedLengthByteCount > 0) { 1436 lengthAndCodepoint = writeEXTDTALengthCodePointForEncryption(0x8004 + extendedLengthByteCount, codePoint); 1438 1439 if (writeNullByte) { 1440 1441 extendedLengthBytes = writeExtendedLengthBytesForEncryption(extendedLengthByteCount, leftToRead + 1); 1442 } else { 1443 extendedLengthBytes = writeExtendedLengthBytesForEncryption(extendedLengthByteCount, leftToRead); 1444 } 1445 } else { 1446 if (writeNullByte) { 1447 lengthAndCodepoint = writeEXTDTALengthCodePointForEncryption(leftToRead + 4 + 1, codePoint); 1448 } else { 1449 lengthAndCodepoint = writeEXTDTALengthCodePointForEncryption(leftToRead + 4, codePoint); 1450 } 1451 } 1452 1453 if (extendedLengthByteCount > 0) { 1454 byte[] newLengthAndCodepoint = new byte[4 + extendedLengthBytes.length]; 1455 System.arraycopy(lengthAndCodepoint, 0, newLengthAndCodepoint, 0, lengthAndCodepoint.length); 1456 System.arraycopy(extendedLengthBytes, 0, newLengthAndCodepoint, lengthAndCodepoint.length, extendedLengthBytes.length); 1457 lengthAndCodepoint = newLengthAndCodepoint; 1458 } 1459 1460 if (writeNullByte) { 1461 byte[] nullByte = new byte[1 + lengthAndCodepoint.length]; 1462 System.arraycopy(lengthAndCodepoint, 0, nullByte, 0, lengthAndCodepoint.length); 1463 nullByte[lengthAndCodepoint.length] = 0; 1464 lengthAndCodepoint = nullByte; 1465 } 1466 return lengthAndCodepoint; 1467 } 1468 1469 1470 private void buildLengthAndCodePointForLob(int codePoint, 1471 int leftToRead, 1472 boolean writeNullByte, 1473 int extendedLengthByteCount) throws DisconnectException { 1474 if (extendedLengthByteCount > 0) { 1475 writeLengthCodePoint(0x8004 + extendedLengthByteCount, codePoint); 1477 1478 if (writeNullByte) { 1479 writeExtendedLengthBytes(extendedLengthByteCount, leftToRead + 1); 1480 } else { 1481 writeExtendedLengthBytes(extendedLengthByteCount, leftToRead); 1482 } 1483 } else { 1484 if (writeNullByte) { 1485 writeLengthCodePoint(leftToRead + 4 + 1, codePoint); 1486 } else { 1487 writeLengthCodePoint(leftToRead + 4, codePoint); 1488 } 1489 } 1490 1491 if (writeNullByte) { 1493 write1Byte(0x0); 1494 } 1495 1496 } 1497 1498 public void setDssLengthLocation(int location) { 1499 dssLengthLocation_ = location; 1500 } 1501 1502 public void setCorrelationID(int id) { 1503 correlationID_ = id; 1504 } 1505} 1506 | Popular Tags |