1 33 34 package com.internetcds.jdbc.tds; 35 36 import java.io.*; 37 import java.net.*; 38 import com.internetcds.util.HexDump; 39 import com.internetcds.util.Logger; 40 import java.sql.Timestamp ; 41 42 49 public class TdsComm implements TdsDefinitions 50 { 51 public static final String cvsVersion = "$Id: TdsComm.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $"; 52 static int z = 0; 54 55 static final int headerLength = 8; 56 57 public static final byte QUERY = 1; 63 public static final byte LOGON = 2; 64 public static final byte PROC = 3; 65 public static final byte REPLY = 4; 66 public static final byte CANCEL = 6; 67 public static final byte LOGON70 = 16; 69 70 private static final int maxPacketLength = 4096; 77 78 private DataOutputStream out = null; 81 private DataInputStream in = null; 82 83 84 byte outBuffer[]; 87 88 int nextOutBufferIndex = 0; 91 92 int packetType = 0; 95 96 97 byte inBuffer[]; 99 100 int inBufferIndex = 0; 102 103 int inBufferLen = 0; 106 107 int packetsSent = 0; 109 int packetsReceived = 0; 110 111 static int id = 0; 114 115 117 private int tdsVer = TDS42; 118 119 public TdsComm(Socket sock, int tdsVer_) 120 throws java.io.IOException 121 { 122 out = new DataOutputStream(sock.getOutputStream()); 123 in = new DataInputStream(sock.getInputStream()); 124 outBuffer = new byte[4096]; 125 inBuffer = new byte[4096]; 126 tdsVer = tdsVer_; 128 129 id++; 130 } 131 132 public void close() 133 { 134 } 136 137 138 147 public synchronized void startPacket(int type) 148 { 149 while(someThreadIsBuildingPacket()) 152 { 153 try 154 { 155 wait(); 156 } 157 catch (java.lang.InterruptedException e) 158 { 159 } 161 } 162 163 packetType = type; 164 nextOutBufferIndex = headerLength; 165 } 166 167 172 public boolean someThreadIsBuildingPacket() 173 { 174 return packetType!=0; 175 } 176 177 178 187 public void appendByte(byte b) 188 throws java.io.IOException 189 { 190 if (nextOutBufferIndex == maxPacketLength) 191 { 192 sendPhysicalPacket(false); 195 nextOutBufferIndex = headerLength; 196 } 197 198 storeByte(nextOutBufferIndex, b); 199 nextOutBufferIndex++; 200 201 202 } 204 205 210 public void appendBytes(byte[] b) 211 throws java.io.IOException 212 { 213 appendBytes(b, b.length, (byte)0); 214 } 216 217 218 225 public void appendBytes(byte[] b, int len, byte pad) 226 throws java.io.IOException 227 { 228 int i = 0; 229 for (; i<b.length && i<len; i++) 230 { 231 appendByte(b[i]); 232 } 233 for (; i<len; i++) 234 { 235 appendByte(pad); 236 } 237 } 238 239 240 245 public void appendShort(short s) 246 throws java.io.IOException 247 { 248 appendByte((byte)((s>>8)&0xff)); 249 appendByte((byte)((s>>0)&0xff)); 250 } 251 252 257 public void appendTdsShort(short s) 258 throws java.io.IOException 259 { 260 appendByte((byte)((s>>0)&0xff)); 261 appendByte((byte)((s>>8)&0xff)); 262 } 263 264 265 273 public void appendFlt8(Double value) 274 throws java.io.IOException 275 { 276 long l = Double.doubleToLongBits(value.doubleValue()); 277 278 appendByte((byte)((l>>0)&0xff)); 279 appendByte((byte)((l>>8)&0xff)); 280 appendByte((byte)((l>>16)&0xff)); 281 appendByte((byte)((l>>24)&0xff)); 282 appendByte((byte)((l>>32)&0xff)); 283 appendByte((byte)((l>>40)&0xff)); 284 appendByte((byte)((l>>48)&0xff)); 285 appendByte((byte)((l>>56)&0xff)); 286 } 287 288 public void appendInt(int i) 289 throws java.io.IOException 290 { 291 appendByte((byte)((i>>24)&0xff)); 292 appendByte((byte)((i>>16)&0xff)); 293 appendByte((byte)((i>>8)&0xff)); 294 appendByte((byte)((i>>0)&0xff)); 295 } 296 297 public void appendTdsInt(int i) 298 throws java.io.IOException 299 { 300 appendByte((byte)((i>>0)&0xff)); 301 appendByte((byte)((i>>8)&0xff)); 302 appendByte((byte)((i>>16)&0xff)); 303 appendByte((byte)((i>>24)&0xff)); 304 } 305 306 307 public void appendInt64(long i) 308 throws java.io.IOException 309 { 310 appendByte((byte)((i>>56)&0xff)); 311 appendByte((byte)((i>>48)&0xff)); 312 appendByte((byte)((i>>40)&0xff)); 313 appendByte((byte)((i>>32)&0xff)); 314 appendByte((byte)((i>>24)&0xff)); 315 appendByte((byte)((i>>16)&0xff)); 316 appendByte((byte)((i>>8)&0xff)); 317 appendByte((byte)((i>>0)&0xff)); 318 } 319 320 329 public void appendChars(String s) throws java.io.IOException { 330 331 for (int i = 0; i < s.length(); ++i) { 332 int c = s.charAt(i); 333 byte b1 = (byte)(c & 0xFF); 334 byte b2 = (byte)((c >> 8) & 0xFF); 335 appendByte(b1); 336 appendByte(b2); 337 } 338 } 339 340 354 378 379 383 public synchronized void sendPacket() 384 throws java.io.IOException 385 { 386 sendPhysicalPacket(true); 387 nextOutBufferIndex = 0; 388 packetType = 0; 389 notify(); 390 } 391 392 393 399 private void storeByte( 400 int index, 401 byte value) 402 { 403 outBuffer[index] = value; 404 405 } 406 407 413 private void storeShort( 414 int index, 415 short s) 416 { 417 outBuffer[index] = (byte)((s>>8) & 0xff); 418 outBuffer[index+1] = (byte)((s>>0) & 0xff); 419 } 420 421 422 431 private void sendPhysicalPacket(boolean isLastSegment) 432 throws java.io.IOException 433 { 434 if (nextOutBufferIndex>headerLength 435 || packetType == CANCEL) 436 { 437 storeByte(0, (byte)(packetType & 0xff)); 439 storeByte(1, isLastSegment ? (byte)1 : (byte)0); 440 storeShort(2, (short)nextOutBufferIndex); 441 storeByte(4, (byte)0); 442 storeByte(5, (byte)0); 443 storeByte(6, (byte)(tdsVer == TDS70 ? 1 : 0)); 444 storeByte(7, (byte)0); 445 z++; 447 461 out.write(outBuffer, 0, nextOutBufferIndex); 462 packetsSent++; 463 for(int j=0; j < outBuffer.length; j++) 464 storeByte(j, (byte)0); 465 466 467 if (Logger.isActive()) 468 { 469 String dump = HexDump.hexDump(outBuffer, nextOutBufferIndex); 470 String t = (new Timestamp ( 471 System.currentTimeMillis())).toString(); 472 Logger.println("Instance " + id + " @ " + t 473 + " sent packet #" + packetsSent + "\n" + dump); 474 } 475 } 476 } 477 478 495 public byte peek() 496 throws com.internetcds.jdbc.tds.TdsException, 497 java.io.IOException 498 { 499 500 byte result = getByte(); 504 backup(); 505 return result; 506 } 507 508 509 520 public void backup() 521 { 522 inBufferIndex--; 523 524 byte b = inBuffer[inBufferIndex]; 527 } 528 529 530 538 public byte getByte() 539 throws com.internetcds.jdbc.tds.TdsException, 540 java.io.IOException 541 { 542 byte result; 543 544 if (inBufferIndex >= inBufferLen) 545 { 546 getPhysicalPacket(); 548 } 549 550 result = inBuffer[inBufferIndex++]; 551 return result; 552 } 553 554 public byte[] getBytes(int len) 555 throws com.internetcds.jdbc.tds.TdsException, 556 java.io.IOException 557 { 558 byte result[] = new byte[len]; 559 int i; 560 561 for(i=0; i<len; i++) 562 { 563 result[i] = getByte(); 564 } 565 566 return result; 567 } 568 569 579 public String getString(int len) 580 throws com.internetcds.jdbc.tds.TdsException, 581 java.io.IOException 582 { 583 if (tdsVer == TDS70) { 584 char[] chars = new char[len]; 585 for (int i = 0; i < len; ++i) { 586 int lo = getByte() & 0xFF; 587 int hi = getByte() & 0xFF; 588 chars[i] = (char)(lo | (hi << 8)); 589 } 590 return new String (chars); 591 } 592 else 593 return new String (getBytes(len)); 594 } 595 596 public void skip(int i) 597 throws com.internetcds.jdbc.tds.TdsException, 598 java.io.IOException 599 { 600 for(; i>0; i--) 601 { 602 getByte(); 603 } 604 } 606 public int getNetShort() 607 throws TdsException, java.io.IOException 608 { 609 byte tmp[] = new byte[2]; 610 tmp[0] = getByte(); 611 tmp[1] = getByte(); 612 return ntohs(tmp, 0); 613 } 614 615 public int getTdsShort() 616 throws com.internetcds.jdbc.tds.TdsException, java.io.IOException 617 { 618 int lo = ((int)getByte() & 0xff); 619 int hi = ((int)getByte() & 0xff) << 8; 620 return lo | hi; 621 } 622 623 public int getTdsInt() 624 throws com.internetcds.jdbc.tds.TdsException, java.io.IOException 625 { 626 int result; 627 628 int b1 = ((int)getByte() & 0xff); 629 int b2 = ((int)getByte() & 0xff) << 8; 630 int b3 = ((int)getByte() & 0xff) << 16; 631 int b4 = ((int)getByte() & 0xff) << 24; 632 633 result = b4 | b3 | b2 | b1; 634 635 return result; 636 } 637 638 public long getTdsInt64() 639 throws com.internetcds.jdbc.tds.TdsException, java.io.IOException 640 { 641 long b1 = ((long)getByte() & 0xff); 642 long b2 = ((long)getByte() & 0xff) << 8; 643 long b3 = ((long)getByte() & 0xff) << 16; 644 long b4 = ((long)getByte() & 0xff) << 24; 645 long b5 = ((long)getByte() & 0xff) << 32; 646 long b6 = ((long)getByte() & 0xff) << 40; 647 long b7 = ((long)getByte() & 0xff) << 48; 648 long b8 = ((long)getByte() & 0xff) << 56; 649 return b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8; 650 } 651 652 653 660 private static int ntohs(byte buf[], int offset) 661 { 662 int lo = ((int)buf[offset+1] & 0xff); 663 int hi = (((int)buf[offset] & 0xff) << 8); 664 665 return hi | lo; } 667 668 673 private void getPhysicalPacket() 674 throws TdsException, java.io.IOException 675 { 676 byte tmpBuf[] = new byte[8]; 677 678 679 for (int nread = 0; nread < 8; ) 681 { 682 nread += in.read(tmpBuf, nread, 8 - nread); 683 } 684 if (Logger.isActive()) 685 { 686 String dump = com.internetcds.util.HexDump.hexDump(tmpBuf, 8); 687 String t = (new Timestamp ( 688 System.currentTimeMillis())).toString(); 689 690 Logger.println("Instance " + id + " @ " + t 691 + " recevied header #" + (packetsReceived+1) 692 + "\n" + dump); 693 } 694 byte packetType = tmpBuf[0]; 695 if (packetType!=LOGON 696 && packetType!=QUERY 697 && packetType!=REPLY) 698 { 699 throw new TdsUnknownPacketType(packetType, tmpBuf); 700 } 701 int len = ntohs(tmpBuf, 2) - 8; 703 if (len >= inBuffer.length) 705 { 706 inBuffer = new byte[len]; 707 } 708 if (len < 0) 709 { 710 throw new TdsException("Confused by a length of " + len); 711 } 712 713 for (int nread = 0; nread < len; ) 715 { 716 718 nread += in.read(inBuffer, nread, len - nread); 719 } 720 packetsReceived++; 721 722 723 inBufferLen = len; 725 inBufferIndex = 0; 726 727 if (Logger.isActive()) 728 { 729 String dump = com.internetcds.util.HexDump.hexDump(inBuffer, len); 730 String t = (new Timestamp ( 731 System.currentTimeMillis())).toString(); 732 733 Logger.println("Instance " + id + " @ " + t 734 + " recevied data #" + (packetsReceived) 735 + "\n" + dump); 736 } 737 } 738 } 739 740 | Popular Tags |