예제 #1
0
        public void ChunkedMemoryStreamCanSeekCorrectly()
        {
            var buffers             = new[] { new byte[10], new byte[20] };
            var chunkedMemoryStream = new ChunkedMemoryStream(buffers, 0, 30);

            chunkedMemoryStream.Seek(10, SeekOrigin.Current);

            Assert.AreEqual(30, chunkedMemoryStream.Length);
            Assert.AreEqual(10, chunkedMemoryStream.Position);
        }
예제 #2
0
        public void TestSeekInternalStateNotChanged1()
        {
            using (var stream = new ChunkedMemoryStream()) {
            Assert.AreEqual(0L, stream.Position);
            Assert.AreEqual(0L, stream.Length);

            Assert.AreEqual(0L, stream.Seek(0L, SeekOrigin.Begin));
            Assert.AreEqual(0L, stream.Length);

            stream.Write(new byte[] {0x00, 0x01, 0x02, 0x03}, 0, 4);

            Assert.AreEqual(4L, stream.Position);
            Assert.AreEqual(4L, stream.Length);
              }
        }
예제 #3
0
        public void TestSeekEnd()
        {
            using (var stream = new ChunkedMemoryStream(8)) {
            for (var i = 0; i < 0x20; i++) {
              stream.WriteByte((byte)i);
            }

            Assert.AreEqual(0x00, stream.Seek(-0x20, SeekOrigin.End));
            Assert.AreEqual(0x00, stream.ReadByte());

            Assert.AreEqual(0x18, stream.Seek(-0x08, SeekOrigin.End));
            Assert.AreEqual(0x18, stream.ReadByte());

            Assert.AreEqual(0x0f, stream.Seek(-0x11, SeekOrigin.End));
            Assert.AreEqual(0x0f, stream.ReadByte());

            Assert.AreEqual(0x40, stream.Seek(+0x20, SeekOrigin.End));

            try {
              stream.Seek(-0x41, SeekOrigin.End);
              Assert.Fail("IOException not thrown");
            }
            catch (IOException) {
            }
              }
        }
예제 #4
0
        public void TestSeekAndReadRandom()
        {
            using (var stream = new ChunkedMemoryStream(8)) {
            for (var i = 0; i < 12; i++) {
              stream.WriteByte((byte)i);
            }

            Assert.IsTrue(stream.CanSeek);
            Assert.AreEqual(12L, stream.Position);
            Assert.AreEqual(12L, stream.Length);

            Assert.AreEqual(6L, stream.Seek(6L, SeekOrigin.Begin));
            Assert.AreEqual(6L, stream.Position);

            var pair = new long[][] {
              // offset / position
              new long[] { 0, 6},
              new long[] {-2, 5},
              new long[] { 1, 7},
              new long[] {-4, 4},
              new long[] { 3, 8},
              new long[] {-6, 3},
              new long[] { 5, 9},
              new long[] {-8, 2},
              new long[] { 7,10},
              new long[] {-10, 1},
              new long[] { 9,11},
            };

            for (var index = 0; index < pair.Length; index++) {
              try {
            Assert.AreEqual(pair[index][1], stream.Seek(pair[index][0], SeekOrigin.Current), "seeked position {0}", index);
              }
              catch (IOException) {
            Assert.Fail("IOException thrown while seeking ({0})", index);
              }

              Assert.AreEqual(pair[index][1], stream.Position);
              Assert.AreEqual(pair[index][1], stream.ReadByte(), "read value {0}", index);
              Assert.AreEqual(pair[index][1] + 1, stream.Position);
            }

            Assert.AreEqual(-1, stream.ReadByte());
            Assert.AreEqual(13, stream.Seek(1, SeekOrigin.Current));
            Assert.AreEqual(-1, stream.ReadByte());
            Assert.AreEqual(13, stream.Position);
              }
        }
예제 #5
0
        public void TestReadByte()
        {
            using (var stream = new ChunkedMemoryStream(8)) {
            for (var i = 0; i < 32; i++) {
              stream.WriteByte((byte)i);
            }

            Assert.AreEqual(0L, stream.Seek(0L, SeekOrigin.Begin));

            Assert.AreEqual(0L, stream.Position);
            Assert.AreEqual(32L, stream.Length);

            for (var i = 0; i < 32; i++) {
              Assert.AreEqual((long)i, stream.Position);
              Assert.AreEqual(i, stream.ReadByte());
              Assert.AreEqual((long)i + 1, stream.Position);
            }

            Assert.AreEqual(-1, stream.ReadByte());
              }
        }
예제 #6
0
        public void TestRead()
        {
            using (var stream = new ChunkedMemoryStream(8)) {
            for (var i = 0; i < 32; i++) {
              stream.WriteByte((byte)i);
            }

            Assert.AreEqual(0L, stream.Seek(0, SeekOrigin.Begin));

            var buffer = new byte[16];

            Assert.AreEqual(1, stream.Read(buffer, 0, 1));
            Assert.AreEqual(new byte[] {0x00}, buffer.Slice(0, 1));
            Assert.AreEqual(1L, stream.Position);

            Assert.AreEqual(3, stream.Read(buffer, 0, 3));
            Assert.AreEqual(new byte[] {0x01, 0x02, 0x03}, buffer.Slice(0, 3));
            Assert.AreEqual(4L, stream.Position);

            Assert.AreEqual(4, stream.Read(buffer, 0, 4));
            Assert.AreEqual(new byte[] {0x04, 0x05, 0x06, 0x07}, buffer.Slice(0, 4));
            Assert.AreEqual(8L, stream.Position);

            Assert.AreEqual(7, stream.Read(buffer, 0, 7));
            Assert.AreEqual(new byte[] {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e}, buffer.Slice(0, 7));
            Assert.AreEqual(15L, stream.Position);

            Assert.AreEqual(2, stream.Read(buffer, 0, 2));
            Assert.AreEqual(new byte[] {0x0f, 0x10}, buffer.Slice(0, 2));
            Assert.AreEqual(17L, stream.Position);

            Assert.AreEqual(15, stream.Read(buffer, 0, 16));
            Assert.AreEqual(new byte[] {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, buffer.Slice(0, 15));
            Assert.AreEqual(32L, stream.Position);
              }
        }
예제 #7
0
 /// <summary>
 /// Read the next unread record from the stream. Will return <see cref="QueueStreamRecord.Empty"/> if there is no next record in the stream.
 /// </summary>
 /// <returns></returns>
 public QueueStreamRecord ReadNextRecord()
 {
     EnsureInstanceNotDisposed();
     if(_recordQueue.Count == 0) {
         return QueueStreamRecord.Empty;
     }
     var next = _recordQueue.Dequeue();
     var streamInfo = GetStreamInfoFromHandle(next);
     if(streamInfo == null) {
         EnsureInstanceNotDisposed();
         throw new InvalidOperationException("unable to access stream for head file");
     }
     streamInfo.Stream.Seek(next.Position + HEADER_SIZE, SeekOrigin.Begin);
     var data = new ChunkedMemoryStream();
     var copied = streamInfo.Stream.CopyTo(data, next.Length);
     if(copied != next.Length) {
         _log.WarnFormat("reached EOF in the middle of the record");
         return QueueStreamRecord.Empty;
     }
     data.Seek(0, SeekOrigin.Begin);
     return new QueueStreamRecord(data, next);
 }