Class MainframeInput


  • public class MainframeInput
    extends Object
    Reads mainframe data.

    Method fillBuffer reads an amount of bytes into an internal buffer. The various read methods retrieve data from the buffer. The position advances automatically.

    Author:
    Uwe Finke
    • Constructor Detail

      • MainframeInput

        public MainframeInput​(InputStream stream,
                              Charset charset)
                       throws UnsupportedEncodingException
        Constructor.

        The charset may be either a single byte or a double byte character set. Do not use a character set with a variant number of bytes for a single character (such as UTF-8)!

        Parameters:
        stream - input stream
        charset - character set
        Throws:
        UnsupportedEncodingException - when the character set is not supported in the runtime environment
      • MainframeInput

        public MainframeInput​(InputStream stream,
                              String charset)
                       throws UnsupportedEncodingException
        Constructor.

        The charset may be either a single byte or a double byte character set. Do not use a character set with a variant number of bytes for a single character (such as UTF-8)!

        Parameters:
        stream - input stream
        charset - name of character set
        Throws:
        UnsupportedEncodingException - when the character set does not exist or ist not supported in the runtime environment
    • Method Detail

      • close

        public void close()
                   throws IOException
        Closes the underlaying stream.
        Throws:
        IOException - when the stream could not be closed
      • fillBuffer

        public boolean fillBuffer​(int byteCount)
                           throws IOException
        Fills the internal buffer.

        Returns true if the given number of bytes could be read, or false when there where no more bytes to read. Throws EOFException when the stream ends before all requested bytes were read.

        For fixed length records, byteCount should be the record length. For variable records, the record length must be retrieved by a separate call before the rest of the record can be read.

        The internal buffer position is set to 0.

        Parameters:
        byteCount - number of bytes to pass into the buffer
        Returns:
        EOF flag
        Throws:
        IOException - when the number of requested bytes could not be read from the input stream
      • nextRecord

        public boolean nextRecord​(int byteCount)
                           throws IOException
        Fills the internal buffer and increments record count. Calls fillBuffer.
        Parameters:
        byteCount - number of bytes to pass into the buffer
        Returns:
        EOF flag
        Throws:
        IOException - when the number of requested bytes could not be read from the input stream
      • getRecordCount

        public int getRecordCount()
        Returns the record count. The record count is incremented by nextRecord.
        Returns:
        record count
      • setPosition

        public void setPosition​(int offset)
        Sets the internal buffer's position.
        Parameters:
        offset - position within the buffer, fist byte position is 0
      • getPosition

        public int getPosition()
        Retrieves the internal buffer's position.
        Returns:
        current position
      • getSize

        public int getSize()
        Retrieves the record size. This is the maximum buffer position.
        Returns:
        record size
      • readUnsignedByte

        public int readUnsignedByte()
                             throws IOException
        Reads a raw byte.
        Returns:
        value
        Throws:
        IOException - when the byte could not be read from the buffer
      • readByte

        public byte readByte()
                      throws IOException
        Reads a binary signed byte value.
        Returns:
        value
        Throws:
        IOException - when the byte could not be read from the buffer
      • readShort

        public short readShort()
                        throws IOException
        Reads a binary short value.
        Returns:
        value
        Throws:
        IOException - when the short could not be read from the buffer
      • readInt

        public int readInt()
                    throws IOException
        Reads a binary int value.
        Returns:
        value
        Throws:
        IOException - when the int could not be read from the buffer
      • readLong

        public long readLong()
                      throws IOException
        Reads a binary long value.
        Returns:
        value
        Throws:
        IOException - when the long could not be read from the buffer
      • readString

        public String readString​(int charCount)
                          throws IOException
        Reads a string.
        Parameters:
        charCount - number of characters
        Returns:
        value
        Throws:
        IOException - when the requested number of characters could not be read from the buffer
      • readZonedInt

        public int readZonedInt​(int digitCount)
                         throws IOException
        Reads a zoned int value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readZonedLong

        public long readZonedLong​(int digitCount)
                           throws IOException
        Reads a zoned long value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readZonedDouble

        public double readZonedDouble​(int integerDigitCount,
                                      int fractionalDigitCount)
                               throws IOException
        Reads a zoned double value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (sum of integer an fractional part) could not be read from the buffer
      • readZonedBigDecimal

        public BigDecimal readZonedBigDecimal​(int integerDigitCount,
                                              int fractionalDigitCount)
                                       throws IOException
        Reads a zoned BigDecimal value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (sum of integer an fractional part) could not be read from the buffer
      • readPackedInt

        public int readPackedInt​(int digitCount)
                          throws IOException
        Reads a packed int value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readPackedLong

        public long readPackedLong​(int digitCount)
                            throws IOException
        Reads a packed long value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readPackedDouble

        public double readPackedDouble​(int integerDigitCount,
                                       int fractionalDigitCount)
                                throws IOException
        Reads a packed double value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (integer plus fractional part) could not be read from the buffer
      • readPackedBigDecimal

        public BigDecimal readPackedBigDecimal​(int integerDigitCount,
                                               int fractionalDigitCount)
                                        throws IOException
        Reads a packed BigDecimal value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (integer plus fractional part) could not be read from the buffer
      • readUnsignedPackedInt

        public int readUnsignedPackedInt​(int digitCount)
                                  throws IOException
        Reads an unsigned packed int value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readUnsignedPackedLong

        public long readUnsignedPackedLong​(int digitCount)
                                    throws IOException
        Reads an unsigned packed long value.
        Parameters:
        digitCount - number of digits
        Returns:
        value
        Throws:
        IOException - when the requested number of digits could not be read from the buffer
      • readUnsignedPackedDouble

        public double readUnsignedPackedDouble​(int integerDigitCount,
                                               int fractionalDigitCount)
                                        throws IOException
        Reads an unsigned packed double value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (integer plus fractional part) could not be read from the buffer
      • readUnsignedPackedBigDecimal

        public BigDecimal readUnsignedPackedBigDecimal​(int integerDigitCount,
                                                       int fractionalDigitCount)
                                                throws IOException
        Reads an unsigned packed BigDecimal value.
        Parameters:
        integerDigitCount - number of digits before imaginary decimal point
        fractionalDigitCount - number of digits after imaginary decimal point
        Returns:
        value
        Throws:
        IOException - when the requested number of digits (integer plus fractional part) could not be read from the buffer
      • skipBytes

        public int skipBytes​(int n)
                      throws IOException
        Advances the buffers position.
        Parameters:
        n - number of bytes to skip
        Returns:
        number of skipped bytes
        Throws:
        IOException - when the requested number of bytes could not be skipped
      • readBytes

        public byte[] readBytes​(int count)
                         throws IOException
        Reads a raw byte array.
        Parameters:
        count - number of bytes to read
        Returns:
        byte array
        Throws:
        IOException - when the requested number of bytes could not be read from the buffer
      • readFully

        public void readFully​(byte[] b)
                       throws IOException
        Fills a byte array.
        Parameters:
        b - array
        Throws:
        IOException - when the array could not be filled from the buffer
      • readFully

        public void readFully​(byte[] b,
                              int off,
                              int len)
                       throws IOException
        Fills a portion of a byte array.
        Parameters:
        b - array
        off - offset in the array
        len - number of bytes
        Throws:
        IOException - when the requested number of bytes could not be read from the buffer
      • getBuffer

        public RandomAccessBuffer getBuffer()
        Gives access to the internal buffer. Use the buffer only if you know what you're doing.
        Returns:
        the buffer
      • setBuffer

        public void setBuffer​(RandomAccessBuffer buffer)
        Replaces the internal buffer. May be useful to share the buffer with other instances.
        Parameters:
        buffer - the new buffer instance