Class XdrTcpDecodingStream

java.lang.Object
org.acplt.oncrpc.XdrDecodingStream
org.acplt.oncrpc.XdrTcpDecodingStream

public class XdrTcpDecodingStream extends XdrDecodingStream
The XdrTcpDecodingStream class provides the necessary functionality to XdrDecodingStream to receive XDR records from the network using the stream-oriented TCP/IP.
Version:
$Revision: 1.2 $ $Date: 2005/11/11 21:07:27 $ $State: Exp $ $Locker: $
Author:
Harald Albrecht
  • Field Details

    • socket

      private Socket socket
      The streaming socket to be used when receiving this XDR stream's buffer contents.
    • stream

      InputStream stream
      The input stream used to pull the bytes off the network.
    • buffer

      private byte[] buffer
      The buffer which will be filled from the datagram socket and then be used to supply the information when decoding data.
    • bufferIndex

      private int bufferIndex
      The read pointer is an index into the buffer.
    • bufferHighmark

      private int bufferHighmark
      Index of the last four byte word in the buffer, which has been read in from the datagram socket.
    • fragmentLength

      private int fragmentLength
      Remaining number of bytes in this fragment -- and still to read.
    • lastFragment

      private boolean lastFragment
      Flag indicating that we've read the last fragment and thus reached the end of the record.
  • Constructor Details

    • XdrTcpDecodingStream

      public XdrTcpDecodingStream(Socket streamingSocket, int bufferSize) throws IOException
      Construct a new XdrTcpDecodingStream object and associate it with the given streamingSocket for TCP/IP-based communication.
      Parameters:
      streamingSocket - Socket from which XDR data is received.
      bufferSize - Size of packet buffer for storing received XDR data.
      Throws:
      IOException
  • Method Details

    • getSenderAddress

      public InetAddress getSenderAddress()
      Returns the Internet address of the sender of the current XDR data. This method should only be called after beginDecoding(), otherwise it might return stale information.
      Specified by:
      getSenderAddress in class XdrDecodingStream
      Returns:
      InetAddress of the sender of the current XDR data.
    • getSenderPort

      public int getSenderPort()
      Returns the port number of the sender of the current XDR data. This method should only be called after beginDecoding(), otherwise it might return stale information.
      Specified by:
      getSenderPort in class XdrDecodingStream
      Returns:
      Port number of the sender of the current XDR data.
    • beginDecoding

      public void beginDecoding() throws OncRpcException, IOException
      Initiates decoding of the next XDR record. For TCP-based XDR decoding streams this reads in the next chunk of data from the network socket (a chunk of data is not necessary the same as a fragment, just enough to fill the internal buffer or receive the remaining part of a fragment).

      Read in the next bunch of bytes. This can be either a complete fragment, or if the fragments sent by the communication partner are too large for our buffer, only parts of fragments. In every case, this method ensures that there will be more data available in the buffer (or else an exception thrown).

      Specified by:
      beginDecoding in class XdrDecodingStream
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • readBuffer

      private void readBuffer(InputStream stream, byte[] bytes, int bytesToRead) throws IOException, OncRpcException
      Read into buffer exactly the amound of bytes specified.
      Parameters:
      stream - Input stream to read byte data from.
      bytes - buffer receiving data.
      bytesToRead - number of bytes to read into buffer.
      Throws:
      OncRpcException - if EOF is reached before all bytes could be read.
      IOException - if an I/O error occurs.
    • fill

      private void fill() throws OncRpcException, IOException
      Fills the internal buffer with the next chunk of data. The chunk is either as long as the buffer or as long as the remaining part of the current XDR fragment, whichever is smaller.

      This method does not accept empty XDR record fragments with the only exception of a final trailing empty fragment. This special case is accepted as some ONC/RPC implementations emit such trailing empty fragments whenever the encoded data is a full multiple of their internal record buffer size.

      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • endDecoding

      public void endDecoding() throws OncRpcException, IOException
      End decoding of the current XDR record. The general contract of endDecoding is that calling it is an indication that the current record is no more interesting to the caller and any allocated data for this record can be freed.

      This method overrides XdrDecodingStream.endDecoding(). It reads in and throws away fragments until it reaches the last fragment.

      Overrides:
      endDecoding in class XdrDecodingStream
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • close

      public void close() throws OncRpcException, IOException
      Closes this decoding XDR stream and releases any system resources associated with this stream. A closed XDR stream cannot perform decoding operations and cannot be reopened.

      This implementation frees the allocated buffer but does not close the associated datagram socket. It only throws away the reference to this socket.

      Overrides:
      close in class XdrDecodingStream
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • xdrDecodeInt

      public int xdrDecodeInt() throws OncRpcException, IOException
      Decodes (aka "deserializes") a "XDR int" value received from a XDR stream. A XDR int is 32 bits wide -- the same width Java's "int" data type has.
      Specified by:
      xdrDecodeInt in class XdrDecodingStream
      Returns:
      The decoded int value.
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • xdrDecodeOpaque

      public byte[] xdrDecodeOpaque(int length) throws OncRpcException, IOException
      Decodes (aka "deserializes") an opaque value, which is nothing more than a series of octets (or 8 bits wide bytes). Because the length of the opaque value is given, we don't need to retrieve it from the XDR stream. This is different from xdrDecodeOpaque(byte[], int, int) where first the length of the opaque value is retrieved from the XDR stream.
      Specified by:
      xdrDecodeOpaque in class XdrDecodingStream
      Parameters:
      length - Length of opaque data to decode.
      Returns:
      Opaque data as a byte vector.
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.
    • xdrDecodeOpaque

      public void xdrDecodeOpaque(byte[] opaque, int offset, int length) throws OncRpcException, IOException
      Decodes (aka "deserializes") a XDR opaque value, which is represented by a vector of byte values, and starts at offset with a length of length. Only the opaque value is decoded, so the caller has to know how long the opaque value will be. The decoded data is always padded to be a multiple of four (because that's what the sender does).
      Specified by:
      xdrDecodeOpaque in class XdrDecodingStream
      Parameters:
      opaque - Byte vector which will receive the decoded opaque value.
      offset - Start offset in the byte vector.
      length - the number of bytes to decode.
      Throws:
      OncRpcException - if an ONC/RPC error occurs.
      IOException - if an I/O error occurs.