📄 requeststream.java
字号:
// jTDS JDBC Driver for Microsoft SQL Server and Sybase// Copyright (C) 2004 The jTDS Project//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package net.sourceforge.jtds.jdbc;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.io.InputStream;import java.io.Reader;import java.math.BigDecimal;import java.math.BigInteger;import net.sourceforge.jtds.util.*;/** * Class to implement an output stream for the server request. * <p> * Implementation note: * <ol> * <li>This class contains methods to write different types of data to the * server request stream in TDS format. * <li>Character translation of String items is carried out. * </ol> * * @author Mike Hutchinson. * @version $Id: RequestStream.java,v 1.16 2005/04/28 14:29:26 alin_sinpalean Exp $ */public class RequestStream { /** The shared network socket. */ private final SharedSocket socket; /** The output packet buffer. */ private byte[] buffer; /** The offset of the next byte to write. */ private int bufferPtr; /** The request packet type. */ private byte pktType; /** The unique stream id. */ private final int streamId; /** True if stream is closed. */ private boolean isClosed; /** The current output buffer size*/ private int bufferSize; /** The maximum decimal precision. */ private int maxPrecision; /** * Construct a RequestStream object. * * @param socket the shared socket object to write to * @param streamId the unique id for this stream * @param bufferSize the initial buffer size to use (the current network * packet size) */ RequestStream(SharedSocket socket, int streamId, int bufferSize) { this.streamId = streamId; this.socket = socket; this.bufferSize = bufferSize; this.buffer = new byte[bufferSize]; this.bufferPtr = TdsCore.PKT_HDR_LEN; } /** * Set the output buffer size * * @param size The new buffer size (>= {@link TdsCore#MIN_PKT_SIZE} <= {@link TdsCore#MAX_PKT_SIZE}). */ void setBufferSize(int size) { if (size < bufferPtr || size == bufferSize) { return; // Can't shrink buffer size; } if (size < TdsCore.MIN_PKT_SIZE || size > TdsCore.MAX_PKT_SIZE) { throw new IllegalArgumentException("Invalid buffer size parameter " + size); } byte[] tmp = new byte[size]; System.arraycopy(buffer, 0, tmp, 0, bufferPtr); buffer = tmp; } /** * Retrieve the current output packet size. * * @return the packet size as an <code>int</code>. */ int getBufferSize() { return bufferSize; } /** * Retrive the maximum decimal precision. * * @return The precision as an <code>int</code>. */ int getMaxPrecision() { return this.maxPrecision; } /** * Set the maximum decimal precision. * * @param precision The precision either 28 or 38. */ void setMaxPrecision(int precision) { this.maxPrecision = precision; } /** * Retrieve the unique stream id. * * @return the unique stream id as an <code>int</code>. */ int getStreamId() { return this.streamId; } /** * Set the current output packet type. * * @param pktType The packet type eg TdsCore.QUERY_PKT. */ void setPacketType(byte pktType) { this.pktType = pktType; } /** * Write a byte to the output stream. * * @param b The byte value to write. * @throws IOException */ void write(byte b) throws IOException { if (bufferPtr == buffer.length) { putPacket(0); } buffer[bufferPtr++] = b; } /** * Write an array of bytes to the output stream. * * @param b The byte array to write. * @throws IOException */ void write(byte[] b) throws IOException { int bytesToWrite = b.length; int off = 0; while (bytesToWrite > 0) { int available = buffer.length - bufferPtr; if (available == 0) { putPacket(0); continue; } int bc = (available > bytesToWrite) ? bytesToWrite : available; System.arraycopy(b, off, buffer, bufferPtr, bc); off += bc; bufferPtr += bc; bytesToWrite -= bc; } } /** * Write a partial byte buffer to the output stream. * * @param b The byte array buffer. * @param off The offset into the byte array. * @param len The number of bytes to write. * @throws IOException */ void write(byte[] b, int off, int len) throws IOException { int limit = (off + len) > b.length? b.length: off + len; int bytesToWrite = limit - off; int i = len - bytesToWrite; while (bytesToWrite > 0) { int available = buffer.length - bufferPtr; if (available == 0) { putPacket(0); continue; } int bc = (available > bytesToWrite)? bytesToWrite: available; System.arraycopy(b, off, buffer, bufferPtr, bc); off += bc; bufferPtr += bc; bytesToWrite -= bc; } for (; i > 0; i--) { write((byte) 0); } } /** * Write an int value to the output stream. * * @param i The int value to write. * @throws IOException */ void write(int i) throws IOException { write((byte) i); write((byte) (i >> 8)); write((byte) (i >> 16)); write((byte) (i >> 24)); } /** * Write a short value to the output stream. * * @param s The short value to write. * @throws IOException */ void write(short s) throws IOException { write((byte) s); write((byte) (s >> 8)); } /** * Write a long value to the output stream. * * @param l The long value to write. * @throws IOException */ void write(long l) throws IOException { write((byte) l); write((byte) (l >> 8)); write((byte) (l >> 16)); write((byte) (l >> 24)); write((byte) (l >> 32)); write((byte) (l >> 40)); write((byte) (l >> 48)); write((byte) (l >> 56)); } /** * Write a double value to the output stream. * * @param f The double value to write. * @throws IOException */ void write(double f) throws IOException { long l = Double.doubleToLongBits(f); write((byte) l); write((byte) (l >> 8)); write((byte) (l >> 16)); write((byte) (l >> 24)); write((byte) (l >> 32)); write((byte) (l >> 40)); write((byte) (l >> 48)); write((byte) (l >> 56)); } /** * Write a float value to the output stream. * * @param f The float value to write. * @throws IOException */ void write(float f) throws IOException { int l = Float.floatToIntBits(f); write((byte) l); write((byte) (l >> 8));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -