/// <summary> /// Convert the provided X9 record to a sequence of bytes and write it to the byte stream /// </summary> void WriteRecord(X9Record record) { // Generate bytes for record and its size prefix byte[] recordBytes = record.WriteRecord(); byte[] recordSizePrefix = BitConverter.GetBytes(recordBytes.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(recordSizePrefix); } // Write them to the stream this.ByteStream.Write(recordSizePrefix, 0, recordSizePrefix.Length); this.ByteStream.Write(recordBytes, 0, recordBytes.Length); }
/// <summary> /// Compare this record to another (usually of the same type) and return whether or not they are identical. /// </summary> public bool Equals(X9Record that) { if (this.GetType() != that.GetType()) { return(false); } else { List <X9Field> thisFields = this.GetFields().ToList(); List <X9Field> thatFields = that.GetFields().ToList(); if (thisFields.Count() != thatFields.Count()) { return(false); } else { for (int i = 0; i < thisFields.Count(); i++) { X9Field thisField = thisFields[i]; X9Field thatField = thatFields[i]; if (thisField.GetType() != thatField.GetType()) { return(false); } else { if (thisField.GetType() == typeof(X9TextField)) { if (!((X9TextField)thisField).Equals((X9TextField)thatField)) { return(false); } } else if (thisField.GetType() == typeof(X9ImageField)) { if (!((X9ImageField)thisField).Equals((X9ImageField)thatField)) { return(false); } } } } } } return(true); }
/// <summary> /// Reads and parses the next X9 record from the file. The type parameter must match the type of the next record found. /// </summary> public T ReadNextRecord <T>() { string recordTypeCode = this.PeekNextRecordTypeCode(); Type recordType = RecordTypes.GetByCode(recordTypeCode); byte[] recordBytes = this.ReadNextRecordBytes(); if (recordType != null && typeof(T) == recordType) { X9Record record = (X9Record)Activator.CreateInstance(recordType); record.ReadRecord(recordBytes); return((T)Convert.ChangeType(record, typeof(T))); } else if (typeof(T) != recordType) { throw new FormatException($"Expected to see {typeof(T).Name}, saw {(recordType != null ? recordType.Name : recordTypeCode)} instead."); } else { return(default(T)); } }