Class DICOM_Comms

  • Direct Known Subclasses:
    DICOM_CommsLink, DICOM_FileIO, DICOM_PDUService

    public class DICOM_Comms
    extends java.lang.Object
    This class is used as a base class for all DICOM file and network I/O.
    This DICOM communication package was originally based on the Java Dicom Package, whose license is below:
     Java Dicom Package (com.zmed.dicom)
     
      Copyright (c) 1996-1997 Z Medical Imaging Systems, Inc.
     
      This software is provided, as is, for non-commercial educational
      purposes only.   Use or incorporation of this software or derivative
      works in commercial applications requires written consent from
      Z Medical Imaging Systems, Inc.
     
      Z MEDICAL IMAGING SYSTEMS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
      THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
      FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, OR CONFORMANCE TO ANY
      SPECIFICATION OR STANDARD.  Z MEDICAL IMAGING SYSTEMS SHALL NOT BE
      LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR
      MODIFYING THIS SOFTWARE OR ITS DERIVATIVES.
     
      =============================================================================
     
      This software package is implemented similarly to the UC Davis public
      domain C++ DICOM implementation which contains the following copyright
      notice:
     
      Copyright (C) 1995, University of California, Davis
     
      THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND THE UNIVERSITY
      OF CALIFORNIA DOES NOT MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
      PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
      USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
      SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
      THE SOFTWARE IS WITH THE USER.
     
      Copyright of the software and supporting documentation is
      owned by the University of California, and free access
      is hereby granted as a license to use this software, copy this
      software and prepare derivative works based upon this software.
      However, any distribution of this software source code or
      supporting documentation or derivative works (source code and
      supporting documentation) must include this copyright notice.
     
      The UC Davis C++ source code is publicly available from the following
      anonymous ftp site:
     
      ftp://imrad.ucdmc.ucdavis.edu/pub/dicom/UCDMC/
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  DICOM_Comms.ByteBuffer
      Simple inner class to facilitate the memory allocation of of a byte buffer used in DICOM_Comms to support I/O.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BIG_ENDIAN
      Flag used to indicate Big Endianess.
      protected int breakSize
      Defaults to the maximum buffer size in streaming.
      private byte[] byteArray1
      One byte array allocated once to speed process and reduced the need to reallocate memory.
      private byte[] byteArray2
      Two byte array allocated once to speed process and reduced the need to reallocate memory.
      private byte[] byteArray4
      Four byte array allocated once to speed process and reduced the need to reallocate memory.
      private boolean debugFlag
      Debug flag.
      private boolean havePeeked
      Flag indicating whether the first byte has been looked at.
      protected int inBuffersLength
      Length of the total input data (Not buffer size but total length of data ).
      protected java.util.Vector<DICOM_Comms.ByteBuffer> incomingBuffers
      Vector of ByteBuffer's of incoming data.
      private int inEndianess
      Endianess of the input stream.
      static int LITTLE_ENDIAN
      Flag used to indicate Little Endianess.
      static int MAX_READ_LENGTH
      The maximum read buffer size.
      protected int outBuffersLength
      Length of the total output data (Not buffer size but total length of data ).
      private int outEndianess
      Endianess of the output stream.
      protected java.util.Vector<DICOM_Comms.ByteBuffer> outgoingBuffers
      Vector of ByteBuffer's of outgoing data.
      private byte peekedByte
      Storage for the first byte (i.e. the PDU type )
      private byte[] srcByteArray
      Source byte array allocated ONCE used in reading from the stream.
    • Constructor Summary

      Constructors 
      Constructor Description
      DICOM_Comms()
      Constructor for DICOM network I/O class.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int bufferToInt16​(byte[] buffer, int index, int endianess)
      Copies a 2 byte array into a 16 bit integer of proper endianess.
      static int bufferToInt32​(byte[] buffer, int index, int endianess)
      Copies a 4 byte array into a 32 bit integer of proper endianess.
      protected void finalize()
      Prepares this class for destruction.
      void flush()
      Sends all byte buffers of the outgoing buffer vector out the port. see DICOM PDUService.sendBinary and DICOMSocket
      void flush​(int nBytes)
      Sends specified number of bytes out the port. see DICOM PDUService.sendBinary and DICOMSocket
      int getIncomingEndian()
      Returns constant indicating endianess of incoming data.
      int getIncomingSize()
      Returns the incoming data size.
      int getOutgoingEndian()
      Returns constant indicating endianess of outgoing data.
      int getOutgoingSize()
      Returns the outgoing data size.
      static void int16ToBuffer​(byte[] buffer, int index, int value, int endianess)
      Copies a 16 bit (2 bytes) integer into the specified portion of the byte array of proper endianess.
      static void int32ToBuffer​(byte[] buffer, int index, int value, int endianess)
      Copies a 32 bit (4 bytes) integer into the specified portion of the byte array of proper endianess.
      byte peekFirstByte()
      Typically used to determine the PDUTYPE (i.e.
      int peekForEndOfSequence()
      DOCUMENT ME!
      void read​(byte[] data)
      Simple method that take a byte array and call read with two parameters: byte array and array length.
      void read​(byte[] data, int nBytes)
      Reads a specified number of bytes into the byte data buffer.
      int readBinary​(byte[] data, int nbytes)
      Dummy method and should be overloaded by DICOMPDUService.
      private void readBlock​(int useBreakSize)
      This is the method that actually reads in the data from the socket or IO stream.
      byte readByte()
      Reads a single byte from the data stream.
      byte[] readBytes​(int length)
      Allocates and.
      void readFill​(int nBytes)
      Reads in a nBytes and puts them in a ByteBuffer and puts the byte buffer in the vector of incomingBuffers.
      int readInt32()
      Reads a 4 byte integer (otherwise known as an int).
      int readShort16()
      Reads a 2 byte integer (otherwise known as a short).
      void seekToEndOfGroupTwoTags()
      Strips preamble and group 2 tags from ioBuffer if present.
      void sendBinary​(byte[] data, int startIndex, int nbytes)
      Dummy method and should be overloaded by DICOMPDUService.
      void setIncomingEndianess​(int endianess)
      Sets the endianess for the incoming data.
      void setOutgoingEndianess​(int endianess)
      Sets the endianess for the outgoing data.
      void write​(byte[] byteArray)
      Writes a byte arrary to the outgoing buffers.
      void write​(byte[] data, int dataOffset, int nBytes)
      Writes a specified number of bytes from the byte buffer into outGoing ByteBuffer list of byte buffers.
      void writeByte​(byte value)
      Writes a byte to the outgoing buffers.
      void writeByteIn​(byte value, DICOM_FileIO ioBuffer)
      Writes a byte to the incoming buffers.
      void writeIn​(byte[] data, int dataOffset, int nBytes, DICOM_FileIO ioBuffer)
      Writes a specified number of bytes from the byte buffer into incomming ByteBuffer list of byte buffers.
      void writeInt32​(int value)
      Writes a 32 (4 byte) integer to the outgoing buffers.
      void writeInt32In​(int value, DICOM_FileIO ioBuffer)
      Writes a 32 (4 byte) integer to the incoming buffers.
      void writeShort16​(int value)
      Writes a 16 (2 byte) integer to the outgoing buffers.
      void writeShort16In​(int value, DICOM_FileIO ioBuffer)
      Writes a 16 (2 byte) integer to the incomming buffers.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BIG_ENDIAN

        public static final int BIG_ENDIAN
        Flag used to indicate Big Endianess.
        See Also:
        Constant Field Values
      • LITTLE_ENDIAN

        public static final int LITTLE_ENDIAN
        Flag used to indicate Little Endianess.
        See Also:
        Constant Field Values
      • MAX_READ_LENGTH

        public static final int MAX_READ_LENGTH
        The maximum read buffer size.
        See Also:
        Constant Field Values
      • breakSize

        protected int breakSize
        Defaults to the maximum buffer size in streaming.
      • inBuffersLength

        protected int inBuffersLength
        Length of the total input data (Not buffer size but total length of data ).
      • incomingBuffers

        protected java.util.Vector<DICOM_Comms.ByteBuffer> incomingBuffers
        Vector of ByteBuffer's of incoming data.
      • outBuffersLength

        protected int outBuffersLength
        Length of the total output data (Not buffer size but total length of data ).
      • outgoingBuffers

        protected java.util.Vector<DICOM_Comms.ByteBuffer> outgoingBuffers
        Vector of ByteBuffer's of outgoing data.
      • byteArray1

        private byte[] byteArray1
        One byte array allocated once to speed process and reduced the need to reallocate memory.
      • byteArray2

        private byte[] byteArray2
        Two byte array allocated once to speed process and reduced the need to reallocate memory.
      • byteArray4

        private byte[] byteArray4
        Four byte array allocated once to speed process and reduced the need to reallocate memory.
      • debugFlag

        private boolean debugFlag
        Debug flag.
      • havePeeked

        private boolean havePeeked
        Flag indicating whether the first byte has been looked at.
      • inEndianess

        private int inEndianess
        Endianess of the input stream.
      • outEndianess

        private int outEndianess
        Endianess of the output stream.
      • peekedByte

        private byte peekedByte
        Storage for the first byte (i.e. the PDU type )
      • srcByteArray

        private byte[] srcByteArray
        Source byte array allocated ONCE used in reading from the stream.
    • Constructor Detail

      • DICOM_Comms

        public DICOM_Comms()
        Constructor for DICOM network I/O class. Sets byte ordering for read and write to big endian.
    • Method Detail

      • bufferToInt16

        public static final int bufferToInt16​(byte[] buffer,
                                              int index,
                                              int endianess)
        Copies a 2 byte array into a 16 bit integer of proper endianess.
        Parameters:
        buffer - source byte array
        index - index into buffer
        endianess - Byte ordering as defined above.
        Returns:
        16 bit integer value
      • bufferToInt32

        public static final int bufferToInt32​(byte[] buffer,
                                              int index,
                                              int endianess)
        Copies a 4 byte array into a 32 bit integer of proper endianess.
        Parameters:
        buffer - source byte array
        index - index into buffer
        endianess - endianess
        Returns:
        32 bit integer value
      • int16ToBuffer

        public static final void int16ToBuffer​(byte[] buffer,
                                               int index,
                                               int value,
                                               int endianess)
        Copies a 16 bit (2 bytes) integer into the specified portion of the byte array of proper endianess.
        Parameters:
        buffer - destination byte array
        index - index into buffer
        value - 16 bit integer value
        endianess - endianess
      • int32ToBuffer

        public static final void int32ToBuffer​(byte[] buffer,
                                               int index,
                                               int value,
                                               int endianess)
        Copies a 32 bit (4 bytes) integer into the specified portion of the byte array of proper endianess.
        Parameters:
        buffer - destination byte array
        index - index into buffer
        value - 16 bit integer value
        endianess - endianess
      • flush

        public void flush()
                   throws DICOM_Exception
        Sends all byte buffers of the outgoing buffer vector out the port. see DICOM PDUService.sendBinary and DICOMSocket
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • flush

        public void flush​(int nBytes)
                   throws DICOM_Exception
        Sends specified number of bytes out the port. see DICOM PDUService.sendBinary and DICOMSocket
        Parameters:
        nBytes - number of bytes to flush
        Throws:
        DICOM_Exception - Throws an error when attempting to flush more bytes than are present!
      • getIncomingEndian

        public int getIncomingEndian()
        Returns constant indicating endianess of incoming data.
        Returns:
        the endianess of the incoming data
      • getIncomingSize

        public int getIncomingSize()
        Returns the incoming data size.
        Returns:
        the number of bytes in the incoming data
      • getOutgoingEndian

        public int getOutgoingEndian()
        Returns constant indicating endianess of outgoing data.
        Returns:
        the endianess of the outgoing data
      • getOutgoingSize

        public int getOutgoingSize()
        Returns the outgoing data size.
        Returns:
        the number of bytes in the outgoing data
      • peekFirstByte

        public byte peekFirstByte()
                           throws DICOM_Exception
        Typically used to determine the PDUTYPE (i.e. PDUTYPE_PresentationContextAccept)
        Returns:
        the PDU type identifier
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • peekForEndOfSequence

        public int peekForEndOfSequence()
                                 throws DICOM_Exception
        DOCUMENT ME!
        Returns:
        DOCUMENT ME!
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • read

        public void read​(byte[] data)
                  throws DICOM_Exception
        Simple method that take a byte array and call read with two parameters: byte array and array length.
        Parameters:
        data - byte buffer in which to store the data
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • read

        public void read​(byte[] data,
                         int nBytes)
                  throws DICOM_Exception
        Reads a specified number of bytes into the byte data buffer.
        Parameters:
        data - byte data array ( destination )
        nBytes - the number of bytes to read into the buffer
        Throws:
        DICOM_Exception - Throws error if there is an error reading data from the port.
      • readBinary

        public int readBinary​(byte[] data,
                              int nbytes)
                       throws DICOM_Exception
        Dummy method and should be overloaded by DICOMPDUService.
        Parameters:
        data - Buffer to store the data.
        nbytes - Number of bytes to be read.
        Returns:
        The actual number of bytes read.
        Throws:
        DICOM_Exception - Throws error if method is not overwritten!
      • readByte

        public final byte readByte()
                            throws DICOM_Exception
        Reads a single byte from the data stream.
        Returns:
        a single byte of data
        Throws:
        DICOM_Exception - Throws error if there is an error reading data from the port.
      • readBytes

        public final byte[] readBytes​(int length)
                               throws DICOM_Exception
        Allocates and.
        Parameters:
        length - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • readFill

        public void readFill​(int nBytes)
                      throws DICOM_Exception
        Reads in a nBytes and puts them in a ByteBuffer and puts the byte buffer in the vector of incomingBuffers.
        Parameters:
        nBytes - number of bytes to read
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • readInt32

        public final int readInt32()
                            throws DICOM_Exception
        Reads a 4 byte integer (otherwise known as an int).
        Returns:
        an integer
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • readShort16

        public final int readShort16()
                              throws DICOM_Exception
        Reads a 2 byte integer (otherwise known as a short).
        Returns:
        an integer of the 2 byte data.
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • seekToEndOfGroupTwoTags

        public void seekToEndOfGroupTwoTags()
        Strips preamble and group 2 tags from ioBuffer if present.
      • sendBinary

        public void sendBinary​(byte[] data,
                               int startIndex,
                               int nbytes)
                        throws DICOM_Exception
        Dummy method and should be overloaded by DICOMPDUService.
        Parameters:
        data - Buffer of data send out the port (connection).
        startIndex - starting offset
        nbytes - Number of bytes to be sent.
        Throws:
        DICOM_Exception - DOCUMENT ME!
      • setIncomingEndianess

        public void setIncomingEndianess​(int endianess)
        Sets the endianess for the incoming data.
        Parameters:
        endianess - the byte ordering (endianess) of the incoming data
      • setOutgoingEndianess

        public void setOutgoingEndianess​(int endianess)
        Sets the endianess for the outgoing data.
        Parameters:
        endianess - the byte ordering (endianess) of the outgoing data
      • write

        public final void write​(byte[] byteArray)
        Writes a byte arrary to the outgoing buffers.
        Parameters:
        byteArray - the array of data.
      • write

        public void write​(byte[] data,
                          int dataOffset,
                          int nBytes)
        Writes a specified number of bytes from the byte buffer into outGoing ByteBuffer list of byte buffers.
        Parameters:
        data - byte buffer
        dataOffset - DOCUMENT ME!
        nBytes - number of bytes to write
      • writeIn

        public void writeIn​(byte[] data,
                            int dataOffset,
                            int nBytes,
                            DICOM_FileIO ioBuffer)
        Writes a specified number of bytes from the byte buffer into incomming ByteBuffer list of byte buffers.
        Parameters:
        data - byte buffer
        dataOffset - DOCUMENT ME!
        nBytes - number of bytes to write
        ioBuffer -
      • writeByte

        public final void writeByte​(byte value)
        Writes a byte to the outgoing buffers.
        Parameters:
        value - byte value
      • writeByteIn

        public final void writeByteIn​(byte value,
                                      DICOM_FileIO ioBuffer)
        Writes a byte to the incoming buffers.
        Parameters:
        value - byte value
        ioBuffer -
      • writeInt32

        public final void writeInt32​(int value)
        Writes a 32 (4 byte) integer to the outgoing buffers.
        Parameters:
        value - the value to be sent. The value is put into a 4 byte "byte" buffer in the order indicated by the specified endianess of this class.
      • writeInt32In

        public final void writeInt32In​(int value,
                                       DICOM_FileIO ioBuffer)
        Writes a 32 (4 byte) integer to the incoming buffers.
        Parameters:
        value - the value to be sent. The value is put into a 4 byte "byte" buffer in the order indicated by the specified endianess of this class.
        ioBuffer -
      • writeShort16

        public final void writeShort16​(int value)
        Writes a 16 (2 byte) integer to the outgoing buffers.
        Parameters:
        value - the value to be sent. The value is put into a 2 byte "byte" buffer in the order indicated by the specified endianess of this class.
      • writeShort16In

        public final void writeShort16In​(int value,
                                         DICOM_FileIO ioBuffer)
        Writes a 16 (2 byte) integer to the incomming buffers.
        Parameters:
        value - the value to be sent. The value is put into a 2 byte "byte" buffer in the order indicated by the specified endianess of this class.
        ioBuffer -
      • finalize

        protected void finalize()
        Prepares this class for destruction.
        Overrides:
        finalize in class java.lang.Object
      • readBlock

        private void readBlock​(int useBreakSize)
                        throws DICOM_Exception
        This is the method that actually reads in the data from the socket or IO stream.
        Parameters:
        useBreakSize - the recommended size of the read buffer
        Throws:
        DICOM_Exception - Throws error if there is an error reading data from the port.