-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRecord.cs
More file actions
197 lines (170 loc) · 6.92 KB
/
Record.cs
File metadata and controls
197 lines (170 loc) · 6.92 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using X937.Attributes;
namespace X937
{
/// <summary>
/// Provides a basic definition of an X937 record.
/// </summary>
public abstract class Record
{
#region Properties
/// <summary>
/// The type of record this is.
/// </summary>
[IntegerField( 1, 2 )]
public int RecordType { get; protected set; }
#endregion
#region Constructors
/// <summary>
/// Initialize a new data record.
/// </summary>
/// <param name="recordType">The type of record that is being initialized.</param>
protected Record( int recordType )
{
RecordType = recordType;
}
#endregion
#region Static Methods
/// <summary>
/// Decodes a record from the binary data reader. The reader should be positioned
/// at the RecordType field, not at the Record Length Indicator.
/// </summary>
/// <param name="reader">The reader containing the data to be decoded.</param>
/// <returns>A new Record object that has been initialized from the reader.</returns>
public static Record DecodeRecord( BinaryReader reader )
{
return DecodeRecord( reader, new DefaultRecordFactory() );
}
/// <summary>
/// Decodes a record from the binary data reader. The reader should be positioned
/// at the RecordType field, not at the Record Length Indicator.
/// </summary>
/// <param name="reader">The reader containing the data to be decoded.</param>
/// <param name="recordFactory">The factory in charge of instantiating the proper record class.</param>
/// <returns>A new Record object that has been initialized from the reader.</returns>
public static Record DecodeRecord( BinaryReader reader, IRecordFactory recordFactory )
{
int recordType = 0;
long startPosition = reader.BaseStream.Position;
//
// Determine the type of record to be decoded.
//
try
{
reader.ReadBytes( 4 ); // Skip record length indicator
recordType = Convert.ToInt32( reader.ReadEbcdicString( 2 ) );
}
catch
{
throw new ArgumentOutOfRangeException( "reader", "Unable to determine record type." );
}
//
// Initialize the blank record based on the type.
//
var record = recordFactory.GetRecordForType( recordType );
//
// Instruct the record to decode itself from the reader.
//
reader.BaseStream.Position = startPosition;
record.Decode( reader );
return record;
}
#endregion
#region Methods
/// <summary>
/// Decode and load a record into memory by using the data in the reader.
/// </summary>
/// <param name="reader">The BinaryReader that contains the data to be decoded.</param>
public virtual void Decode( BinaryReader reader )
{
int recordLength;
//
// Determine the length of the record as specified by the Record Length Indicator.
//
try
{
recordLength = System.Net.IPAddress.NetworkToHostOrder( BitConverter.ToInt32( reader.ReadBytes( 4 ), 0 ) );
}
catch ( Exception e )
{
throw new InvalidDataException( "Invalid record length.", e );
}
long startPosition = reader.BaseStream.Position;
//
// Get all properties that have a FieldAttribute defined on them and then order
// by the FieldNumber.
//
var fields = GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance )
.Where( p => Attribute.IsDefined( p, typeof( FieldAttribute ) ) )
.Select( p => new
{
Property = p,
Attribute = p.GetCustomAttribute<FieldAttribute>()
} )
.OrderBy( f => f.Attribute.FieldNumber )
.ToList();
//
// Step through each field and decode it into the record.
//
foreach ( var field in fields )
{
field.Attribute.DecodeField( reader, this, field.Property );
}
//
// Verify that the RLI matches how many bytes we actually decoded.
//
if ( recordLength != ( reader.BaseStream.Position - startPosition ) )
{
throw new InvalidDataException( string.Format( "Record Length Indicator {0} != {1} actual bytes decoded.", recordLength, reader.BaseStream.Position - startPosition ) );
}
}
/// <summary>
/// Encode the record into the BinaryWriter. This will include the Record Length Indicator.
/// </summary>
/// <param name="writer">The writer that will contain the data.</param>
public virtual void Encode( BinaryWriter writer )
{
Encode( writer, true );
}
/// <summary>
/// Encode the record into the BinaryWriter and optionally include the Record Length Indicator.
/// </summary>
/// <param name="writer">The writer that will contain the data.</param>
/// <param name="AddRecordSize">Should the record be prefixed with the length of the data.</param>
public virtual void Encode( BinaryWriter writer, bool AddRecordSize )
{
//
// Get all properties that have a FieldAttribute defined on them and then order
// by the FieldNumber.
//
var fields = GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance )
.Where( p => Attribute.IsDefined( p, typeof( FieldAttribute ) ) )
.Select( p => new
{
Property = p,
Attribute = p.GetCustomAttribute<FieldAttribute>()
} )
.OrderBy( f => f.Attribute.FieldNumber )
.ToList();
//
// Write the Record Length Indicator.
//
if ( AddRecordSize )
{
int recordSize = fields.Sum( f => f.Attribute.DataSize( this, f.Property ) );
writer.Write( BitConverter.GetBytes( System.Net.IPAddress.HostToNetworkOrder( recordSize ) ) );
}
//
// Step through each field and encode it into the data writer.
//
foreach ( var field in fields )
{
field.Attribute.EncodeField( writer, this, field.Property );
}
}
#endregion
}
}