net.sourceforge.jtds.jdbc
Class ResponseStream

java.lang.Object
  |
  +--net.sourceforge.jtds.jdbc.ResponseStream

public class ResponseStream
extends java.lang.Object

Class to implement an input stream for the server response.

Implementation note:

  1. This class contains methods to read different types of data from the server response stream in TDS format.
  2. Character translation of String items is carried out.

Version:
$Id: ResponseStream.java,v 1.16 2005/04/04 20:36:56 alin_sinpalean Exp $
Author:
Mike Hutchinson.

Field Summary
private  byte[] buffer
          The Input packet buffer.
private  int bufferLen
          The length of current input packet.
private  int bufferPtr
          The offset of the next byte to read.
private  byte[] byteBuffer
          A shared byte buffer.
private  char[] charBuffer
          A shared char buffer.
private  boolean isClosed
          True if stream is closed.
private  SharedSocket socket
          The shared network socket.
private  int streamId
          The unique stream id.
 
Constructor Summary
(package private) ResponseStream(SharedSocket socket, int streamId, int bufferSize)
          Construct a RequestStream object.
 
Method Summary
(package private)  void close()
          Close this response stream.
private  void getPacket()
          Read the next TDS packet from the network.
(package private)  int getServerType()
          Retrieve the Server type.
(package private)  int getStreamId()
          Retrieve the unique stream id.
(package private)  int getTdsVersion()
          Retrieve the TDS version number.
(package private)  int peek()
          Retrieve the next input byte without reading forward.
(package private)  int read()
          Retrieve the next input byte from the server response stream.
(package private)  int read(byte[] b)
          Retrieve a byte array from the server response stream.
(package private)  int read(byte[] b, int off, int len)
          Retrieve a byte array from the server response stream, specifying a start offset and length.
(package private)  int read(char[] c)
          Retrieve a char array from the server response stream.
(package private)  int readInt()
          Retrieve an int value from the server response stream.
(package private)  long readLong()
          Retrieve a long value from the server response stream.
(package private)  java.lang.String readNonUnicodeString(int len)
          Retrieve a String object from the server response stream, creating the string from a translated byte array.
(package private)  java.lang.String readNonUnicodeString(int len, CharsetInfo charsetInfo)
          Retrieve a String object from the server response stream, translating it from a byte array using the specified character set.
(package private)  short readShort()
          Retrieve a short value from the server response stream.
(package private)  java.lang.String readString(int len)
          Retrieve a String object from the server response stream.
(package private)  java.lang.String readString(int len, CharsetInfo info)
          Retrieve a String object from the server response stream, creating the string from a translated byte array.
(package private)  java.lang.String readUnicodeString(int len)
          Retrieve a UCS2-LE (Unicode) encoded String object from the server response stream.
(package private)  int skip(int skip)
          Discard bytes from the server response stream.
(package private)  void skipString(int len)
          Skip a string from the server response stream.
(package private)  void skipToEnd()
          Consumes the rest of the server response, without parsing it.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

socket

private SharedSocket socket
The shared network socket.

buffer

private byte[] buffer
The Input packet buffer.

bufferPtr

private int bufferPtr
The offset of the next byte to read.

bufferLen

private int bufferLen
The length of current input packet.

streamId

private int streamId
The unique stream id.

isClosed

private boolean isClosed
True if stream is closed.

byteBuffer

private byte[] byteBuffer
A shared byte buffer.

charBuffer

private char[] charBuffer
A shared char buffer.
Constructor Detail

ResponseStream

ResponseStream(SharedSocket socket,
               int streamId,
               int bufferSize)
Construct a RequestStream object.
Parameters:
socket - The shared socket object to write to.
streamId - The unique id for this stream (from ResponseStream).
Method Detail

getStreamId

int getStreamId()
Retrieve the unique stream id.
Returns:
the unique stream id as an int.

peek

int peek()
   throws java.io.IOException
Retrieve the next input byte without reading forward.
Returns:
The next byte in the input stream as an int.
Throws:
java.io.IOException -  

read

int read()
   throws java.io.IOException
Retrieve the next input byte from the server response stream.
Returns:
The next byte in the input stream as an int.
Throws:
java.io.IOException -  

read

int read(byte[] b)
   throws java.io.IOException
