public void TestMixCopy() { var array = new byte[] { 0, 1, 2, 3, 4, 5 }; var chunkedStream = new ChunkedStream(); chunkedStream.Write(array, 1, 3); chunkedStream.Write(array, 2, 4); chunkedStream.Write(array, 3, 3); var b = chunkedStream.ReadByte(); Assert.AreEqual(b, array[1]); var destArray = new byte[8]; chunkedStream.Read(destArray, 0, destArray.Length); Assert.AreEqual(array[2], destArray[0]); Assert.AreEqual(array[3], destArray[1]); Assert.AreEqual(array[2], destArray[2]); Assert.AreEqual(array[3], destArray[3]); Assert.AreEqual(array[4], destArray[4]); Assert.AreEqual(array[5], destArray[5]); Assert.AreEqual(array[3], destArray[6]); Assert.AreEqual(array[4], destArray[7]); }
public void TestRead() { byte[] content = null; using (MemoryStream file = new MemoryStream()) { byte[] buffer = new byte[this.chunkSize]; this.FillArray <byte>(buffer, (byte)'1'); file.Write(buffer, 0, this.chunkSize); this.FillArray <byte>(buffer, (byte)'2'); file.Write(buffer, 0, this.chunkSize); this.FillArray <byte>(buffer, (byte)'3'); file.Write(buffer, 0, 3); content = file.ToArray(); } using (Stream file = new MemoryStream(content)) using (ChunkedStream chunked = new ChunkedStream(file, this.chunkSize)) { byte[] buffer = new byte[this.chunkSize]; byte[] result = new byte[this.chunkSize]; Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); this.FillArray <byte>(buffer, (byte)'1'); Assert.AreEqual(1, chunked.Read(result, 0, 1)); Assert.IsTrue(this.EqualArray(buffer, result, 1)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(1, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); Assert.AreEqual(this.chunkSize - 1, chunked.Read(result, 1, this.chunkSize)); Assert.IsTrue(this.EqualArray(buffer, result, this.chunkSize)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(this.chunkSize, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); Assert.AreEqual(0, chunked.Read(result, 0, this.chunkSize)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(this.chunkSize, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); chunked.ChunkPosition = 2 * this.chunkSize; Assert.AreEqual(2 * this.chunkSize, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(3, chunked.Length); this.FillArray <byte>(buffer, (byte)'3'); Assert.AreEqual(3, chunked.Read(result, 0, this.chunkSize)); Assert.IsTrue(this.EqualArray(buffer, result, 3)); Assert.AreEqual(2 * this.chunkSize, chunked.ChunkPosition); Assert.AreEqual(3, chunked.Position); Assert.AreEqual(3, chunked.Length); chunked.ChunkPosition = this.chunkSize; Assert.AreEqual(this.chunkSize, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); this.FillArray <byte>(buffer, (byte)'2'); for (int i = 0; i < this.chunkSize; ++i) { Assert.AreEqual(1, chunked.Read(result, i, 1)); } Assert.IsTrue(this.EqualArray(buffer, result, this.chunkSize)); Assert.AreEqual(this.chunkSize, chunked.ChunkPosition); Assert.AreEqual(this.chunkSize, chunked.Position); Assert.AreEqual(this.chunkSize, chunked.Length); } }
public void TestRead() { //using (Database database = new Database(DatabasePath)) { using (Stream file = File.OpenWrite(TestFilePath)) { byte[] buffer = new byte[ChunkSize]; FillArray <byte>(buffer, (byte)'1'); file.Write(buffer, 0, ChunkSize); FillArray <byte>(buffer, (byte)'2'); file.Write(buffer, 0, ChunkSize); FillArray <byte>(buffer, (byte)'3'); file.Write(buffer, 0, 3); } using (Stream file = new FileStream(TestFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) using (ChunkedStream chunked = new ChunkedStream(file, ChunkSize)) { byte[] buffer = new byte[ChunkSize]; byte[] result = new byte[ChunkSize]; Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); FillArray <byte>(buffer, (byte)'1'); Assert.AreEqual(1, chunked.Read(result, 0, 1)); Assert.IsTrue(EqualArray(buffer, result, 1)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(1, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); Assert.AreEqual(ChunkSize - 1, chunked.Read(result, 1, ChunkSize)); Assert.IsTrue(EqualArray(buffer, result, ChunkSize)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(ChunkSize, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); Assert.AreEqual(0, chunked.Read(result, 0, ChunkSize)); Assert.AreEqual(0, chunked.ChunkPosition); Assert.AreEqual(ChunkSize, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); chunked.ChunkPosition = 2 * ChunkSize; Assert.AreEqual(2 * ChunkSize, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(3, chunked.Length); FillArray <byte>(buffer, (byte)'3'); Assert.AreEqual(3, chunked.Read(result, 0, ChunkSize)); Assert.IsTrue(EqualArray(buffer, result, 3)); Assert.AreEqual(2 * ChunkSize, chunked.ChunkPosition); Assert.AreEqual(3, chunked.Position); Assert.AreEqual(3, chunked.Length); chunked.ChunkPosition = ChunkSize; Assert.AreEqual(ChunkSize, chunked.ChunkPosition); Assert.AreEqual(0, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); FillArray <byte>(buffer, (byte)'2'); for (int i = 0; i < ChunkSize; ++i) { Assert.AreEqual(1, chunked.Read(result, i, 1)); } Assert.IsTrue(EqualArray(buffer, result, ChunkSize)); Assert.AreEqual(ChunkSize, chunked.ChunkPosition); Assert.AreEqual(ChunkSize, chunked.Position); Assert.AreEqual(ChunkSize, chunked.Length); } } }