Beispiel #1
0
        private byte[] Pad()
        {
            if (Value == null)
            {
                byteValue = FillByte.NewArray(Length.ToInt()).Convert(Encoder.SourceEncoding, Encoder.DestinationEncoding);
            }
            if (byteValue.Length > Length)
            {
                throw new InvalidCastException(
                          $"{IO.FileParser.Properties.Resources.ResourceManager.GetString("InvalidLength")} {Value}");
            }
            if (byteValue.Length == Length)
            {
                return(byteValue);
            }
            byte fb = FillByte.Convert(Encoder.SourceEncoding, Encoder.DestinationEncoding);

            if (DataFormat == FileFieldDataFormat.IBMPacked)
            {
                byteValue = byteValue.PadLeft(Length.ToInt(), fb, true);
            }
            else if (RightAlign)
            {
                byteValue = byteValue.PadLeft(Length.ToInt(), fb, true);
            }
            else
            {
                byteValue = byteValue.PadRight(Length.ToInt(), fb, true);
            }
            return(byteValue);
        }
        /// <summary>
        /// Packs the <see cref="Records">Records</see> and their <see cref="IFileRecord.Fields">Fields</see>
        /// using the properties provided.
        /// </summary>
        public byte[] Pack()
        {
            List <byte> bytes = new List <byte>();

            if (Layout.HeaderRecord?.Fields?.Length > 0)
            {
                bytes.AddRange(Layout.HeaderRecord.Pack(Length, FillByte));
            }
            foreach (var record in Records)
            {
                bytes.AddRange(record.Pack(Length, FillByte));
            }
            if (Layout.FooterRecord?.Fields?.Length > 0)
            {
                bytes.AddRange(Layout.FooterRecord.Pack(Length, FillByte));
            }
            if (Length > bytes.Count)
            {
                bytes = bytes.ToArray().PadRight(Length.ToInt(),
                                                 FillByte.Convert(Encoder.SourceEncoding, Encoder.DestinationEncoding), true).ToList();
            }
            ByteValue = bytes.ToArray();
            return(ByteValue);
        }