📄 tdscomm.java
字号:
//
// for(; i<length; i++)
// {
// appendByte(pad);
// }
// }
/**
* Send the logical packet.
* <p>
* Send the logical packet the has been constructed. */
public synchronized void sendPacket()
throws java.io.IOException
{
sendPhysicalPacket(true);
nextOutBufferIndex = 0;
packetType = 0;
notify();
}
/**
* store a byte of data at a particular location in the outBuffer.
*
* @param index position in outBuffer to store data
* @param value value to store in the outBuffer.
*/
private void storeByte(
int index,
byte value)
{
outBuffer[index] = value;
}
/**
* store a short integer of data at a particular location in the outBuffer.
*
* @param index position in outBuffer to store data
* @param value value to store in the outBuffer.
*/
private void storeShort(
int index,
short s)
{
outBuffer[index] = (byte)((s>>8) & 0xff);
outBuffer[index+1] = (byte)((s>>0) & 0xff);
}
/**
* send the data in the outBuffer.
* <p>
* Fill in the TDS packet header data and send the data in outBuffer
* to the DB server.
*
* @param isLastSegment is this the last physical packet that makes
* up the physical packet?
*/
private void sendPhysicalPacket(boolean isLastSegment)
throws java.io.IOException
{
if (nextOutBufferIndex>headerLength
|| packetType == CANCEL)
{
// packet type
storeByte(0, (byte)(packetType & 0xff));
storeByte(1, isLastSegment ? (byte)1 : (byte)0);
storeShort(2, (short)nextOutBufferIndex);
storeByte(4, (byte)0);
storeByte(5, (byte)0);
storeByte(6, (byte)(tdsVer == TDS70 ? 1 : 0));
storeByte(7, (byte)0);
//Dusan
z++;
/* if (z==7) {
storeByte(18, "1".getBytes()[0]);
storeByte(20, (byte)0);
storeByte(21, "#".getBytes()[0]);
storeByte(22, "1".getBytes()[0]);
storeByte(23, "(".getBytes()[0]);
storeByte(24, "@".getBytes()[0]);
storeByte(25, "P".getBytes()[0]);
storeByte(26, "1".getBytes()[0]);
storeByte(31, (byte)0);
}
*/
out.write(outBuffer, 0, nextOutBufferIndex);
packetsSent++;
for(int j=0; j < outBuffer.length; j++)
storeByte(j, (byte)0);
if (Logger.isActive())
{
String dump = HexDump.hexDump(outBuffer, nextOutBufferIndex);
String t = (new Timestamp(
System.currentTimeMillis())).toString();
Logger.println("Instance " + id + " @ " + t
+ " sent packet #" + packetsSent + "\n" + dump);
}
}
}
/**
* peek at the next byte of data.
* <p>
* This returns the next byte of data that would be returned
* by getByte(), but does not actually consume the data.
*
* <b>Note-</b> We can't synchronize this method (or most of the other
* methods in this class) because of the way cancels are handled.
* If a thread is waiting for a response from the server the
* cancelController class must be able to call sendPacket() to
* cancel the request.
*
* @return The next byte of data that will be returned by getByte()
*
* @exception com.internetcds.jdbc.tds.TdsException
* @exception java.io.IOException
*/
public byte peek()
throws com.internetcds.jdbc.tds.TdsException,
java.io.IOException
{
// XXX RACE CONDITION- It is possible that two threads
// could be modifying inBuffer at the same time. We need
// to synchronized based on inBuffer itself.
byte result = getByte();
backup();
return result;
}
/**
* put the most recently read byte of data back in the inBuffer.
* <p>
* This function effectivly ungets the last byte read.
* It is guaranteed to be able to unget the last byte read.
* Trying to unget multiple bytes is not recomended and will
* only work so long as all the bytes were in the same
* physical TDS network packet.
*
* @author Craig Spannring
*/
public void backup()
{
inBufferIndex--;
// make sure we have fallen of the beginning of the buffer
// throw an array out of bounds error if we've gone back too far.
byte b = inBuffer[inBufferIndex];
}
/**
* read a byte of data from the DB server.
* <p>
* This will return the next byte of data from the DB server.
* <p>
* <B>Warning</B> If there is not data available this method
* will block.
*/
public byte getByte()
throws com.internetcds.jdbc.tds.TdsException,
java.io.IOException
{
byte result;
if (inBufferIndex >= inBufferLen)
{
// out of data, read another physical packet.
getPhysicalPacket();
}
result = inBuffer[inBufferIndex++];
return result;
}
public byte[] getBytes(int len)
throws com.internetcds.jdbc.tds.TdsException,
java.io.IOException
{
byte result[] = new byte[len];
int i;
for(i=0; i<len; i++)
{
result[i] = getByte();
}
return result;
}
/**
* Reads bytes or characters (depending on TDS version) and constructs a
* string with them.
*
* Sybase will let the client choose byte ordering, but SQLServer 7.0
* wants little endian only. In the interest of simplicity, just use
* little endian regardless of the type of server.
*
* Added 2000-06-05.
*/
public String getString(int len)
throws com.internetcds.jdbc.tds.TdsException,
java.io.IOException
{
if (tdsVer == TDS70) {
char[] chars = new char[len];
for (int i = 0; i < len; ++i) {
int lo = getByte() & 0xFF;
int hi = getByte() & 0xFF;
chars[i] = (char)(lo | (hi << 8));
}
return new String(chars);
}
else
return new String(getBytes(len));
}
public void skip(int i)
throws com.internetcds.jdbc.tds.TdsException,
java.io.IOException
{
for(; i>0; i--)
{
getByte();
}
} // skip()
public int getNetShort()
throws TdsException, java.io.IOException
{
byte tmp[] = new byte[2];
tmp[0] = getByte();
tmp[1] = getByte();
return ntohs(tmp, 0);
}
public int getTdsShort()
throws com.internetcds.jdbc.tds.TdsException, java.io.IOException
{
int lo = ((int)getByte() & 0xff);
int hi = ((int)getByte() & 0xff) << 8;
return lo | hi;
}
public int getTdsInt()
throws com.internetcds.jdbc.tds.TdsException, java.io.IOException
{
int result;
int b1 = ((int)getByte() & 0xff);
int b2 = ((int)getByte() & 0xff) << 8;
int b3 = ((int)getByte() & 0xff) << 16;
int b4 = ((int)getByte() & 0xff) << 24;
result = b4 | b3 | b2 | b1;
return result;
}
public long getTdsInt64()
throws com.internetcds.jdbc.tds.TdsException, java.io.IOException
{
long b1 = ((long)getByte() & 0xff);
long b2 = ((long)getByte() & 0xff) << 8;
long b3 = ((long)getByte() & 0xff) << 16;
long b4 = ((long)getByte() & 0xff) << 24;
long b5 = ((long)getByte() & 0xff) << 32;
long b6 = ((long)getByte() & 0xff) << 40;
long b7 = ((long)getByte() & 0xff) << 48;
long b8 = ((long)getByte() & 0xff) << 56;
return b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8;
}
/**
* convert two bytes in a byte array into a Java short integer.
*
* @param buf array of data
* @param offset index into the buf array where the short integer
* is stored.
*/
private static int ntohs(byte buf[], int offset)
{
int lo = ((int)buf[offset+1] & 0xff);
int hi = (((int)buf[offset] & 0xff) << 8);
return hi | lo; // return an int since we really want an _unsigned_
}
/**
* Read a physical packet.
* <p>
* <B>Warning</B> This method will block until it gets all of the input.
*/
private void getPhysicalPacket()
throws TdsException, java.io.IOException
{
byte tmpBuf[] = new byte[8];
// read the header
for (int nread = 0; nread < 8; )
{
nread += in.read(tmpBuf, nread, 8 - nread);
}
if (Logger.isActive())
{
String dump = com.internetcds.util.HexDump.hexDump(tmpBuf, 8);
String t = (new Timestamp(
System.currentTimeMillis())).toString();
Logger.println("Instance " + id + " @ " + t
+ " recevied header #" + (packetsReceived+1)
+ "\n" + dump);
}
byte packetType = tmpBuf[0];
if (packetType!=LOGON
&& packetType!=QUERY
&& packetType!=REPLY)
{
throw new TdsUnknownPacketType(packetType, tmpBuf);
}
// figure out how many bytes are remaining in this packet.
int len = ntohs(tmpBuf, 2) - 8;
// Added 2000-06-05
if (len >= inBuffer.length)
{
inBuffer = new byte[len];
}
if (len < 0)
{
throw new TdsException("Confused by a length of " + len);
}
// now get the data
for (int nread = 0; nread < len; )
{
//Dusan1
nread += in.read(inBuffer, nread, len - nread);
}
packetsReceived++;
// adjust the bookkeeping info about the incoming buffer
inBufferLen = len;
inBufferIndex = 0;
if (Logger.isActive())
{
String dump = com.internetcds.util.HexDump.hexDump(inBuffer, len);
String t = (new Timestamp(
System.currentTimeMillis())).toString();
Logger.println("Instance " + id + " @ " + t
+ " recevied data #" + (packetsReceived)
+ "\n" + dump);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -