public void Dispose() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose); cacheStream.Dispose(); try { cacheStream.Position = 0; cacheStream.ReadByte(); Assert.True(false, "Cache stream should have failed - disposed"); } catch (ObjectDisposedException) { } try { ms.Position = 0; ms.ReadByte(); Assert.True(false, "Cache stream should have failed - disposed"); } catch (ObjectDisposedException) { } }
public void Write() { MemoryStream ms = CreateSequencedMemStream(100, true); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 10; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 10); cacheStream.Position = 20; cacheStream.Write(new byte[10], 0, 10); Assert.Equal(30, cacheStream.Position); cacheStream.Position = 10; buffer = new byte[30]; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0, 10, 10); AssertSequenced(buffer, 20, 10, 30); Assert.Equal(0, buffer[10]); Assert.Equal(0, buffer[19]); }
public void FailWrite() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[25]; cacheStream.Position = 0; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0); int freeBefore = cacheStream.Statistics.FreeReadBlocks; cacheStream.Position = 11; try { cacheStream.Write(new byte[10], 0, 10); } catch (NotSupportedException) { Assert.Equal(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks); } }
public void UnalignedCachedRead() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(0, cacheStream.Statistics.ReadCacheHits); Assert.Equal(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[buffer.Length]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(1, cacheStream.Statistics.ReadCacheHits); Assert.Equal(2, cacheStream.Statistics.TotalReadsIn); }
public void CacheBlockRecycle() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 50, LargeReadSize = 100 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[50]; cacheStream.Position = 10; int numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.Equal(50, numRead); AssertSequenced(buffer, 10); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(0, cacheStream.Statistics.ReadCacheHits); Assert.Equal(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[40]; cacheStream.Position = 50; numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.Equal(40, numRead); AssertSequenced(buffer, 50); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(1, cacheStream.Statistics.ReadCacheHits); Assert.Equal(2, cacheStream.Statistics.TotalReadsIn); }
public void Dispose() { if (_bitmap != null) { _bitmap.Dispose(); _bitmap = null; } }
internal DiscImageFile(Uri uri, string userName, string password) { Content = new BufferStream(new DiscContentBuffer(uri, userName, password), FileAccess.Read); BlockCacheSettings cacheSettings = new BlockCacheSettings { BlockSize = (int)(32 * Sizes.OneKiB), OptimumReadSize = (int)(128 * Sizes.OneKiB) }; Content = new BlockCacheStream(Content, Ownership.Dispose); }
protected override void DoRun() { VolumeManager volMgr = new VolumeManager(); foreach (string disk in _diskFiles.Values) { volMgr.AddDisk(VirtualDisk.OpenDisk(disk, FileAccess.Read, UserName, Password)); } Stream partitionStream = null; if (!string.IsNullOrEmpty(VolumeId)) { partitionStream = volMgr.GetVolume(VolumeId).Open(); } else if (Partition >= 0) { partitionStream = volMgr.GetPhysicalVolumes()[Partition].Open(); } else { partitionStream = volMgr.GetLogicalVolumes()[0].Open(); } SparseStream cacheStream = SparseStream.FromStream(partitionStream, Ownership.None); cacheStream = new BlockCacheStream(cacheStream, Ownership.None); NtfsFileSystem fs = new NtfsFileSystem(cacheStream); fs.NtfsOptions.HideHiddenFiles = !_showHidden.IsPresent; fs.NtfsOptions.HideSystemFiles = !_showSystem.IsPresent; fs.NtfsOptions.HideMetafiles = !_showMeta.IsPresent; fs.Dump(Console.Out, ""); }
public void Bug5203_IncreaseSize() { MemoryStream ms = new MemoryStream(); BlockCacheSettings settings = new BlockCacheSettings { BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024 }; BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); // Pre-load read cache with a 'short' block bcs.Write(new byte[11], 0, 11); bcs.Position = 0; bcs.Read(new byte[11], 0, 11); // Extend stream for (int i = 0; i < 20; ++i) { bcs.Write(new byte[11], 0, 11); } // Try to read from first block beyond length of original cached short length // Bug was throwing exception here bcs.Position = 60; bcs.Read(new byte[20], 0, 20); }
public Bitmap(Stream stream, long maxIndex) { _stream = stream; _maxIndex = maxIndex; _bitmap = new BlockCacheStream(SparseStream.FromStream(stream, Ownership.None), Ownership.None); }
private void button2_Click(object sender, EventArgs e) { DiscUtils.VolumeManager volMgr = new VolumeManager(); volMgr.AddDisk(VirtualDisk.OpenDisk(textBox1.Text, FileAccess.Read)); Stream partitionStream = null; if (!string.IsNullOrEmpty(VolumeId)) { partitionStream = volMgr.GetVolume(VolumeId).Open(); } else if (Partition >= 0) { partitionStream = volMgr.GetPhysicalVolumes()[Partition].Open(); } else { partitionStream = volMgr.GetLogicalVolumes()[0].Open(); } SparseStream cacheStream = SparseStream.FromStream(partitionStream, Ownership.None); cacheStream = new BlockCacheStream(cacheStream, Ownership.None); fs = new NtfsFileSystem(cacheStream); fs.NtfsOptions.HideHiddenFiles = false; fs.NtfsOptions.HideSystemFiles = false; fs.NtfsOptions.HideMetafiles = false; ClusterMap clusterMap = null; try { clusterMap = fs.BuildClusterMap(); } catch (IOException ex) { // DebugConsole.LogException(ex, "Trying to build a clustermap of " + textBox1.Text); MessageBox.Show(ex.Message, "Exception"); // return; } // string[] files = null; // string[] sysfiles = fs.GetFileSystemEntries(@"\"); // string[] files = fs.GetFiles(@"\", "*.*", SearchOption.AllDirectories); // fs.Dump(Console.Out, ""); //QueuedBackgroundWorker.QueueWorkItem(QueuedBackgroundWorker.m_Queue, null, // (args) => { return fs.GetFiles(@"\", "*.*", SearchOption.AllDirectories); }, // (args) => { if (args.Result != null && args.Result is string[]) ProcessFiles((string[])args.Result); } //); BitArray ba = new BitArray((int)fs.TotalClusters); for (int i = 0; i < fs.TotalClusters; i++) { if (clusterMap.ClusterToPaths(i).Length > 0) { ba.Set(i, true); } } ClusterForm cf = new ClusterForm(); cf.Icon = Icon; cf.Text = Text + " - Cluster Map"; //cf.clusterBitmaps1.GradientStart = Color.WhiteSmoke; //cf.clusterBitmaps1.GradientEnd = Color.Black; //cf.clusterBitmaps1.GradientSteps = 8; cf.clusterBitmaps1.Clusters = ba; cf.Show(); }
public BitArray GetClusterUsage() { if (vhd.masterBootRecord.partition[Partition].PartitionType != 7) { return(null); } try { DiscUtils.VolumeManager volMgr = new VolumeManager(); using (VirtualDisk virtualDisk = VirtualDisk.OpenDisk(vhd.footer.FileName, FileAccess.Read)) { volMgr.AddDisk(virtualDisk); using (Stream partitionStream = volMgr.GetPhysicalVolumes()[Partition].Open()) { //if (!string.IsNullOrEmpty(VolumeId)) //{ // partitionStream = volMgr.GetVolume(VolumeId).Open(); //} //else if (Partition >= 0) //{ // partitionStream = volMgr.GetPhysicalVolumes()[Partition].Open(); //} //else //{ // partitionStream = volMgr.GetLogicalVolumes()[0].Open(); //} SparseStream cacheStream = SparseStream.FromStream(partitionStream, Ownership.None); cacheStream = new BlockCacheStream(cacheStream, Ownership.None); try { fs = new NtfsFileSystem(cacheStream); } catch { cacheStream.Dispose(); return(null); } fs.NtfsOptions.HideHiddenFiles = false; fs.NtfsOptions.HideSystemFiles = false; fs.NtfsOptions.HideMetafiles = false; BitArray ba = fs.BuildClusterBitArray(); fs.Dispose(); cacheStream.Dispose(); partitionStream.Dispose(); ClusterBitArray = ba; } } } catch (VhdReadException ex) { this.vhd.vhdReadException = ex; return(null); } return(ClusterBitArray); /* * ClusterMap clusterMap = null; * try * { * clusterMap = fs.BuildClusterMap(); * } * catch (IOException ex) * { * // TODO: Need to some error notfication * // MessageBox.Show(ex.Message, "Exception"); * return null; * } * * * BitArray ba = new BitArray((int)fs.TotalClusters); */ //UsedClusters = 0; //FreeClusters = 0; //TotalClusters = fs.TotalClusters; //for (int i = 0; i < TotalClusters; i++) //{ // if (clusterMap.ClusterToPaths(i).Length > 0) // { // UsedClusters++; // ba.Set(i, true); // } // else // { // FreeClusters++; // } //} /* * ClusterForm cf = new ClusterForm(); * cf.Icon = Icon; * cf.Text = Text + " - Cluster Map"; * cf.clusterBitmaps1.Clusters = ba; * cf.Show(); */ //cf.clusterBitmaps1.GradientStart = Color.WhiteSmoke; //cf.clusterBitmaps1.GradientEnd = Color.Black; //cf.clusterBitmaps1.GradientSteps = 8; }