/// <summary> /// Convert recording objects into their protobuf counterpart. /// </summary> /// <param name="recordings">Recordings to convert.</param> /// <returns>The recordings in the protobuf format.</returns> /// <exception cref="System.ArgumentException">Thrown when the recordings presented is null or empty.</exception> public static Transport.Recording[] ToTransport(params Recording[] recordings) { if (recordings == null) { throw new ArgumentException("Nothing to convert (null recordings)"); } if (recordings.Length == 0) { throw new ArgumentException("Nothing to convert (empty recordings array)"); } Transport.Recording[] transports = new Transport.Recording[recordings.Length]; for (int recordingIndex = 0; recordingIndex < recordings.Length; recordingIndex++) { if (recordings[recordingIndex] == null) { throw new ArgumentException(string.Format("Null Recording at index {0}", recordingIndex)); } transports[recordingIndex] = new Transport.Recording { Name = recordings[recordingIndex].RecordingName }; transports[recordingIndex].CustomEvents.AddRange(ToTransport(recordings[recordingIndex].CapturedCustomEvents)); transports[recordingIndex].Metadata.Add(recordings[recordingIndex].Metadata); transports[recordingIndex].Subjects.AddRange(ToTransport(recordings[recordingIndex].SubjectRecordings)); } return(transports); }
/// <summary> /// Reads from a stream of data and builds an array of recordings that can be used for playback. /// </summary> /// <param name="stream">The stream to be read from for building Recording objects.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">Thrown when the stream is null or empty.</exception> /// <exception cref="System.SystemException">Thrown when the stream contains data in an unexpected format.</exception> public static Recording[] Unpackage(Stream stream) { if (stream == null || stream.Length == 0) { throw new ArgumentException("Nothing to unpackage (stream is null or empty)"); } int fileVersion = stream.ReadByte(); if (fileVersion == -1) { throw new SystemException("End of stream prematurely! Not enough data to build recordings from"); } if (fileVersion != 1) { throw new SystemException(string.Format("Unsupported File Version: {0}", fileVersion)); } byte[] numberRecordingsByteData = new byte[4]; stream.Read(numberRecordingsByteData, 0, 4); int numberRecordings = BitConverter.ToInt32(numberRecordingsByteData, 0); using (BinaryReader reader = new BinaryReader(stream)) { Transport.Recording[] recordings = new Transport.Recording[numberRecordings]; for (int i = 0; i < numberRecordings; i++) { var transportDataCompressed = new MemoryStream(reader.ReadBytes((int)reader.ReadInt64())); using (DeflateStream dstream = new DeflateStream(transportDataCompressed, CompressionMode.Decompress)) { recordings[i] = Transport.Recording.Parser.ParseFrom(dstream); } } return(FromTransport(recordings)); } }