Last active
August 29, 2015 14:13
-
-
Save ronnieoverby/e9e1fb904191f872cfb7 to your computer and use it in GitHub Desktop.
Comment after something happens, not before!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Write(RecordBase record) | |
{ | |
if (record == null) | |
throw new ArgumentNullException("record"); | |
// we have something to write | |
var spec = Spec.GetRecordSpec(record); | |
if (spec == null) | |
throw new UnknownRecordTypeException("Unknown record type: " + record.GetType().FullName); | |
// we know how to write it (because we have the spec) | |
using (var memoryStream = new MemoryStream()) | |
{ | |
var binaryWriter = new BinaryWriter(memoryStream, _writer.Encoding); | |
spec.Write(binaryWriter, record); | |
// record was successfully converted to bytes | |
_writer.WriteBigEndian((int) memoryStream.Length); | |
// length of record written out to stream | |
memoryStream.Position = 0; | |
memoryStream.CopyTo(_writer.Stream); | |
// record data written out to stream | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment