TdsStream.java

Index Score
net.sourceforge.jtds.jdbc
jTDS

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
DECL_COMMENTSComments in declarations
RETURNSNumber of return points from functions
INTERFACE_COMPLEXITYInterface complexity
LOOPSNumber of loops
JAVA0076JAVA0076 Use of magic number
DOC_COMMENTNumber of javadoc comment lines
LINE_COMMENTNumber of line comments
SIZESize of the file in bytes
COMMENTSComment lines
CYCLOMATICCyclomatic complexity
LOGICAL_LINESNumber of statements
OPERATORSNumber of operators
JAVA0067JAVA0067 Array descriptor on identifier name
PROGRAM_LENGTHHalstead program length
LINESNumber of lines in the source file
COMPARISONSNumber of comparison operators
BLOCKSNumber of blocks
ELOCEffective lines of code
OPERANDSNumber of operands
LOCLines of code
FUNCTIONSNumber of function declarations
PARAMSNumber of formal parameter declarations
PROGRAM_VOCABHalstead program vocabulary
UNIQUE_OPERANDSNumber of unique operands
JAVA0082JAVA0082 Unnecessary widening cast
EXEC_COMMENTSComments in executable code
JAVA0034JAVA0034 Missing braces in if statement
JAVA0177JAVA0177 Variable declaration missing initializer
UNIQUE_OPERATORSNumber of unique operators
JAVA0128JAVA0128 Public constructor in non-public class
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
EXITSProcedure exits
JAVA0117JAVA0117 Missing javadoc: method 'method'
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0123JAVA0123 Use all three components of for loop
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
NEST_DEPTHMaximum nesting depth
JAVA0145JAVA0145 Tab character used in source file
// 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.InputStream; import java.nio.charset.Charset; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.CharBuffer; /** * This class contains methods to read or write data to and from the TDS data stream. * @author Mike Hutchinson. * */ public final class TdsStream { /** The shared network socket. */ private final TdsSocket socket; /** The output packet buffer. */ private byte[] outBuffer; /** The offset of the next byte to write. */ private int outBufferPtr; /** The request packet type. */ private byte pktType; /** True if stream is closed. */ private boolean isClosed; /** The current output buffer size*/ private int bufferSize; /** The Input packet buffer. */ private byte[] inBuffer; /** The offset of the next byte to read. */ private int inBufferPtr; /** The length of current input packet. */ private int inBufferLen; /** A shared byte buffer. */ private final byte[] byteBuffer = new byte[8000]; /** A shared char buffer. */ private final char[] charBuffer = new char[4000]; /** The parent TdsCore object. */ private final TdsCore tds; /** Cached TdsInputStream instance. */ private final TdsInputStream tdsInputStream = new TdsInputStream(this); /** Cached Tds90InputStream instance. */ private final Tds90InputStream tds90InputStream = new Tds90InputStream(this); /** The byte to char map. */ private char byteToChar[]; /** The char to byte map. */ private byte charToByte[]; /** default Charset Name. */ private String charsetName; /** High speed character translation mapping table. */ private static java.util.HashMap<String, Object[]> charsetMap = new java.util.HashMap<String, Object[]>(); /** Maximum index for byte maps. */ private static int MAX_BYTE_INDEX = 256; /** Maximum index for char maps. */ private static int MAX_CHAR_INDEX = 65536; /** Standard replacement character for unsupported mappings. */ private static byte REPLACEMENT_CHAR = (byte)0x3F; /** * Construct a RequestStream object. * * @param tds the TdcCore instance for this stream * @param socket the IO socket. */ TdsStream(TdsCore tds, TdsSocket socket) { this.tds = tds; this.socket = socket; this.bufferSize = TdsCore.MIN_PKT_SIZE; this.outBuffer = new byte[bufferSize]; this.outBufferPtr = TdsCore.PKT_HDR_LEN; this.inBuffer = new byte[bufferSize]; this.inBufferLen = bufferSize; this.inBufferPtr = bufferSize; } /** * Set the default character set for this connection stream. * <p/>For single byte charset a simple lookup table is used to do * the conversions. This is roughly 3 times faster than the Charset * encoder/decoder. The mapping tables are shared between connections. * @param cs the charset instance. */ void setCharset(Charset cs) { charsetName = null; if (cs.newEncoder().maxBytesPerChar() > 1.0) { // Only worth doing for single byte character sets return; } String csName = cs.name(); synchronized (charsetMap) { Object maps[] = charsetMap.get(csName); if (maps == null) { byte page[] = new byte[MAX_BYTE_INDEX]; for (int i = 0; i < MAX_BYTE_INDEX; i++) { page[i] = (byte)i; } char charMap[]; try { charMap = new String(page, cs.name()).toCharArray(); if (charMap.length != MAX_BYTE_INDEX) { return; // Should 256 if really max 1 byte per char! } } catch (java.io.UnsupportedEncodingException e) { return; // Should not occur } byte byteMap[] = new byte[MAX_CHAR_INDEX]; for (int i = 0; i < MAX_CHAR_INDEX; i++) { byteMap[i] = REPLACEMENT_CHAR; } for (int i = 0; i < MAX_BYTE_INDEX; i++) { byteMap[charMap[i]] = (byte)i; } maps = new Object[2]; maps[0] = charMap; maps[1] = byteMap; charsetMap.put(csName, maps); } byteToChar = (char[])maps[0]; charToByte = (byte[])maps[1]; charsetName = csName; } } /** * 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 < outBufferPtr || 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(outBuffer, 0, tmp, 0, outBufferPtr); outBuffer = tmp; } /** * 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 (outBufferPtr == outBuffer.length) { putPacket(0); } outBuffer[outBufferPtr++] = 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 = outBuffer.length - outBufferPtr; if (available == 0) { putPacket(0); continue; } int bc = (available > bytesToWrite) ? bytesToWrite : available; System.arraycopy(b, off, outBuffer, outBufferPtr, bc); off += bc; outBufferPtr += bc; bytesToWrite -= bc; } } /** * Write a ByteBuffer to the output stream. * * @param bb The ByteBuffer to write. * @throws IOException */ void write(ByteBuffer bb) throws IOException { int bytesToWrite = bb.remaining(); while (bytesToWrite > 0) { int available = outBuffer.length - outBufferPtr; if (available == 0) { putPacket(0); continue; } int bc = (available > bytesToWrite) ? bytesToWrite : available; bb.get(outBuffer, outBufferPtr, bc); outBufferPtr += 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 = outBuffer.length - outBufferPtr; if (available == 0) { putPacket(0); continue; } int bc = (available > bytesToWrite)? bytesToWrite: available; System.arraycopy(b, off, outBuffer, outBufferPtr, bc); off += bc; outBufferPtr += 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)); write((byte) (l >> 16)); write((byte) (l >> 24)); } /** * Write a String to the output stream as translated bytes. * * @param s The String to write. * @param info The CharsetInfo instance defining the charset. * @throws IOException */ void write(String s, Charset info) throws IOException { int len = s.length(); char chars[] = (len > charBuffer.length)? new char[len]: charBuffer; s.getChars(0, len, chars, 0); write(chars, len, info); } /** * Write a String to the output stream as translated bytes. * * @param chars the char[] to write. * @param len the length of the string. * @param info the CharsetInfo instance defining the charset. * @throws IOException */ void write(char chars[], int len, Charset info) throws IOException { if (info.name().equals(charsetName)) { // OK Use our highspeed converter byte bytes[] = (len > byteBuffer.length)? new byte[len]: byteBuffer; for (int i = 0; i < len; i++) { bytes[i] = charToByte[chars[i]]; } write(bytes, 0, len); } else { // Fall back on the JVM routines CharBuffer cb = CharBuffer.wrap(chars, 0, len); ByteBuffer bb = info.encode(cb); len = bb.remaining(); byte bytes[] = (len > byteBuffer.length)? new byte[len]: byteBuffer; bb.get(bytes, 0, len); write(bytes, 0, len); } } /** * Write a String object to the output stream as unicode. * * @param chars the char[] to write. * @param len the length of the array to write. * @throws IOException */ void writeUnicode(char chars[], int len) throws IOException { int src = 0; while (src < len) { int available = outBuffer.length - outBufferPtr; if (available == 0) { putPacket(0); available = outBuffer.length - outBufferPtr; } if (available == 1) { write((byte)chars[src]); putPacket(0); write((byte)(chars[src++] >> 8)); available = outBuffer.length - outBufferPtr; if (src == len) { break; } } available /= 2; byte buf[] = outBuffer; int ptr = outBufferPtr; int limit = src + ((len - src > available)? available: len - src); while (src < limit) { int c = chars[src++]; buf[ptr] = (byte)c; buf[ptr + 1] = (byte)(c >> 8); ptr += 2; } outBufferPtr = ptr; } } /** * Write a String object to the output stream as unicode. * * @param s The String to write. * @throws IOException */ void writeUnicode(String s) throws IOException { int len = s.length(); char chars[] = (len > charBuffer.length)? new char[len]: charBuffer; s.getChars(0, len, chars, 0); writeUnicode(chars, len); } /** * Output a java.sql.Date/Time/Timestamp value to the server * as a Sybase datetime value. * * @param value the date value to write */ void write(DateTime value) throws IOException { if (value == null) { write((byte) 0); return; } write((byte) 8); write((int)value.getDate()); write((int)value.getTime()); } /** * Write a BigDecimal value to the output stream. * * @param value The BigDecimal value to write. * @param serverType the server type (sqlserver, sybase). * @throws IOException */ void write(BigDecimal value, int serverType) 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 > 17) { // Should never happen now as value is normalized elsewhere throw new IOException("BigDecimal to big to send"); } if (serverType == TdsCore.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); } /** * 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(Messages.get("error.io.outclosed")); } outBuffer[0] = pktType; outBuffer[1] = (byte) last; // last segment indicator outBuffer[2] = (byte) (outBufferPtr >> 8); outBuffer[3] = (byte) outBufferPtr; outBuffer[4] = 0; outBuffer[5] = 0; outBuffer[6] = (byte) ((tds.getTdsVersion() >= TdsCore.TDS70) ? 1 : 0); outBuffer[7] = 0; socket.sendBytes(outBuffer); outBufferPtr = TdsCore.PKT_HDR_LEN; } /** * Retrieves the next input byte without reading forward. * * @return the next byte in the input stream as an <code>int</code> * @throws IOException if an I/O error occurs */ int peek() throws IOException { if (inBufferPtr >= inBufferLen) { getPacket(); } return (int) inBuffer[inBufferPtr] & 0xFF; } /** * Reads the next input byte from the server response stream. * * @return the next byte in the input stream as an <code>int</code> * @throws IOException if an I/O error occurs */ int read() throws IOException { if (inBufferPtr >= inBufferLen) { getPacket(); } return (int) inBuffer[inBufferPtr++] & 0xFF; } /** * Reads a byte array from the server response stream. * * @param b the byte array to read into * @return the number of bytes read as an <code>int</code> * @throws IOException if an I/O error occurs */ int read(byte[] b) throws IOException { return read(b, 0, b.length); } /** * Reads a byte array from the server response stream, specifying a start * offset and length. * * @param b the byte array * @param off the starting offset in the array * @param len the number of bytes to read * @return the number of bytes read as an <code>int</code> * @throws IOException if an I/O error occurs */ int read(byte[] b, int off, int len) throws IOException { int bytesToRead = len; while (bytesToRead > 0) { if (inBufferPtr >= inBufferLen) { getPacket(); } int available = inBufferLen - inBufferPtr; int bc = (available > bytesToRead) ? bytesToRead : available; System.arraycopy(inBuffer, inBufferPtr, b, off, bc); off += bc; bytesToRead -= bc; inBufferPtr += bc; } return len; } /** * Reads a <code>String</code> from the server response stream, creating * it from a translated <code>byte</code> array. * @param len the length of the string to read <b>in bytes</b> * @param info descriptor of the charset to use * @return the result as a <code>String</code> * @throws IOException if an I/O error occurs */ String readString(int len, Charset info) throws IOException { byte[] bytes = (len > byteBuffer.length) ? new byte[len] : byteBuffer; read(bytes, 0, len); if (info.name().equals(charsetName)) { // OK we have a high speed mapping for this one char[] chars = (len > charBuffer.length)? new char[len]: charBuffer; for (int i = 0; i < len; i++) { chars[i] = byteToChar[bytes[i] & 0xFF]; } return new String(chars, 0, len); } // // Fall back on the full (but slow) Charset decoder. // return info.decode(ByteBuffer.wrap(bytes, 0, len)).toString(); } /** * Reads a <code>String</code> object from the server response stream. * @param len the length of the string to read <b>in characters</b> * @return the result as a <code>String</code> * @throws IOException if an I/O error occurs */ String readUnicode(int len) throws IOException { char[] chars = (len > charBuffer.length) ? new char[len] : charBuffer; int dest = 0; while (dest < len) { int available = inBufferLen - inBufferPtr; if (available == 0) { getPacket(); available = inBufferLen - inBufferPtr; } if (available == 1) { // A unicode char might be split over two packets. int b1 = read(); chars[dest++] = (char)(b1 | (read() << 8)); available = inBufferLen - inBufferPtr; if (dest == len) { break; } } available /= 2; byte buf[] = inBuffer; int ptr = inBufferPtr; int limit = dest + ((len - dest > available)? available: len - dest); while (dest < limit) { // High speed copy using local variables chars[dest++] = (char)((buf[ptr] & 0xFF) | (buf[ptr+1] << 8)); ptr += 2; } inBufferPtr = ptr; } return new String(chars, 0, len); } /** * Reads a <code>short</code> value from the server response stream. * * @return the result as a <code>short</code> * @throws IOException if an I/O error occurs */ short readShort() throws IOException { if (inBufferPtr >= inBufferLen) { getPacket(); } int b1 = inBuffer[inBufferPtr++] & 0xFF; if (inBufferPtr >= inBufferLen) { getPacket(); } int b2 = inBuffer[inBufferPtr++] << 8; return (short) (b1 | b2); } /** * Reads an <code>int</code> value from the server response stream. * * @return the result as a <code>int</code> * @throws IOException if an I/O error occurs */ int readInt() throws IOException { if (inBufferLen - inBufferPtr > 3) { int b1 = inBuffer[inBufferPtr++] & 0xFF; int b2 = inBuffer[inBufferPtr++] & 0xFF; int b3 = inBuffer[inBufferPtr++] & 0xFF; return (inBuffer[inBufferPtr++] << 24) | (b3 << 16) | (b2 << 8) | b1; } int b1 = read(); int b2 = read() << 8; int b3 = read() << 16; int b4 = read() << 24; return b4 | b3 | b2 | b1; } /** * Reads a <code>long</code> value from the server response stream. * * @return the result as a <code>long</code> * @throws IOException if an I/O error occurs */ long readLong() throws IOException { if (inBufferLen - inBufferPtr > 7) { long b1 = inBuffer[inBufferPtr++] & 0xFFL; long b2 = (inBuffer[inBufferPtr++] & 0xFFL) << 8; long b3 = (inBuffer[inBufferPtr++] & 0xFFL) << 16; long b4 = (inBuffer[inBufferPtr++] & 0xFFL) << 24; long b5 = (inBuffer[inBufferPtr++] & 0xFFL) << 32; long b6 = (inBuffer[inBufferPtr++] & 0xFFL) << 40; long b7 = (inBuffer[inBufferPtr++] & 0xFFL) << 48; long b8 = (long)inBuffer[inBufferPtr++] << 56; return b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8; } long b1 = ((long) read()); long b2 = ((long) read()) << 8; long b3 = ((long) read()) << 16; long b4 = ((long) read()) << 24; long b5 = ((long) read()) << 32; long b6 = ((long) read()) << 40; long b7 = ((long) read()) << 48; long b8 = ((long) read()) << 56; return b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8; } /** * Reads an <code>unsigned long</code> value from the server response stream. * * @return the result as a <code>BigDecimal</code> * @throws IOException if an I/O error occurs */ BigDecimal readUnsignedLong() throws IOException { int b1 = read(); long b2 = read(); long b3 = ((long) read()) << 8; long b4 = ((long) read()) << 16; long b5 = ((long) read()) << 24; long b6 = ((long) read()) << 32; long b7 = ((long) read()) << 40; long b8 = ((long) read()) << 48; // Convert via String as BigDecimal(long) is actually BigDecimal(double) // on older versions of java return new BigDecimal(Long.toString(b2 | b3 | b4 | b5 | b6 | b7 | b8)) .multiply(new BigDecimal(256)) .add(new BigDecimal(b1)); } /** * Read a MONEY value from the server response stream. * * @param len the length of the money type. * @return The java.math.BigDecimal value or null. * @throws IOException */ BigDecimal readMoney(int len) throws IOException { if (len == 4) { return BigDecimal.valueOf(readInt(), 4); } else if (len == 8) { long msw = (long)readInt() << 32; long lsw = readInt() & 0xFFFFFFFFL; return BigDecimal.valueOf((lsw | msw), 4); } else if (len != 0) { throw new IOException("Invalid money value."); } return null; } /** * Get a DATETIME value from the server response stream. * * @param len the length of the datatime data type. * @return The java.sql.Timestamp value or null. * @throws java.io.IOException */ DateTime readDatetime(final int len) throws IOException { int daysSince1900; int time; int minutes; switch (len) { case 0: return null; case 8: // A datetime is made of of two 32 bit integers // The first one is the number of days since 1900 // The second integer is the number of seconds*300 // Negative days indicate dates earlier than 1900. // The full range is 1753-01-01 to 9999-12-31. daysSince1900 = readInt(); time = readInt(); return new DateTime(daysSince1900, time); case 4: // A smalldatetime is two 16 bit integers. // The first is the number of days past January 1, 1900, // the second smallint is the number of minutes past // midnight. // The full range is 1900-01-01 to 2079-06-06. daysSince1900 = readShort() & 0xFFFF; minutes = readShort(); return new DateTime((short) daysSince1900, (short) minutes); default: throw new IOException("Invalid DATETIME value with size of " + len + " bytes."); } } /** * Discards bytes from the server response stream. * * @param skip the number of bytes to discard * @return the number of bytes skipped */ int skip(int skip) throws IOException { int tmp = skip; while (skip > 0) { if (inBufferPtr >= inBufferLen) { getPacket(); } int available = inBufferLen - inBufferPtr; if (skip > available) { skip -= available; inBufferPtr = inBufferLen; } else { inBufferPtr += skip; skip = 0; } } return tmp; } /** * Consumes the rest of the server response, without parsing it. * <p/> * <b>Note:</b> Use only in extreme cases, packets will not be parsed and * could leave the connection in an inconsistent state. */ void skipToEnd() { try { // No more data to read. inBufferPtr = inBufferLen; // Now consume all data until we get the last buffer while (inBuffer[1] != 1) { inBuffer = socket.getNetPacket(inBuffer); } } catch (IOException ex) { // Ignore it. Probably no more packets. } } /** * Closes this response stream. The stream id is unlinked from the * underlying shared socket as well. */ void close() { isClosed = true; } /** * Creates a simple <code>InputStream</code> over the server response. * <p/> * This method can be used to obtain a stream which can be passed to * <code>InputStreamReader</code>s to assist in reading multi byte * character sets. * * @param len the number of bytes available in the server response * @return the <code>InputStream</code> built over the server response */ InputStream getInputStream(int len) { tdsInputStream.init(len); return tdsInputStream; } /** * Creates a simple <code>InputStream</code> over the server response. * <p/> * This method can be used to obtain a stream which can be passed to * <code>InputStreamReader</code>s to assist in reading multi byte * character sets. * * @return the <code>InputStream</code> built over the server response */ InputStream getTDS90InputStream() throws IOException { tds90InputStream.init(); return tds90InputStream; } /** * Read the next TDS packet from the network. * * @throws IOException if an I/O error occurs */ private void getPacket() throws IOException { while (inBufferPtr >= inBufferLen) { if (isClosed) { throw new IOException(Messages.get("error.io.inclosed")); } inBuffer = socket.getNetPacket(inBuffer); inBufferLen = socket.getPktLen(inBuffer); inBufferPtr = TdsCore.PKT_HDR_LEN; } } /** * Simple inner class implementing an <code>InputStream</code> over the * server response. */ private static class TdsInputStream extends InputStream { /** The underlying <code>ResponseStream</code>. */ private TdsStream tds; /** The maximum amount of data to make available. */ private int maxLen; /** * Creates a <code>TdsInputStream</code> instance. * * @param tds the underlying <code>ResponseStream</code> */ public TdsInputStream(TdsStream tds) { this.tds = tds; } /** * initialize the stream and set the length. * @param len the length of byte data available. */ public void init(int len) { this.maxLen = len; } public int read() throws IOException { return (maxLen-- > 0)? tds.read(): -1; } public int read(byte[] b) throws IOException { return read(b, 0, b.length); } public int read(byte[] b, int off, int len) throws IOException { if (maxLen < 1) { return -1; } len = (len > this.maxLen)? this.maxLen: len; this.maxLen -= len; return tds.read(b, off, len); } public int available() throws IOException { return maxLen; } public void close() throws IOException { // Empty IO Stream while (this.maxLen-- > 0) { tds.read(); } } } /** * Simple inner class implementing an <code>InputStream</code> over the * server response. * <p/>Used to read SQL 2005 streamed data. */ private static class Tds90InputStream extends InputStream { /** The underlying <code>ResponseStream</code>. */ private TdsStream tds; /** End of file flag. */ private boolean eof; /** The TDS fragement size or 0 for EOF. */ private int fragSize; /** * Creates a <code>Tds90InputStream</code> instance. * * @param tds the underlying <code>ResponseStream</code> */ public Tds90InputStream(TdsStream tds) { this.tds = tds; } /** * Initialise the stream. * * @throws IOException */ public void init() throws IOException { this.fragSize = tds.readInt(); this.eof = this.fragSize < 1; } public int read() throws IOException { while (!this.eof) { if (this.fragSize-- > 0) { return tds.read(); } this.fragSize = tds.readInt(); this.eof = this.fragSize < 1; } return -1; } public int read(byte[] b) throws IOException { return read(b, 0, b.length); } public int read(byte[] b, int off, int len) throws IOException { if (this.eof) { return -1; } int saveLen = len; while (len > 0 && !this.eof) { int bc = (len > this.fragSize)? this.fragSize: len; tds.read(b, off, bc); this.fragSize -= bc; if (this.fragSize < 1) { this.fragSize = tds.readInt(); this.eof = this.fragSize < 1; } len -= bc; off += bc; } return saveLen - len; } public void close() throws IOException { // Empty IO Stream while (!this.eof) { read(); } } } }

The table below shows all metrics for TdsStream.java.

MetricValueDescription
BLOCKS123.00Number of blocks
BLOCK_COMMENT 0.00Number of block comment lines
COMMENTS357.00Comment lines
COMMENT_DENSITY 0.75Comment density
COMPARISONS89.00Number of comparison operators
CYCLOMATIC143.00Cyclomatic complexity
DECL_COMMENTS89.00Comments in declarations
DOC_COMMENT311.00Number of javadoc comment lines
ELOC473.00Effective lines of code
EXEC_COMMENTS17.00Comments in executable code
EXITS50.00Procedure exits
FUNCTIONS52.00Number of function declarations
HALSTEAD_DIFFICULTY136.10Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY226.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034 0.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 1.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA006713.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA007676.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 7.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 0.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 1.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 3.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 1.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 2.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 0.00JAVA0144 Line exceeds maximum M characters
JAVA0145 0.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 4.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 1.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1052.00Number of lines in the source file
LINE_COMMENT46.00Number of line comments
LOC594.00Lines of code
LOGICAL_LINES351.00Number of statements
LOOPS23.00Number of loops
NEST_DEPTH 5.00Maximum nesting depth
OPERANDS1361.00Number of operands
OPERATORS2997.00Number of operators
PARAMS50.00Number of formal parameter declarations
PROGRAM_LENGTH4358.00Halstead program length
PROGRAM_VOCAB402.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS176.00Number of return points from functions
SIZE33727.00Size of the file in bytes
UNIQUE_OPERANDS335.00Number of unique operands
UNIQUE_OPERATORS67.00Number of unique operators
WHITESPACE101.00Number of whitespace lines