📄 requeststream.java
字号:
write((byte) l);
write((byte) (l >> 8));
write((byte) (l >> 16));
write((byte) (l >> 24));
}
/**
* Write a String object to the output stream.
* If the TDS version is >= 7.0 write a UNICODE string otherwise
* wrote a translated byte stream.
*
* @param s The String to write.
* @throws IOException
*/
void write(String s) throws IOException {
if (socket.getTdsVersion() >= Driver.TDS70) {
int len = s.length();
for (int i = 0; i < len; ++i) {
int c = s.charAt(i);
if (bufferPtr == buffer.length) {
putPacket(0);
}
buffer[bufferPtr++] = (byte) c;
if (bufferPtr == buffer.length) {
putPacket(0);
}
buffer[bufferPtr++] = (byte) (c >> 8);
}
} else {
writeAscii(s);
}
}
/**
* Write a char array object to the output stream.
*
* @param s The char[] to write.
* @throws IOException
*/
void write(char s[], int off, int len) throws IOException {
int i = off;
int limit = (off + len) > s.length ? s.length : off + len;
for ( ; i < limit; i++) {
char c = s[i];
if (bufferPtr == buffer.length) {
putPacket(0);
}
buffer[bufferPtr++] = (byte) c;
if (bufferPtr == buffer.length) {
putPacket(0);
}
buffer[bufferPtr++] = (byte) (c >> 8);
}
}
/**
* Write a String to the output stream as translated bytes.
*
* @param s The String to write.
* @throws IOException
*/
void writeAscii(String s) throws IOException {
String charsetName = socket.getCharset();
if (charsetName != null) {
try {
write(s.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
write(s.getBytes());
}
} else {
write(s.getBytes());
}
}
/**
* Copy the contents of an InputStream to the server.
*
* @param in The InputStream to read.
* @param length The length of the stream.
* @throws IOException
*/
void writeStreamBytes(InputStream in, int length) throws IOException {
byte buffer[] = new byte[1024];
while (length > 0) {
int res = in.read(buffer);
if (res < 0) {
throw new java.io.IOException(
"Data in stream less than specified by length");
}
write(buffer, 0, res);
length -= res;
}
// XXX Not sure that this is actually an error
if (length < 0 || in.read() >= 0) {
throw new java.io.IOException(
"More data in stream than specified by length");
}
}
/**
* Copy the contents of a Reader stream to the server.
*
* @param in The Reader object with the data.
* @param length The length of the data in characters.
* @throws IOException
*/
void writeReaderChars(Reader in, int length) throws IOException {
char cbuffer[] = new char[512];
byte bbuffer[] = new byte[1024];
while (length > 0) {
int res = in.read(cbuffer);
if (res < 0) {
throw new java.io.IOException(
"Data in stream less than specified by length");
}
for (int i = 0, j = -1; i < res; i++) {
bbuffer[++j] = (byte) cbuffer[i];
bbuffer[++j] = (byte) (cbuffer[i] >> 8);
}
write(bbuffer, 0, res * 2);
length -= res;
}
// XXX Not sure that this is actually an error
if (length < 0 || in.read() >= 0) {
throw new java.io.IOException(
"More data in stream than specified by length");
}
}
/**
* Copy the contents of a Reader stream to the server as bytes.
* <p>
* NB. Only reliable where the charset is single byte.
*
* @param in The Reader object with the data.
* @param length The length of the data in bytes.
* @throws IOException
*/
void writeReaderBytes(Reader in, int length) throws IOException {
char buffer[] = new char[1024];
for (int i = 0; i < length;) {
int result = in.read(buffer);
if (result == -1) {
throw new java.io.IOException(
"Data in stream less than specified by length");
} else if (i + result > length) {
throw new java.io.IOException(
"More data in stream than specified by length");
}
write(Support.encodeString(socket.getCharset(), new String(buffer, 0, result)));
i += result;
}
}
/**
* Write a BigDecimal value to the output stream.
*
* @param value The BigDecimal value to write.
* @throws IOException
*/
void write(BigDecimal value) throws IOException {
if (value == null) {
write((byte) 0);
} else {
byte signum = (byte) (value.signum() < 0 ? 0 : 1);
BigInteger bi = value.unscaledValue();
byte mantisse[] = bi.abs().toByteArray();
byte len = (byte) (mantisse.length + 1);
if (len > getMaxDecimalBytes()) {
// Should never happen now as value is normalized elsewhere
throw new IOException("BigDecimal to big to send");
}
if (socket.serverType == Driver.SYBASE) {
write((byte) len);
// Sybase TDS5 stores MSB first opposite sign!
// length, prec, scale already sent in parameter descriptor.
write((byte) ((signum == 0) ? 1 : 0));
for (int i = 0; i < mantisse.length; i++) {
write((byte) mantisse[i]);
}
} else {
write((byte) len);
write((byte) signum);
for (int i = mantisse.length - 1; i >= 0; i--) {
write((byte) mantisse[i]);
}
}
}
}
/**
* Flush the packet to the output stream setting the last packet flag.
*
* @throws IOException
*/
void flush() throws IOException {
putPacket(1);
}
/**
* Close the output stream.
*/
void close() {
isClosed = true;
}
/**
* Retrieve the TDS version number.
*
* @return The TDS version as an <code>int</code>.
*/
int getTdsVersion() {
return socket.getTdsVersion();
}
/**
* Retrieve the Server type.
*
* @return The Server type as an <code>int</code>.
*/
int getServerType() {
return socket.serverType;
}
/**
* Write the TDS packet to the network.
*
* @param last Set to 1 if this is the last packet else 0.
* @throws IOException
*/
private void putPacket(int last) throws IOException {
if (isClosed) {
throw new IOException("RequestStream is closed");
}
buffer[0] = pktType;
buffer[1] = (byte) last; // last segment indicator
buffer[2] = (byte) (bufferPtr >> 8);
buffer[3] = (byte) bufferPtr;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = (byte) ((socket.getTdsVersion() >= Driver.TDS70) ? 1 : 0);
buffer[7] = 0;
if (Logger.isActive()) {
Logger.logPacket(streamId, false, buffer);
}
buffer = socket.sendNetPacket(streamId, buffer);
bufferPtr = TdsCore.PKT_HDR_LEN;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -