-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX9Writer.cs
113 lines (98 loc) · 3.89 KB
/
X9Writer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.IO;
using CompAnalytics.X9.Document;
namespace CompAnalytics.X9
{
public class X9Writer : X9Modifier
{
X9Document Document { get; set; }
public X9Writer(X9Document doc) : base()
{
this.Document = doc;
}
/// <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>
/// Converts the X9Document provided in the constructor to a byte array holding the full X9 file
/// </summary>
public byte[] WriteX9Document()
{
this.ByteStream = new MemoryStream();
this.WriteRecord(Document.Header);
foreach (X9Deposit deposit in Document.Deposits)
{
this.WriteRecord(deposit.CashLetterHeader);
foreach (X9Bundle bundle in deposit.Bundles)
{
bundle.ThrowIfInvalid();
this.WriteRecord(bundle.Header);
if (bundle.IsDeposit)
{
foreach (X9DepositItem item in bundle.DepositItems)
{
this.WriteRecord(item.CheckDetail);
if (item.CheckDetailAddendum != null)
{
this.WriteRecord(item.CheckDetailAddendum);
}
foreach (X9DepositItemImage image in item.Images)
{
this.WriteRecord(image.ImageViewDetail);
this.WriteRecord(image.ImageViewData);
}
}
}
else
{
foreach (X9ReturnItem item in bundle.ReturnItems)
{
this.WriteRecord(item.Return);
if (item.ReturnAddendumA != null)
{
this.WriteRecord(item.ReturnAddendumA);
}
if (item.ReturnAddendumB != null)
{
this.WriteRecord(item.ReturnAddendumB);
}
if (item.ReturnAddendumD != null)
{
this.WriteRecord(item.ReturnAddendumD);
}
foreach (X9DepositItemImage image in item.Images)
{
this.WriteRecord(image.ImageViewDetail);
this.WriteRecord(image.ImageViewData);
}
}
}
this.WriteRecord(bundle.Trailer);
}
this.WriteRecord(deposit.CashLetterTrailer);
}
this.WriteRecord(Document.Trailer);
byte[] ret;
using (var ms = new MemoryStream())
{
this.ByteStream.Position = 0;
this.ByteStream.CopyTo(ms);
ret = ms.ToArray();
}
return ret;
}
}
}