Пример #1
0
        public void Write(SampleStruct chunk)
        {
            if (chunk.Elapsed < 0)
            {
                throw new SampleException("Chunk has an invalid elapsed value");
            }
            else if (chunk.Length <= 0)
            {
                throw new SampleException("Chunk has an invalid length");
            }
            else if (chunk.Data?.Length == 0)
            {
                throw new SampleException("Chunk has no data");
            }
            else if (chunk.Length != chunk.Data.Length)
            {
                throw new SampleException("Chunk has an invalid data length");
            }

            try
            {
                _writer.Write(chunk.Elapsed);
                _writer.Write(chunk.Length);
                _writer.Write(chunk.Data);
                Writes++;
            }
            catch (Exception ex)
            {
                throw new SampleException("Unexpected sample write error", ex);
            }
        }
Пример #2
0
        public bool TryRead(out SampleStruct chunk)
        {
            EndOfStream = false;

            try
            {
                long elapsed = _reader.ReadInt64();
                int  length  = _reader.ReadInt32();

                if (elapsed >= 0 && length > 0)
                {
                    byte[] data = _reader.ReadBytes(length);

                    if (length == data.Length)
                    {
                        chunk = new SampleStruct
                        {
                            Elapsed = elapsed,
                            Length  = length,
                            Data    = data
                        };
                        Reads++;
                        return(true);
                    }
                }

                chunk = default;
                return(false);
            }
            catch (EndOfStreamException)
            {
                EndOfStream = true;
                chunk       = default;
                return(false);
            }
            catch (Exception ex)
            {
                throw new SampleException("Unexpected sample read error", ex);
            }
        }