Class ResponseStream

java.lang.Object
net.sourceforge.jtds.jdbc.ResponseStream

public class ResponseStream extends Object
Implements 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.20 2005-10-27 13:22:33 alin_sinpalean Exp $
Author:
Mike Hutchinson.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Simple inner class implementing an InputStream over the server response.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The SharedSocket.VirtualSocket used by this stream.
    private byte[]
    The Input packet buffer.
    private int
    The length of current input packet.
    private int
    The offset of the next byte to read.
    private final byte[]
    A shared byte buffer.
    private final char[]
    A shared char buffer.
    private boolean
    True if stream is closed.
    private final SharedSocket
    The shared network socket.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ResponseStream(SharedSocket socket, SharedSocket.VirtualSocket vsock, int bufferSize)
    Constructs a RequestStream object.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Closes this response stream.
    (package private) InputStream
    getInputStream(int len)
    Creates a simple InputStream over the server response.
    private void
    Read the next TDS packet from the network.
    (package private) int
    Retrieves the server type.
    (package private) int
    Retrieves the TDS version number.
    (package private) SharedSocket.VirtualSocket
    Retrieves the SharedSocket.VirtualSocket used by this stream id.
    (package private) int
    Retrieves the next input byte without reading forward.
    (package private) int
    Reads the next input byte from the server response stream.
    (package private) int
    read(byte[] b)
    Reads a byte array from the server response stream.
    (package private) int
    read(byte[] b, int off, int len)
    Reads a byte array from the server response stream, specifying a start offset and length.
    (package private) int
    read(char[] c)
    Reads a char array from the server response stream.
    (package private) int
    Reads an int value from the server response stream.
    (package private) long
    Reads a long value from the server response stream.
    (package private) String
    Reads a non Unicode String from the server response stream, creating the String from a translated byte array.
    (package private) String
    readNonUnicodeString(int len, CharsetInfo charsetInfo)
    Reads a String from the server response stream, translating it from a byte array using the specified character set.
    (package private) short
    Reads a short value from the server response stream.
    (package private) String
    readString(int len)
    Reads a String object from the server response stream.
    (package private) String
    readString(int len, CharsetInfo info)
    Reads a String from the server response stream, creating it from a translated byte array.
    (package private) String
    Reads a UCS2-LE (Unicode) encoded String object from the server response stream.
    (package private) BigDecimal
    Reads an unsigned long value from the server response stream.
    (package private) int
    skip(int skip)
    Discards bytes from the server response stream.
    (package private) void
    skipString(int len)
    Skips a String from the server response stream.
    (package private) void
    Consumes the rest of the server response, without parsing it.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • socket

      private final 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.
    • _VirtualSocket

      private final SharedSocket.VirtualSocket _VirtualSocket
      The SharedSocket.VirtualSocket used by this stream.
    • isClosed

      private boolean isClosed
      True if stream is closed.
    • byteBuffer

      private final byte[] byteBuffer
      A shared byte buffer.
    • charBuffer

      private final char[] charBuffer
      A shared char buffer.
  • Constructor Details

  • Method Details

    • getVirtualSocket

      SharedSocket.VirtualSocket getVirtualSocket()
      Retrieves the SharedSocket.VirtualSocket used by this stream id.
      Returns:
      the unique stream id as an int
    • peek

      int peek() throws IOException
      Retrieves the next input byte without reading forward.
      Returns:
      the next byte in the input stream as an int
      Throws:
      IOException - if an I/O error occurs
    • read

      int read() throws IOException
      Reads the next input byte from the server response stream.
      Returns:
      the next byte in the input stream as an int
      Throws:
      IOException - if an I/O error occurs
    • read

      int read(byte[] b) throws IOException
      Reads a byte array from the server response stream.
      Parameters:
      b - the byte array to read into
      Returns:
      the number of bytes read as an int
      Throws:
      IOException - if an I/O error occurs
    • read

      int read(byte[] b, int off, int len) throws IOException
      Reads 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:
      IOException - if an I/O error occurs
    • read

      int read(char[] c) throws IOException
      Reads a char array from the server response stream.
      Parameters:
      c - the char array
      Returns:
      the byte array as a byte[]
      Throws:
      IOException - if an I/O error occurs
    • readString

      String readString(int len) throws IOException
      Reads a String object from the server response stream. If the TDS protocol version is 4.2 or 5.0 decode the string use 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:
      IOException - if an I/O error occurs
    • skipString

      void skipString(int len) throws IOException
      Skips a String from the server response stream. If the TDS protocol version 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:
      IOException - if an I/O error occurs
    • readUnicodeString

      String readUnicodeString(int len) throws IOException
      Reads 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:
      IOException - if an I/O error occurs
    • readNonUnicodeString

      String readNonUnicodeString(int len) throws IOException
      Reads a non Unicode String 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:
      IOException - if an I/O error occurs
    • readNonUnicodeString

      String readNonUnicodeString(int len, CharsetInfo charsetInfo) throws IOException
      Reads a String 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:
      IOException - if an I/O error occurs
    • readString

      String readString(int len, CharsetInfo info) throws IOException
      Reads a String from the server response stream, creating it 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:
      IOException - if an I/O error occurs
    • readShort

      short readShort() throws IOException
      Reads a short value from the server response stream.
      Returns:
      the result as a short
      Throws:
      IOException - if an I/O error occurs
    • readInt

      int readInt() throws IOException
      Reads an int value from the server response stream.
      Returns:
      the result as a int
      Throws:
      IOException - if an I/O error occurs
    • readLong

      long readLong() throws IOException
      Reads a long value from the server response stream.
      Returns:
      the result as a long
      Throws:
      IOException - if an I/O error occurs
    • readUnsignedLong

      BigDecimal readUnsignedLong() throws IOException
      Reads an unsigned long value from the server response stream.
      Returns:
      the result as a BigDecimal
      Throws:
      IOException - if an I/O error occurs
    • skip

      int skip(int skip) throws IOException
      Discards bytes from the server response stream.
      Parameters:
      skip - the number of bytes to discard
      Returns:
      the number of bytes skipped
      Throws:
      IOException
    • 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()
      Closes this response stream. The stream id is unlinked from the underlying shared socket as well.
    • getTdsVersion

      int getTdsVersion()
      Retrieves the TDS version number.
      Returns:
      the TDS version as an int
    • getServerType

      int getServerType()
      Retrieves the server type.
      Returns:
      the server type as an int
    • getInputStream

      InputStream getInputStream(int len)
      Creates a simple InputStream over the server response.

      This method can be used to obtain a stream which can be passed to InputStreamReaders to assist in reading multi byte character sets.

      Parameters:
      len - the number of bytes available in the server response
      Returns:
      the InputStream built over the server response
    • getPacket

      private void getPacket() throws IOException
      Read the next TDS packet from the network.
      Throws:
      IOException - if an I/O error occurs