Class BCDStringCodec<T>

  • All Implemented Interfaces:
    StringCodec<T>, Serializable
    Direct Known Subclasses:
    BCDPlacesStringCodec

    public class BCDStringCodec<T>
    extends Object
    implements StringCodec<T>
    Implements a StringCodec for the case of decimal and up to six more digits encoded in semi-octets. This StringCodec:
    • allocates the smallest sufficient number of octets that has the given factor;
    • has a filler for padding unused semi-octets;
    • can reverse the semi-octet ordering;
    • can leave one semi-octet from each octet unused;
    • and allows hex and lax variants to be obtained, for use when there are invalid digits.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected BCDStringCodec​(CharCodec charCodec, boolean packed, int filler, boolean swapped, int factor)  
        BCDStringCodec​(String digitMapping, boolean packed, int filler, boolean swapped, int factor)
      Constructs a 3GPP standards-based BCDStringCodec customised to the given digit mapping, packing, filler, semi-octet ordering and factor.
    • Field Detail

      • charCodec

        protected final CharCodec charCodec
    • Constructor Detail

      • BCDStringCodec

        public BCDStringCodec​(String digitMapping,
                              boolean packed,
                              int filler,
                              boolean swapped,
                              int factor)
        Constructs a 3GPP standards-based BCDStringCodec customised to the given digit mapping, packing, filler, semi-octet ordering and factor. The digitMapping can use the character ? for invalid digits.
        Parameters:
        digitMapping - String of 16 address characters, where index is value for semi-octet
        packed - whether to fill every semi-octet, or only one per octet
        filler - semi-octet value for padding an odd-length address
        swapped - whether to save address characters into semi-octets right-to-left
        factor - of the number of octets to be allocated
      • BCDStringCodec

        protected BCDStringCodec​(CharCodec charCodec,
                                 boolean packed,
                                 int filler,
                                 boolean swapped,
                                 int factor)
    • Method Detail

      • getHexVariant

        public BCDStringCodec<T> getHexVariant()
        Gets a BCDStringCodec like this one but that decodes to and encodes from hexadecimal characters, excepting 'F' if that is this codec's filler. The hex variant of a hex variant is itself (idempotent).
        Returns:
        BCDStringCodec
      • getLaxDecoder

        public BCDStringCodec<T> getLaxDecoder()
        Gets a BCDStringCodec that decodes like this one for valid digits and like the hex variant for invalid digits. It is not expected to be used for encoding, and does nothing useful in that case. The lax variant of a lax variant is itself (idempotent).
        Specified by:
        getLaxDecoder in interface StringCodec<T>
        Returns:
        BCDStringCodec
      • encodeString

        public byte[] encodeString​(String string,
                                   StringIndicators indicators)
        Encodes a String of characters to a byte array, accepting only characters present in the 16-character digit mapping, and encoding each in a semi-octet as [0,15]. Allocates the smallest sufficient number of octets that has the given factor. Writes two semi-octets per octet if packed, otherwise one. Writes the semi-octets right-to-left if swapped, and left-to-right if unswapped. Assigns the filler to any excess semi-octets. Sets indicators to assist decoding by this or other StringCodecs.
        Specified by:
        encodeString in interface StringCodec<T>
        Parameters:
        string - the String to be encoded to a byte array
        indicators - the StringIndicators object in which to set the indicators
        Returns:
        the encoded byte array
        Throws:
        UnsupportedDigitException - if any character of the given String is not encodable
      • encodeString

        public byte[] encodeString​(String string)
        Encodes a String of characters to a byte array, accepting only characters present in the 16-character digit mapping, and encoding each in a semi-octet as [0,15]. Allocates the smallest sufficient number of octets that has the given factor. Writes two semi-octets per octet if packed, otherwise one. Writes the semi-octets right-to-left if swapped, and left-to-right if unswapped. Assigns the filler to any excess semi-octets.
        Specified by:
        encodeString in interface StringCodec<T>
        Parameters:
        string - the String to be encoded to a byte array
        Returns:
        the encoded byte array
        Throws:
        UnsupportedDigitException - if any character of the given String is not encodable
      • decodeString

        public String decodeString​(byte[] encoded,
                                   StringIndicators indicators)
                            throws UnsupportedDigitException
        Decodes a String of characters from a byte array, rejecting any value marked as invalid (?) in the digit mapping. Reads two semi-octets per octet if packed, otherwise one. Reads from semi-octets right-to-left if swapped, and left-to-right if unswapped. The given indicators should include numSemiOctets, with odd being used if numSemiOctets is inconsistent with the length of the encoded form.
        Specified by:
        decodeString in interface StringCodec<T>
        Parameters:
        encoded - the byte array to be decoded to a String
        indicators - indicator values needed for decoding
        Returns:
        the decoded String
        Throws:
        UnsupportedDigitException - if any encoded content cannot be decoded by this codec
      • decodeString

        public String decodeString​(byte[] encoded)
                            throws UnsupportedDigitException
        Decodes a String of characters from a byte array, rejecting any value marked as invalid (?) in the digit mapping. Reads two semi-octets per octet if packed, otherwise one. Reads from semi-octets right-to-left if swapped, and left-to-right if unswapped.
        Specified by:
        decodeString in interface StringCodec<T>
        Parameters:
        encoded - the byte array to be decoded to a String
        Returns:
        the decoded String
        Throws:
        UnsupportedDigitException - if any encoded content cannot be decoded by this codec
      • getFactor

        public int getFactor()
        Gets the value of the factor.
        Returns:
        int factor