예제 #1
0
        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 });
        }
예제 #2
0
        public Block GetNextBlock()
        {
            var buffer = this.GetNextBlockBytes();
            Block block = new Block
            {
                Id = this.counter++,
                OriginBlockSize = buffer.Length,
                Data = buffer
            };

            return block;
        }
예제 #3
0
        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();
            }
        }
예제 #4
0
        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;
        }
예제 #5
0
        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();
        }
예제 #6
0
        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);
        }
예제 #7
0
        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();
            }
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
 public void WriteBlock(Block block)
 {
     WriteBlock(block.Data, block.Id);
 }
예제 #10
0
        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;
        }
예제 #11
0
 public void WriteBlock(Block block)
 {
     var buffer = this.ToByteArray(block);
     this.stream.Write(buffer, 0, buffer.Length);
 }