Skip to content

Instantly share code, notes, and snippets.

@ronnieoverby
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnieoverby/e9e1fb904191f872cfb7 to your computer and use it in GitHub Desktop.
Save ronnieoverby/e9e1fb904191f872cfb7 to your computer and use it in GitHub Desktop.
Comment after something happens, not before!
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