Retrieve a byte array from the server response stream.
Parameters:
b - The byte array.
Returns:
The number of bytes read as an int.
Throws:
java.io.IOException -  

read

int read(byte[] b,
         int off,
         int len)
   throws java.io.IOException
Retrieve a byte array from the server response stream, specifying a start offset and length.
Parameters:
b - The byte array.
off - The starting offset in the array.
len - The number of bytes to read.
Returns:
The number of bytes read as an int.
Throws:
java.io.IOException -  

read

int read(char[] c)
   throws java.io.IOException
Retrieve a char array from the server response stream.
Parameters:
c - The char array.
Returns:
The byte array as a byte[].
Throws:
java.io.IOException -  

readString

java.lang.String readString(int len)
                      throws java.io.IOException
Retrieve a String object from the server response stream. If the TDS protocol is 4.2 or 5.0 decode the string using the default server charset, otherwise use UCS2-LE (Unicode).
Parameters:
len - the length of the string to read in bytes in the case of TDS 4.2/5.0 and in characters for TDS 7.0+ (UCS2-LE encoded strings)
Returns:
the result as a String
Throws:
java.io.IOException - if an I/O error occurs

skipString

void skipString(int len)
          throws java.io.IOException
Skip a string from the server response stream. If the TDS protocol is 4.2 or 5.0 len is the length in bytes, otherwise it's the length in UCS2-LE characters (length in bytes == 2 * len).
Parameters:
len - the length of the string to skip in bytes in the case of TDS 4.2/5.0 and in characters for TDS 7.0+ (UCS2-LE encoded strings)
Throws:
java.io.IOException - if an I/O error occurs

readUnicodeString

java.lang.String readUnicodeString(int len)
                             throws java.io.IOException
Retrieve a UCS2-LE (Unicode) encoded String object from the server response stream.
Parameters:
len - the length of the string to read in characters
Returns:
the result as a String
Throws:
java.io.IOException - if an I/O error occurs

readNonUnicodeString

java.lang.String readNonUnicodeString(int len)
                                throws java.io.IOException
Retrieve a String object from the server response stream, creating the string from a translated byte array.
Parameters:
len - the length of the string to read in bytes
Returns:
the result as a String
Throws:
java.io.IOException - if an I/O error occurs

readNonUnicodeString

java.lang.String readNonUnicodeString(int len,
                                      CharsetInfo charsetInfo)
                                throws java.io.IOException
Retrieve a String object from the server response stream, translating it from a byte array using the specified character set.
Parameters:
len - the length of the string to read in bytes
Returns:
the result as a String
Throws:
java.io.IOException - if an I/O error occurs

readString

java.lang.String readString(int len,
                            CharsetInfo info)
                      throws java.io.IOException
Retrieve a String object from the server response stream, creating the string from a translated byte array.
Parameters:
len - the length of the string to read in bytes
info - descriptor of the charset to use
Returns:
the result as a String
Throws:
java.io.IOException - if an I/O error occurs

readShort

short readShort()
          throws java.io.IOException
Retrieve a short value from the server response stream.
Returns:
The result as a short.
Throws:
java.io.IOException -  

readInt

int readInt()
      throws java.io.IOException
Retrieve an int value from the server response stream.
Returns:
The result as a int.
Throws:
java.io.IOException -  

readLong

long readLong()
        throws java.io.IOException
Retrieve a long value from the server response stream.
Returns:
The result as a long.
Throws:
java.io.IOException -  

skip

int skip(int skip)
   throws java.io.IOException
Discard bytes from the server response stream.
Parameters:
skip - The number of bytes to discard.
Returns:
The skip parameter as an int.

skipToEnd

void skipToEnd()
Consumes the rest of the server response, without parsing it.

Note: Use only in extreme cases, packets will not be parsed and could leave the connection in an inconsistent state.


close

void close()
Close this response stream. The stream id is unlinked from the underlying shared socket as well.

getTdsVersion

int getTdsVersion()
Retrieve the TDS version number.
Returns:
The TDS version as an int.

getServerType

int getServerType()
Retrieve the Server type.
Returns:
The Server type as an int.

getPacket

private void getPacket()
                throws java.io.IOException
Read the next TDS packet from the network.
Throws:
java.io.IOException -  


Generated on April 15 2005