public void WriteBlockTest() { // Arrage MemoryStream stream = new MemoryStream(); DefaultStreamWriter writer = new DefaultStreamWriter(stream, 4); Block block1 = new Block() { Id = 0, Data = new byte[] { 1, 1, 1, 1 } }; Block block2 = new Block() { Id = 1, Data = new byte[] { 2, 2 } }; // Act writer.WriteBlock(block1); writer.WriteBlock(block2); byte[] result = stream.ToArray(); // Assert Assert.AreEqual(result.Length, 6); Assert.AreEqual(result, new byte[] { 1, 1, 1, 1, 2, 2 }); }
public Block GetNextBlock() { var buffer = this.GetNextBlockBytes(); Block block = new Block { Id = this.counter++, OriginBlockSize = buffer.Length, Data = buffer }; return block; }
public void Act(Block block) { byte[] hash = this.hasher.GetHash(block.Data); var hashString = Hasher.Hasher.ToString(hash); block.Hash = hash; Console.WriteLine("Block id = {0} Hash = {1}", block.Id, hashString); using(var stream = new MemoryStream()) { using(var compressStream = new GZipStream(stream, CompressionMode.Compress)) { compressStream.Write(block.Data, 0, block.OriginBlockSize); } block.Data = stream.ToArray(); } }
private byte[] ToByteArray(Block block) { byte[] idBytes = BitConverter.GetBytes(block.Id); byte[] originSize = BitConverter.GetBytes(block.OriginBlockSize); int blockSize = idBytes.Length + originSize.Length + block.Hash.Length + block.Data.Length; byte[] blockSizeBytes = BitConverter.GetBytes(blockSize); byte[] result = new byte[blockSize + 4]; Array.Copy(blockSizeBytes, result, blockSizeBytes.Length); Array.Copy(idBytes, 0, result, blockSizeBytes.Length, idBytes.Length); Array.Copy(originSize, 0, result, blockSizeBytes.Length + idBytes.Length, originSize.Length); Array.Copy(block.Hash, 0, result, blockSizeBytes.Length + idBytes.Length + originSize.Length, block.Hash.Length); Array.Copy(block.Data, 0, result, blockSizeBytes.Length + idBytes.Length + originSize.Length + block.Hash.Length, block.Data.Length); return result; }
public void AddUnhandledBlock(Block block) { bool isFullQueue = false; lock (this.inpitLockObject) { isFullQueue = this.input.Count >= MAX_QUEUE_SIZE; } if(isFullQueue) this.readAdd.WaitOne(); lock (this.inpitLockObject) { this.input.Enqueue(block); } this.readGet.Set(); }
public void GetByteArrayTest() { // Arrage Block block = new Block { Id = 1, OriginBlockSize = 1, Hash = new byte[] { 1, 2, 3, 4 }, Data = new byte[] { 1, 2, 3, 4 } }; // Act MemoryStream stream = new MemoryStream(); BlockStreamWriter streamWriter = new BlockStreamWriter(stream); streamWriter.WriteBlock(block); var buffer = stream.ToArray(); // Assert Assert.AreEqual(buffer.Length, 20); }
public void Act(Block block) { byte[] data = new byte[block.OriginBlockSize]; using(var stream = new MemoryStream(block.Data)) { using(var compressStream = new GZipStream(stream, CompressionMode.Decompress, true)) { compressStream.Read(data, 0, block.OriginBlockSize); stream.Close(); compressStream.Close(); } } block.Data = data; byte[] hash = this.hasher.GetHash(block.Data); var hashString = Hasher.Hasher.ToString(hash); Console.WriteLine("Block id = {0} Hash = {1}", block.Id, hashString); if(!block.Hash.SequenceEqual(hash)) { throw new InvalidDataException(); } }
private void EnqueueHandledBlock(Block block) { bool isFullQueue = false; lock (this.outputLockObject) { isFullQueue = this.output.Count >= MAX_QUEUE_SIZE; } if(isFullQueue) this.writeAdd.WaitOne(); lock (this.outputLockObject) { this.output.Enqueue(block); } }
public void WriteBlock(Block block) { WriteBlock(block.Data, block.Id); }
public Block GetNextBlock() { byte[] bytes = new byte[4]; stream.Read(bytes, 0, bytes.Length); int blockSize = BitConverter.ToInt32(bytes, 0); if (blockSize < hashSize + 4) throw new ArgumentOutOfRangeException(); byte[] buffer = new byte[blockSize]; stream.Read(buffer, 0, blockSize); uint blockId = BitConverter.ToUInt32(buffer, 0); int originBlockSize = BitConverter.ToInt32(buffer, 4); byte[] hash = new byte[hashSize]; Array.Copy(buffer, 8, hash, 0, hash.Length); byte[] data = new byte[blockSize - 8 - hashSize]; Array.Copy(buffer, 8 + hashSize, data, 0, data.Length); Block block = new Block { Id = blockId, OriginBlockSize = originBlockSize, Hash = hash, Data = data }; return block; }
public void WriteBlock(Block block) { var buffer = this.ToByteArray(block); this.stream.Write(buffer, 0, buffer.Length); }