Beispiel #1
0
        public void CreateBlobWithInputBytes()
        {
            byte[]     bytes      = { 0, 1, 2, 3, 4 };
            SpookyHash spookyHash = new SpookyHash();

            BlobAllocator allocator = new BlobAllocator();
            IBlob         blob      = allocator.CreateBlob(bytes);

            Assert.AreEqual((ulong)5, blob.Length);
            Assert.AreEqual(spookyHash.CalculateHash(bytes), blob.BlobId);
        }
Beispiel #2
0
        public void CreateBlobWithInputSubStream()
        {
            byte[]     bytes      = { 0, 1, 2, 3, 4 };
            SpookyHash spookyHash = new SpookyHash();

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                BlobAllocator allocator = new BlobAllocator();
                IBlob         blob      = allocator.CreateBlob(ms, 4);

                Assert.AreEqual((ulong)4, blob.Length);
                Assert.AreEqual(spookyHash.CalculateHash(new byte[] { 0, 1, 2, 3 }), blob.BlobId);
            }
        }
Beispiel #3
0
        internal void GetZipChunkingBlobs(ILogger logger, out string[] blobIds, out IReadOnlyDictionary <string, IBlob> blobs)
        {
            if (ResourceId.Equals(Constants.ZipChunkingResourceFiles.ZeroByteOfficeDocumentResourceId))
            {
                blobIds = new string[0];
                blobs   = new Dictionary <string, IBlob>();
                return;
            }

            try
            {
                List <string> offsets = new List <string>();

                string chunkIdsResourcePath = FilePath + Constants.ZipChunkingResourceFiles.ChunkIds;
                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(chunkIdsResourcePath))
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        while (!streamReader.EndOfStream)
                        {
                            offsets.Add(streamReader.ReadLine());
                        }
                    }

                string[] blobIdsToReturn = new string[offsets.Count];
                Dictionary <string, IBlob> blobsToReturn = new Dictionary <string, IBlob>();
                IBlobAllocator             blobAllocator = new BlobAllocator();
                string fileStreamResourcePath            = FilePath + Constants.ZipChunkingResourceFiles.FileStream;
                using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream(fileStreamResourcePath))
                {
                    for (int i = 0; i < offsets.Count; i++)
                    {
                        if (!int.TryParse(offsets[i], out int offset_1))
                        {
                            throw new IOException($"In {nameof(Resource.GetZipChunkingBlobs)}, FileName:{FileName}, FilePath:{FilePath}, Offset:{offsets[ i ]} parsing failed");
                        }

                        if (fs.Position != offset_1)
                        {
                            throw new IOException("Filestream position not equal to offset parsed from ChunkIds.txt");
                        }

                        int offset_2 = (int)fs.Length;
                        if (i + 1 < offsets.Count)
                        {
                            if (!int.TryParse(offsets[i + 1], out offset_2))
                            {
                                throw new IOException($"In {nameof(Resource.GetZipChunkingBlobs)}, FileName:{FileName}, FilePath:{FilePath}, Offset:{offsets[ i + 1 ]} parsing failed");
                            }
                        }

                        IBlob blob = blobAllocator.CreateBlob(fs, offset_2 - offset_1);
                        blobIdsToReturn[i] = blob.BlobId;
                        if (!blobsToReturn.ContainsKey(blob.BlobId))
                        {
                            blobsToReturn.Add(blob.BlobId, blob);
                        }
                    }
                }

                blobIds = blobIdsToReturn;
                blobs   = blobsToReturn;
                return;
            }
            catch (IOException ex)
            {
                logger.Log($"{nameof(Resource.GetZipChunkingBlobs)} IO Exception when trying to get resource content.");
                logger.Log(ex.Message);

                blobIds = new string[0];
                blobs   = new Dictionary <string, IBlob>();
                return;
            }
        }